意外的顶级异常:com.android.dex.DexException:多个dex文件定义

我在尝试使用 Android Studio 的 Android App 上使用 Google Play 服务时遇到问题。

我已经尝试了一切,但仍然不起作用。

这是错误。

Execution failed for task ':app:dexDebug'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command: /Users/jghg/Desktop/My App/Android/SDK/android-sdk-mac_86/build-tools/19.0.1/dx --dex --output /Users/jghg/Desktop/My App/Eureka/UDA/app/build/libs/app-debug.dex /Users/jghg/Desktop/My App/Eureka/UDA/app/build/classes/debug /Users/jghg/Desktop/My App/Eureka/UDA/app/build/dependency-cache/debug /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/classes-08979151dd1373bd3f799299d93376d22d4afa46.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/classes-167b9d3c5d689abe004c3fa5b0bcb945d3f0fc8e.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/google-play-services-ec20f8af7bb457c5095cae1afa0cee722582f198.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-13.0.0-473d85b8d55c88bfed3404072e6c132f96543429.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-19.0.1-861cc05365a0e9262c764da37d61e3f93dc16de6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-19.0.1-dcc11377c764caea791f7 11123b8b678f876c3b6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-async-3.0.5-0904cb320186fb23a9a9bf25a048c5bc4ec07bc2.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-core-3.0.5-41d2d5805e2d90cf77813a126306c4cbe22583ae.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-examples-3.0.5-adc1ee9b037c8061429560e6a5fe89ce8 e502db6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-media-support-3.0.5-37d138cdc631738d13ddb6f4d34c560a9cd8e048.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-stream-3.0.5-c96c138ea216b25631a1a1a8b47520ecaf33f288d8.jar 错误代码: 2 输出: 意外的顶级异常: com.android.dex.DexException: 多个 dex 文件定义 Lcom/google/ads/AdRequest$ErrorCode;at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170) at com.android.dx.merge.DexMerger.merge(DexMerger.java:188) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103)

谢谢。此致敬意。


答案 1

当您在 build.gradle 的依赖项中多次包含同一库/目录时,会发生此错误。好吧,假设你有一个如下所示的应用结构:

enter image description here

所以你有主要的“应用程序”,然后你有一堆子应用程序/模块/库。这些库是:1) 、 2) 和 3)。gene_test_librarygenes_nine_old_androids_libraryswipe_list_view_library

我的名字是Gene,所以这就是为什么有所有这些“基因”库。

在里面的,我有:build.gradle“app”

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.0'

    compile project(':libraries:gene_test_library')
    //compile project(':libraries:genes_nine_old_androids_library')
    compile project(':libraries:swipe_list_view_library')
}

里面的 for,我什么都没有:build.gradlegene_test_library

dependencies {
}

里面的,我有:build.gradlegene_nine_old_androids_library

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
}

里面的,我有:build.gradleswipe_list_view_library

dependencies {
    compile 'com.nineoldandroids:library:2.4.0+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
}

这行代码只是意味着“嘿,看看这个模块内的'libs'文件夹中的任何jar文件。我在任何模块的libs文件夹中都没有任何东西,所以你可以忽略那行代码。compile fileTree(dir: 'libs', include: ['*.jar'])

因此,假设我取消注释 在模块中。然后我会得到错误。为什么?//compile project(':libraries:genes_nine_old_androids_library')build.gradle“app”“UNEXPECTED TOP-LEVEL EXCEPTION:”

enter image description here

好吧,在 for 中编写 与获取模块的构建依赖项并将其放在那里是一样的。因此,取消注释语句,for “app”模块将变为://compile project(':libraries:genes_nine_old_androids_library')build.gradle“app”“genes_nine_old_androids_library”//compile project(':libraries:genes_nine_old_androids_library')build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.0'

    compile project(':libraries:gene_test_library')
    ***compile fileTree(dir: 'libs', include: ['*.jar'])***
    ***compile 'com.android.support:appcompat-v7:21.0.0'***
    compile project(':libraries:swipe_list_view_library')
}

请注意现在如何显示 2x。这就是错误的来源。compile 'com.android.support:appcompat-v7:21.0.0'


答案 2

您的Google Play服务库正在从项目的其他依赖项中导出,并且在编译时dex编译器会感到困惑。

如果您使用的是 Gradle,则将其包含在项目的 build.gradle 中应该会将支持库从导出到主项目中排除。

apply plugin: 'android'
apply plugin: 'crashlytics'

/** Must exclude exported support jars from dependencies, or get dex duplicate class      error.
*  we're
**/
configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
    all*.exclude group: 'com.google.android.gms', module: 'play-services'
}

如果您使用的是andoid工作室构建系统。然后,您应该转到文件 -> 项目结构并禁用 -> 模块。浏览每个模块,然后单击依赖项选项卡,取消选中支持库和Google Play服务库的导出列。

如果您需要更多帮助,请发表评论。


推荐