Mukesh Rana Mukesh Rana - 1 year ago 84
Android Question


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

Error:Execution failed for task ':emBazaarV4:dexDebug'. 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: Multiple dex files define
Lcom/google/gson/JsonSerializer; at
at at
at at at at

and here is my dependencies

dependencies {
compile ''
compile ''
compile ''
compile ''
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-')
compile files('libs/signpost-core-')
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: '', module: 'support-v4'

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

Answer Source

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("") {
              exclude group: '', module: 'support-v4'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download