Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - java.util.zip.ZipException: duplicate entry :how to overcome

I have used multiple Android libraries and modules in my project. Each one has its own v4.Support lib. I am getting java.util.zip.ZipException: duplicate entry . When I search for the duplicate class file in the project, there are multiple files of these classes as because of multiple v4.support lib in each libraries. I know this question has been asked many a times here but nothing is working for me .

My question is : how to remove these multiple v4.support files ? I want to put this v4.support lib only once and all other module should reference it from there. How do I achieve this?

Following is my build.gradle script

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        //classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'io.fabric.tools:gradle:1.15.2'
    }
}

apply plugin: 'android'
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile('com.crashlytics.sdk.android:crashlytics:2.2.3@aar') {
        transitive = true
    }
    compile('com.android.support:multidex:1.0.0')
    //compile ('com.android.support:appcompat-v7:22.1.0')
    compile project(':..:..:..:..:..:..:android_libraries:appcompat')
    compile project(':..:..:..:..:..:..:android_libraries:facebook')
    compile project(':..:..:..:..:..:..:android_libraries:google_play_services:libproject:google-play-services_lib')
    compile project(':..:..:..:..:..:..:android_libraries:SlidingMenu')
    compile project(':..:..:..:..:..:..:android_libraries:StickingGridViewLibrary')
    compile project(':..:..:..:..:..:..:android_libraries:view_pager_library')
    compile project(':..:..:..:..:..:..:android_libraries:ZXing2.3')
    compile project(':..:..:..:..:..:..:android_libraries:xyz')
    compile project(':..:..:..:..:..:..:android_libraries:apptentive')

}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        versionCode 1
        versionName "1.0"
        minSdkVersion 15
        targetSdkVersion 21

        // Enabling multidex support.
        multiDexEnabled true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    ant.importBuild './../../../../../../buildscripts/wlbuild.xml'


    apt {
        arguments {
            androidManifestFile variant.outputs[0].processResources.manifestFile
            // if you have multiple outputs (when using splits), you may want to have other index than 0

            resourcePackageName 'com.kohls.mcommerce.opal'

            // If you're using Android NBS flavors you should use the following line instead of hard-coded packageName
            // resourcePackageName android.defaultConfig.applicationId

            // You can set optional annotation processing options here, like these commented options:
            // logLevel 'INFO'
            // logFile '/var/log/aa.log'
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    buildDir = './../../../../../../build/native'

    lintOptions {
        abortOnError false
    }
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
}

And I am getting errors like this. Each time I clean the project it complies successfully. But when I try to give Run command it shows the following types of error.

Execution failed for task ':packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: android/support/v4/media/TransportMediatorCallback.class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I workaround my above problem and find a way to overcome it. Main reason for packageAllDebugClassesForMultiDex error is the same you find in all other posts in Stackoverflow i.e. you must have some class files which are available more than once in project. In my case I have so many modules in above project as Facebook , google pay services and sliding menu etc.. . All these module have there own copy of v4.support jar file. What I did was delete all v4.support files from all these libraries libs folder. Then Add dependency to v4.support lib from my sdks i.e. com.android.support libs from Android Studio project Structure. For This..

  • 1* GO to File>Project Structure.
  • 2* Select each one of these modules one by one.

  • 3* In the last tab named Dependency , Remove compile fileTree(dir: 'libs', include: '*.jar') if you had only v4.support lib (or the probleomatic one)in libs folder of that module.

  • 4* Add v4.support lib by clicking + then add libraries and select v4.support libs from your sdks.

Its done. Clean the project and build it again.packageAllDebugClassesForMultiDex problem has gone.

As for transitive dependency , if you know the exact dependency which cause the Duplicate file in dex error while building the project, you can exclude it as belo

 compile(project(':..:..:..:..:..:..:android_libraries:walletsdkandroidmodule')) {
        exclude group: 'com.google.code.gson'
    }

If you want any transitive dependency in any module, eg.

compile('com.crashlytics.sdk.android:crashlytics:2.2.3@aar') {
        transitive = true
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...