意外的顶级异常:com.android.dex.DexException:多个 dex 文件定义 [英] UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define

查看:26
本文介绍了意外的顶级异常:com.android.dex.DexException:多个 dex 文件定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经尝试了所有方法,但仍然无法正常工作.

这是错误.

任务:app:dexDebug"执行失败.

<块引用>

com.android.ide.common.internal.LoggedErrorException:无法运行命令:/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/buildAUD/Esktop/My App//pre-dexed/debug/classes-167b9d3c5d689abe004c3fa5b0bcb945d3f0fc8e.jar/Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/google-play-services-ec204c3fa5b0bcb945d3f0fc8e.jar桌面/我的应用程序/Eureka/UDA/app/build/pre-dexed/debug/support-v4-13.0.0-473d85b8d55c88bfed3404072e6c132f96543429.jar/Users/jghg/Desktop/My App/Eureka/UDA-predexed/debug/support-v4-19.0.1-861cc05365a0e9262c764da37d61e3f93dc16de6.jar/Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-1d9377c764caea791f711123b8b678f876c3b6.jar/Users/jghg/Desktop/我的应用程序/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-async-3.0.5-0904cb320185async-3.0.5-0904cb320189ab8b678f876c3b4b320185ab8c001/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/bugs/pre-dexed4-3.0.5-adc1ee9b037c8061429560e6a5fe89ce8e502db6.jar/用户/jghg/桌面/我的应用/尤里卡/UDA/应用/编译/预dexed/调试/twitter4j媒体支持 - 3.0.5-37d138cdc631738d13ddb6f4d34c560a9cd8e048.jar/用户/jghg/桌面/我的应用程序/尤里卡/UDA/app/build/pre-dexed/debug/twitter4j-stream-3.0.5-c96c138ea216b25631a1a8b47520ecaf33f288d8.jar错误代码:2输出:意外的顶级例外:com.android.dex.DexException:多个 dex 文件定义了 Lcom/google/ads/AdRequest$ErrorCode;在 com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)在 com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)在 com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)在 com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)在 com.android.dx.merge.DexMerger.merge(DexMerger.java:188)在 com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)在 com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)在 com.android.dx.command.dexer.Main.run(Main.java:230)在 com.android.dx.command.dexer.Main.main(Main.java:199)在 com.android.dx.command.Main.main(Main.java:103)

谢谢.此致.

解决方案

如果在 build.gradle 的依赖项中多次包含相同的库/目录,就会发生错误.好的,假设您有一个如下所示的 App 结构:

所以你有主应用程序",然后你有一堆子应用程序/模块/库.这些库是:1) gene_test_library, 2) genes_nine_old_androids_library, &3) swipe_list_view_library.

我的名字是 Gene,这就是为什么会有所有这些基因"库的原因.

app"build.gradle 中,我有:

依赖项{编译文件树(包括:['*.jar'],目录:'libs')编译'com.android.support:appcompat-v7:21.0.0'编译项目(':libraries:gene_test_library')//编译项目(':libraries:genes_nine_old_androids_library')编译项目(':libraries:swipe_list_view_library')}

gene_test_librarybuild.gradle 里面,我什么都没有:

依赖项{}

gene_nine_old_androids_librarybuild.gradle 中,我有:

依赖项{编译文件树(目录:'libs',包括:['*.jar'])编译'com.android.support:appcompat-v7:21.0.0'}

build.gradle 中用于 swipe_list_view_library,我有:

依赖项{编译'com.nineoldandroids:library:2.4.0+'编译文件树(目录:'libs',包括:['*.jar'])编译'com.android.support:appcompat-v7:21.0.0'}

这行代码 compile fileTree(dir: 'libs', include: ['*.jar']) 只是意味着嘿,看看这个模块内的 'libs' 文件夹中是否有任何jar 文件.我在任何模块的 libs 文件夹中都没有任何内容,因此您可以忽略该行代码.

假设我取消注释 //compile project(':libraries:genes_nine_old_androids_library')app" 模块的 build.gradle 中.然后我会得到 UNEXPECTED TOP-LEVEL EXCEPTION:" 错误.这是为什么?

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

依赖项{编译文件树(包括:['*.jar'],目录:'libs')编译'com.android.support:appcompat-v7:21.0.0'编译项目(':libraries:gene_test_library')***编译文件树(目录:'libs',包括:['*.jar'])******编译'com.android.support:appcompat-v7:21.0.0'***编译项目(':libraries:swipe_list_view_library')}

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

I have a trouble trying use a Google Play Services on my Android App using Android Studio.

I've tried everything and still doesn't work.

This is the error.

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-dcc11377c764caea791f711123b8b678f876c3b6.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-adc1ee9b037c8061429560e6a5fe89ce8e502db6.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-c96c138ea216b25631a1a8b47520ecaf33f288d8.jar Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define 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)

Thanks. Best Regards.

解决方案

The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:

So you have the main "app" and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.

My name is Gene, so that’s why there are all these "gene" libraries.

Inside the build.gradle for "app", I have:

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')
}

Inside the build.gradle for gene_test_library, I have nothing:

dependencies {
}

Inside build.gradle for gene_nine_old_androids_library, I have:

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

Inside build.gradle for swipe_list_view_library, I have:

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

This line of code compile fileTree(dir: 'libs', include: ['*.jar']) just means "hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.

So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library') In the build.gradle for the "app" module. Then I would get the "UNEXPECTED TOP-LEVEL EXCEPTION:" error. Why is that?

Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for "app", is the same as taking the build dependencies of "genes_nine_old_androids_library" module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for "app" module becomes:

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')
}

Notice how now compile 'com.android.support:appcompat-v7:21.0.0' shows up 2x. That’s where the error is coming from.

这篇关于意外的顶级异常:com.android.dex.DexException:多个 dex 文件定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆