如果我从命令行运行gradle assembleDebug,我突然得到这个错误:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.util.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:592)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:550)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:531)
    at com.android.dx.merge.DexMerger.mergeDexBuffers(DexMerger.java:168)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:186)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:300)
    at com.android.dx.command.dexer.Main.run(Main.java:232)
    at com.android.dx.command.dexer.Main.main(Main.java:174)
    at com.android.dx.command.Main.main(Main.java:91)

如果我grep for v4,我会在构建文件夹中看到两个文件。

Binary file build/pre-dexed/debug/support-v4-19.0.0-2ba5fdd60a6c3836b3104a863fe42897da1fa9d1.jar matches
Binary file build/pre-dexed/debug/support-v4-r7-227d905d79b23b20866531d4f700446c040a2ccb.jar matches

我的gradle文件只包括这个支持库:

compile 'com.android.support:support-v13:19.0.0'

我不知道r7库是如何被包含进来的。我已经运行gradle清洁,它总是出现在那里,当我重新运行assembleDebug。

如果我在构建目录中grep r7,我看到它在文件中:

Binary file build/exploded-bundles/ComGoogleAndroidGmsPlayServices4030.aar/classes.jar matches

如果我不包含v13,那么其他东西就不能编译。

但是v13不包括v4支持库吗?

这是播放服务AAR包和v13库之间的不兼容性吗?

我从gradleplease.appspot.com上抓取了gradle文件。

删除播放服务并不能解决这个问题;同样的错误。

我在build.gradle中的依赖项:

 dependencies {


 // Google Play Services
//compile 'com.google.android.gms:play-services:4.0.30'

// Support Libraries
//compile 'com.android.support:support-v4:19.0.0'
///compile 'com.android.support:appcompat-v7:19.0.0'
//compile 'com.android.support:gridlayout-v7:19.0.0'
compile 'com.android.support:support-v13:19.0.0'
compile 'org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5'
compile 'commons-codec:commons-codec:1.9'
compile 'com.madgag:markdownj-core:0.4.1'
compile 'com.wu-man:android-oauth-client:0.0.2'
compile 'com.google.http-client:google-http-client-jackson2:1.17.0-rc'
compile 'org.apache.commons:commons-lang3:3.2'
compile 'com.google.code.gson:gson:2.2.4'
}

当前回答

收到以下错误

“:app:transformDexArchiveWithDexMergerForDebug”任务执行失败。

com.android.build.api.transform.TransformException: com.android.dex.DexException:多个dex文件定义Landroid/support/constraint/ConstraintSet$1

修复:去构建->清洁项目

其他回答

我在一个遗留项目中也犯了同样的错误。我的错误在于支持库被包含了两次:一次是在google-play-services库中,另一次是作为独立的。

我是这样解决的:

坏build.gradle:

dependencies {
   compile files('libs/android-support-v4.jar') 
   compile files('libs/core-2.2.jar')
   compile files('libs/universal-image-loader-1.8.5-with-sources.jar')
   compile 'com.google.android.gms:play-services:3.2.65'
}

好build.gradle:

dependencies {
   // compile files('libs/android-support-v4.jar')  // not needed 
   compile files('libs/core-2.2.jar')
   compile files('libs/universal-image-loader-1.8.5-with-sources.jar')
   compile 'com.google.android.gms:play-services:3.2.65'
}

希望它能帮助到一些人:-)

如果您已经从Eclipse导入了项目。

1. The select project 
2. Go to File -> **Project Structure**
3. Select app in **module** section on left hand panel
4. Select **Dependency** tab
5. Your able to see jars you have added in eclipse project for v4 and v13.
6. Remove that jar by clicking on minus sign at bottom after selection
7. Click on Plus sign select **Library Dependency** 
8. Choose V4 and V13 if added
9. Press Ok and Clean and Rebuild your project

将Eclipse项目导入Android studio后所面临的场景。

希望这能有所帮助。

类似的dex问题解决方法

gradle。Build包含:

compile files('libs/httpclient-4.2.1.jar')
compile 'org.apache.httpcomponents:httpclient:4.5'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

当我移除时,问题就解决了

compile files('libs/httpclient-4.2.1.jar') 

我的gradle现在看起来像:

apply plugin: 'com.android.application'

android {

compileSdkVersion 24
buildToolsVersion "24.0.3"

defaultConfig {
    applicationId "com.mmm.ll"
    minSdkVersion 16
    targetSdkVersion 24
    useLibrary  'org.apache.http.legacy'
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
}

dependencies {

compile 'com.google.android.gms:play-services:6.1.+'
compile files('libs/PayPalAndroidSDK.jar')
compile files('libs/ksoap2-android-assembly-3.0.0-RC.4-jar-with-dependencies.jar')
compile files('libs/picasso-2.1.1.jar')
compile files('libs/gcm.jar')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

JAR文件和已编译的gradle项目中存在冗余

所以要仔细寻找具有相同类的依赖项和jar文件。

去除冗余。 这对我很管用。

另外要注意的是,你可以通过进入android Studio Gradle视图,并选择目标“androidDependencies”来查看你的android依赖项。

还有一个技巧:我一直有这个问题,直到我从项目和相关模块/库项目的libs文件夹中删除了v4支持库。

我也有同样的错误,但这是因为我最近从使用v4改为使用v13。所以我要做的就是清理这个项目。