java.util.zip.ZipException:重复条目:如何克服

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30567821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:49:56  来源:igfitidea点击:

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

javaandroidandroid-studio

提问by Mahendra Chhimwal

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 .

我在我的项目中使用了多个 Android 库和模块。每个都有自己的 v4.Support 库。我收到 java.util.zip.ZipException: duplicate entry。当我在项目中搜索重复的类文件时,由于每个库中有多个 v4.support 库,因此存在这些类的多个文件。我知道这个问题在这里被问过很多次,但对我来说没有任何作用。

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?

我的问题是:如何删除这些多个 v4.support 文件?我只想把这个 v4.support 库放一次,所有其他模块都应该从那里引用它。我如何实现这一目标?

Following is my build.gradle script

以下是我的 build.gradle 脚本

// 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.

我收到这样的错误。每次我清理项目时,它都会成功地遵守。但是当我尝试给出 Run 命令时,它显示了以下类型的错误。

Execution failed for task ':packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: android/support/v4/media/TransportMediatorCallback.class

采纳答案by Mahendra Chhimwal

As I workaround my above problem and find a way to overcome it. Main reason for packageAllDebugClassesForMultiDexerror 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..

当我解决上述问题并找到克服它的方法时。packageAllDebugClassesForMultiDex错误的主要原因与您在 Stackoverflow 的所有其他帖子中发现的相同,即您必须有一些在项目中多次可用的类文件。就我而言,我在上述项目中有很多模块,如 Facebook、谷歌付费服务和滑动菜单等。所有这些模块都有自己的 v4.support jar 文件副本。我所做的是从所有这些库 libs 文件夹中删除所有 v4.support 文件。然后从我的 sdks 中添加对 v4.support 库的依赖,即 Android Studio 项目结构中的 com.android.support 库。为了这..

  • 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.

  • 1* 转到文件>项目结构。
  • 2* 一一选择这些模块中的每一个。

  • 3* 在名为 Dependency 的最后一个选项卡中,如果该模块的 libs 文件夹中只有 v4.support lib(或有问题的 lib ,则删除compile fileTree(dir: 'libs', include: '*.jar')

  • 4* 通过单击+添加 v4.support 库, 然后添加库并从您的 sdks 中选择 v4.support 库。

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

完成。清理项目并重新构建它。packageAllDebugClassesForMultiDex问题消失了。

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

至于传递依赖,如果您知道在构建项目时导致 dex 中的 Duplicate file 错误的确切依赖,您可以将其排除为 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
    }