UNEXPECTED TOP-LEVEL EXCEPTION in Android Studio

2019-05-22 02:40发布

问题:

In one of my projects i am getting this Exception while building gradle file

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

com.android.ide.common.internal.LoggedErrorException: Failed to run command: F:\AndroidSDK\build-tools\21.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)

and here is my dependencies

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

I have tried everything but not getting how to exclude this multiple dex files. I have found solution for v4 like this

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

so is there any general solution for these kind of errors?

回答1:

As Gabriele says, TopLevel Exception always occur due to conflict of different version of same library.

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.

So now, There are some ways to avoid it:

  1. Use only one version which contains all your required classes. like app compact has all v4 support classes, SO you dont need to import v4 support veresion if you're importing appcompact v7.

  2. You can always exclude a package from the compiled dependencies. For Example:

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


回答2:

This error

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files

happens when you are using different versions of the same library.

In you case it happens with the gson library: Lcom/google/gson/JsonSerializer;

Check your dependencies. For example OkHttp uses the 2.2.3 version. https://github.com/square/okhttp/blob/master/pom.xml#L44



回答3:

You are using the mastodontic Google Play Services in your app. Recently, you can get more granularity when using it.

Following this guide, you can use only parts that you want. Probably this will fix the problem. You can use it that way:

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

You can read the entire list of "parts" in this link