Android Studio 中的意外顶级异常

UNEXPECTED TOP-LEVEL EXCEPTION in Android Studio

在我的一个项目中,我在构建 gradle 文件时遇到此异常

Error:Execution failed for task ':emBazaarV4:dexDebug'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command: F:\AndroidSDK\build-tools.1.2\dx.bat --dex --no-optimize --output F:\AndroidStudioWorkspace\EmBazaarV4\emBazaarV4\build\intermediates\dex\debug --input- list=F:\AndroidStudioWorkspace\EmBazaarV4\emBazaarV4\build\intermediates\tmp\dex\debug\inputList.txt Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

这是我的依赖关系

dependencies {
    compile 'com.facebook.android:facebook-android-sdk:3.22.0'
    compile 'com.aviary.android.feather.sdk:aviary-sdk:3.6.3'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.google.android.gms:play-services:+'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile 'com.squareup.okio:okio:1.1.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile files('libs/commons-io-2.4.jar')
    compile files('libs/crashlytics.jar')
    compile files('libs/httpmime-4.1.3.jar')
    compile files('libs/jumblr-0.0.8-jar-with-dependencies.jar')
    compile files('libs/signpost-commonshttp4-1.2.1.2.jar')
    compile files('libs/signpost-core-1.2.1.2.jar')
    compile files('libs/simplesocialsharing.jar')
    compile files('libs/gcm.jar') }

我已经尝试了所有方法,但不知道如何排除这个多个 dex 文件。我找到了这样的 v4 解决方案

 configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

那么对于这类错误有什么通用的解决方案吗?

这个错误

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

当您使用同一库的不同版本时会发生这种情况。

在你的情况下,它发生在 gson 库中: Lcom/google/gson/JsonSerializer;

检查您的依赖项。例如 OkHttp 使用 2.2.3 版本。 https://github.com/square/okhttp/blob/master/pom.xml#L44

正如 Gabriele 所说,TopLevel Exception 总是由于同一库的不同版本的冲突而发生。

First of all I would suggest, avoid using adding file dependency as much as possible for you, Always try to add dependency from the jcenter repository. Looks like gradle takes care of conflicting dependencies.

所以现在,有一些方法可以避免它:

  1. 只使用一个版本,其中包含您需要的所有内容类。就像 app compact 具有所有 v4 支持 类,因此如果您要导入 appcompact v7,则不需要导入 v4 支持版本。

  2. 您始终可以从已编译的依赖项中排除包。例如:

    //for the package:
    dependencies {
        compile("com.android.support:support-v4:21.0.3") {
              exclude group: 'com.android.support', module: 'support-v4'
        }
    }
    

您正在您的应用中使用 mastodontic Google Play 服务。最近在使用的时候可以得到更多的粒度

this guide之后,您可以只使用您想要的部分。可能这会解决问题。你可以这样使用它:

compile com.google.android.gms:play-services-base:6.5.87
compile <... some other google play services part ...>

您可以在 this link

中阅读 "parts" 的完整列表