51

Whever I try to debug and deploy my android application (in Android Studio 0.9) I get the following error:

Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
java.util.zip.ZipException: duplicate entry: android/support/multidex/BuildConfig.class

To make things clear here is a brief history of my actions:

  1. This morning the project was working fine
  2. Added some additional classes and methods
  3. Broke the limit and received this error: Unable to execute dex: method ID not in [0, 0xffff]: 65536
  4. Decided to add multiDex support to my project as reducing the dependencies was not an option

Since then I keep getting the described error just after adding multiDex to my project by following this SO post Using Gradle to split external libraries in separated dex files to solve Android Dalvik 64k methods limit.

Here is my build.gradle file:

apply plugin: 'com.android.application'

repositories {
    jcenter()
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.0'

    defaultConfig {
        applicationId "com.stackoverflow.application"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        preDexLibraries = false
    }
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex'
        dx.additionalParameters += "--main-dex-list=$projectDir/<filename>".toString() // enable the main-dex-list
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':viewPagerIndicatorLibrary')
    compile 'com.google.android:multidex:0.1'
    compile 'com.j256.ormlite:ormlite-android:4.48'
    compile 'com.j256.ormlite:ormlite-core:4.48'
    compile 'de.greenrobot:eventbus:2.2.1'
    compile 'se.emilsjolander:stickylistheaders:2.5.1'
    compile 'joda-time:joda-time:2.5'
    compile 'com.makeramen:roundedimageview:1.4.0'
    compile 'javax.inject:javax.inject:1'
    compile 'com.squareup.picasso:picasso:2.3.4'
    compile 'com.googlecode.libphonenumber:libphonenumber:6.3.1'
    compile('com.google.api-client:google-api-client-gson:1.18.0-rc') {
        exclude module: 'httpclient'
    }

    compile 'com.google.android.gms:play-services:6.1.71'
    compile('com.google.api-client:google-api-client:1.17.0-rc') {
        exclude(group: 'xpp3', module: 'xpp3')
        exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
        exclude(group: 'junit', module: 'junit')
        exclude(group: 'com.google.android', module: 'android')
    }
    compile('com.google.api-client:google-api-client-android:1.17.0-rc') {
        exclude(group: 'com.google.android.google-play-services', module: 'google-play-services')
    }
    compile('com.google.http-client:google-http-client-android:1.17.0-rc') {
        exclude(group: 'com.google.android', module: 'android')
    }
    compile 'com.google.guava:guava:18.0'
}

I also have another project dependencies to use the viewPagerIndicator library as well as a few jars in my /libs folder:

  • android-async-http-1.4.6.jar
  • guice-3.0-no_aop.jar
  • jsr305-1.3.9.jar
  • roboguice-2.0.jar

Any advice on how I could resolve this problem without removing any of my needed dependencies is welcomed !

11 Answers 11

32

Thread is a bit old, but I got this error too.

My problem was that I used two different com.google.android.gms:play-services Versions in my gradle file.

compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-analytics:7.3.0' // WRONG!

Make sure, that you always use same versions like:

compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-analytics:7.5.0'
Sign up to request clarification or add additional context in comments.

1 Comment

I also face the similar issue.. thanx Dennis
14

Edit: This is a bug and a fix is due. See https://code.google.com/p/android/issues/detail?id=81804

I have this problem too, and I don't have an answer. But here's what I can add:

The class BuildConfig is a magic class generated as part of the build process. For some reason, there exists a version with the same fully-qualified name (android.support.multidex.BuildConfig) in both mutildex-1.0.0 and multidex-instrumentation-1.0.0 aars.

I don't believe we have done anything wrong. I think this is a symptom of being on the cutting-edge. I raised a bug report.

2 Comments

Hello thanks for you precisions and actions, I hope that one day a solution will be found to solve this problem. For the moment I use several "hacks" to not break this Dalvik's 65k limit.
The referenced bug is now marked as fixed
12

To diagnose and fix this problem, run this gradle command:

./gradlew clean app:dependencies

this will list all of the dependencies in your app in a tree. Search the results for the offending duplicate class and add

compile('naughty.library') {
    exclude group: 'foo', module: 'bar'
}

to remove the duplicate.

1 Comment

Thanks! Thanks for showing how to diagnose. Is the only way I found to really know where was the error
6

I recently had this error, and after looking at my "External Libraries" in Android studio, it turns out one of my libraries had been included under two version numbers. (In this case it was wire-runtime 1.5.1 and 1.5.2).

What I would recommend is to look inside "External Libraries" in your Project view, and see if there are any redundant libraries there. It includes transitive dependencies as well so you might find something there that surprises you.

4 Comments

I have checked several times indeed if the problem could appear in the external libraries but nothing revelant showed up. I have no duplicates so far.
related to this, I've inadvertently manually put one version of a jar under libs/ (with the dependency compile fileTree(dir: 'libs', include: '*.jar') ), but also referenced the library again later with a compile line pulling from maven. The fix was to remove one or the other!
Same here... Didn't notice any double libraries since there were none... Then I read that Analytics is now included in Google Play Services and I had a jar reference in my project. Removing that fixed my problem...
qix, if I could I'd upvote that comment a million times. You just saved my day.
3

1.update google play service

2.add

compile 'com.google.android.gms:play-services-fitness:8.1.0'
compile 'com.google.android.gms:play-services-wearable:8.1.0'

instead of compile 'com.google.android.gms:play-services:8.1.0'

in build.gradle file.

3.

defaultConfig {
    multiDexEnabled true
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

Comments

1

In my case the reason was Facebook Android SDK. Just exclude transitive dependency:

compile('com.facebook.android:facebook-android-sdk:+') {
    exclude group: 'com.android.support', module: 'multidex'
}

In your case it can be some other dependency - just sort through them one by one and you'll find the one who has transitive multidex dependency.

Comments

0

If you enable multiDex, you should stop doing the logic in afterEvaluate {}. The multi-dex support will take care of the main dex list for you.

2 Comments

Sadly removing that part won't change change anything. What I suppose is needed to be done is to find and resolve some conflicting dependencies.
There is something weird going on, because it's trying to package a .class in the APK which it really shouldn't, and I'm not sure where this is coming from. It actually seems to be unrelated to multi-dex. My comment about remove afterEvaluate {} still stand though (but for another reason).
0

This type of error like duplicate entry occurs when you some class is at more than one place. To overcome this problem , Simple search the class which shows duplicate entry in project. It will show you all paths where this class present more than one place. In Windows CNTRL+N is keyboard shortcut for search in files. Simple try to remove one of the lib or file and the problem is solved.

Comments

0
tools:overrideLibrary="com.google.android.gms.location, com.google.android.gms.internal"

Did the trick for me (I guess you should change location with whatever google library you use)

3 Comments

where do I do this?
in manifest , its a tag for the uses-sdk item <uses-sdk android:minSdkVersion="8" tools:overrideLibrary= ... />
Why does this specifically solve java.util.zip.ZipException? And shouldn't minSdkVersion be specified in gradle?
0

You are adding a library like this
(A). compile files('libs/YOUR_LIBRARY.jar')
and this library package already available in your code.
comment it like or remove this line
//compile files('libs/YOUR_LIBRARY.jar')

How to know duplicate-copy of library in your project:
unzip your "YOUR_LIBRARY.jar" and see the same class name in your code.

Comments

-1

go to File/Invalidate Chaches/Restart and your problem is solved

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.