java util zip ZipException: duplicate entry

2019-01-15 11:44发布

问题:

While running the project I'm getting this error

Error:Execution failed for task ':..........'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: javax/annotation/CheckForNull.class

This is how my app:gradle looks like :

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary  'org.apache.http.legacy'

defaultConfig {
    applicationId "com.sample.Example"
    minSdkVersion 15
    targetSdkVersion 24
    multiDexEnabled true
}

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

dependencies {
compile (project(':library')){
    exclude module: 'support-v4'
}
compile (project(':facebookLib')){
    exclude module: 'support-v4'
}
compile 'com.google.http-client:google-http-client-gson:1.20.0'
compile 'com.google.code.gson:gson:2.4'
compile files('libs/CWAC-SackOfViewsAdapter.jar')
compile files('libs/FlurryAgent.jar')
compile files('libs/google-api-client-1.15.0-rc.jar')
compile files('libs/google-api-client-android-1.15.0-rc.jar')
compile files('libs/google-api-services-androidpublisher-v1-rev15-1.15.0-rc.jar')
compile files('libs/google-http-client-1.15.0-rc.jar')
compile files('libs/google-http-client-android-1.15.0-rc.jar')
compile files('libs/google-http-client-jackson-1.15.0-rc.jar')
compile files('libs/google-http-client-jackson2-1.15.0-rc.jar')
compile files('libs/google-oauth-client-1.15.0-rc.jar')
compile files('libs/google-oauth-client-java6-1.15.0-rc.jar')
compile files('libs/httpmime-4.2.jar')
compile files('libs/in-app-purchasing-1.0.3.jar')
compile files('libs/jackson-core-2.1.3.jar')
compile files('libs/jackson-core-asl-1.9.11.jar')
compile files('libs/json_simple-1.1.jar')
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/picasso-2.1.1.jar')
compile files('libs/signpost-commonshttp4-1.2.1.1.jar')
compile files('libs/signpost-core-1.2.1.1.jar')
compile files('libs/signpost-jetty6-1.2.1.1.jar')
compile files('libs/twitter4j-core-4.0.1.jar')
compile files('libs/amazon-device-messaging-1.0.1.jar')
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.android.support:multidex:1.0.1'
}

volley:gradle

android {
compileSdkVersion 17
buildToolsVersion "24.0.3"

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 8
}

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

library:gradle

android {
compileSdkVersion 16
buildToolsVersion "23.0.3"

defaultConfig {
    minSdkVersion 4
    targetSdkVersion 4
}

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

dependencies {
compile 'com.android.support:support-v4:18.0.0'
}

facebookLib:gradle

android {
compileSdkVersion 17
buildToolsVersion "24.0.3"

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 8
}

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

dependencies {
compile 'com.android.support:support-v4:18.0.0'
}

Can someone suggest a way out of this error ? Thanks in advance.

回答1:

com.android.support:support-v4:_____

This library is used multiple times. So here's what you can do. do this on either your library's gradle or facebooklib's gradle:

compile(project(':facebookLib')) {
    exclude module: 'support-v4'
}

OR

compile(project(':library')) {
    exclude module: 'support-v4'
}

Tip: You should make a gradle task to print your dependencies when building and take a look at it. See if there is anything you can remove/improve

EDIT:

run this command in the terminal tab of your Android Studio.

./gradlew app:dependencies

This will print out all dependencies and their (sub)dependencies. Look through it and see which of the dependency is called twice where.

Every time you see the same library twice, you have to use the exclude method showed above to exclude that moudle or group.

See here for more details. It's nice and simple explanation. https://www.linkedin.com/pulse/how-find-dependencies-particular-dependency-gradle-hesamedin-kamalan-1



回答2:

Run the following to find the duplicates

task findDuplicates {
   doLast {
      String findMe = 'javax/annotation/CheckForNull.class'
      configurations.compile.files.each { file ->
         if (file.name.endsWith('.jar')) {
            def classMatches = zipTree(file).matching {
               include findMe
            }.files
            if (!classMatches.empty) {
               println "Found $findMe in $file ${classMatches.size()} time(s)"
            }
         }
      }
   }
}


回答3:

You could do a dependencies cleanup in Android Studio. When doing this you will find the referenced libraries per module in tree view. In that way you will find which dependencies are generating the duplicate entry error. Search in your project which files are using o referencing CheckForNull.class. Take note of the package who contain it.

Open the terminal window in your project and excecute the follow command

gradlew clean app:dependencies

Please note that ALL dependencies will be remove and re-downloaded. If you have a lot of gradle libraries it may take a couple of minutes to download again. After that you will find a summary where you could filter the package you fin. In my case the problem was with the apache.commons package. I do the filter and find something like this:

+--- com.amazonaws:aws-android-sdk-s3:2.4.0
|    +--- com.amazonaws:aws-android-sdk-core:2.4.0 (*)
|    +--- com.amazonaws:aws-android-sdk-kms:2.4.0
|    |    \--- com.amazonaws:aws-android-sdk-core:2.4.0 (*)
|    +--- org.apache.commons:commons-io:1.3.2
|    |    \--- commons-io:commons-io:1.3.2 -> 2.4
|    +--- commons-io:commons-io:2.4
|    \--- org.bouncycastle:bcprov-jdk16:1.44

In my case it was a duplicate entry generated by AWS Android SDK, refered to the commons-io dependency.

Finally, just make the exclusion in your gradle file like this (Remember that in my case it was a diffenrt dependency):

compile('com.amazonaws:aws-android-sdk-s3:2.4.0')
        {
            exclude group: 'org.apache.commons'
        }


回答4:

You don't need to compile same dependencies in each separate module, as your app:gradle is compiling 'com.android.support:support-v4:24.2.1' which has already been compiled in your library gradle as 'com.android.support:support-v4:18.+' where I can see you have added library module as a dependency to app:gradle, here you are trying to replicate it. Same goes with other modules also. Unless they are independent.

Tip: Also don't use anonymous versions like 'com.android.support:support-v4:18.+'instead use appropriate versions.



回答5:

Build -> Rebuild Project in Android Studio fixed it for me.