如果我从命令行运行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'
}

当前回答

如果有人发现CommonsWare的答案不能应用到android库项目,这里有一个片段来修复

编制(项目(':yourAndroidLibrary ')) { 排除模块:'support-v13' }

你会发现问题

发现不支持的Gradle DSL方法:'exclude()'

如果你使用 编译项目(“:yourAndroidLibrary”){ 排除模块:'support-v13' }

区别在于“project”之前的手链“(”和“)”。

其他回答

收到以下错误

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

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

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

如果您已经从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文件。

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

最后,我解决了它修改模块gradle文件上的这些属性

compileSdkVersion 25 targetSdkVersion 25 编译的com.android.support: appcompat-v7: + 编译的com.android.support: recyclerview-v7: +

在Android Studio中,转到你的构建。Gradle(检查项目和模块的构建。Gradle文件)并搜索重复的依赖项。

删除那些你的项目不需要的。