2

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?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39

3 Answers3

4

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • okay i know gson library is used by OkHttp. but now how can i find another and if i am able to find then how can i manage that different versions since i can't remove any of the depedencies listed above – Mukesh Rana Feb 05 '15 at 09:10
  • Use the gradlew dependencies to list all dependencies. – Gabriele Mariotti Feb 07 '15 at 14:16
2

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'
        }
    }
    
TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
  • so how to find these two different versions of gson ?? – Mukesh Rana Feb 05 '15 at 10:00
  • Check for the package. You could do by importing it into class, if there will be two different packages, you would possibly find out what dependencies does the class belong to in suggestions. – TheLittleNaruto Feb 05 '15 at 10:02
0

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

webo80
  • 3,365
  • 5
  • 35
  • 52