Gradle构建失败:java.util.zip.ZipException:重复条目:org/apache/commons/io/CopyUtils.class

ins*_*rum 6 java android gradle build.gradle android-gradle-plugin

当我尝试在我的nexus 7上运行应用程序时,gradle构建每次都会失败并给出相同的错误:

> com.android.build.api.transform.TransformException:
java.util.zip.ZipException: duplicate entry:
org/apache/commons/io/CopyUtils.class
Run Code Online (Sandbox Code Playgroud)

该错误似乎表明CopyUtils.classfrom commons-io在构建过程中被包含两次.

完整日志:

Information:Gradle tasks [:android:assembleDebug]

:android:preBuild UP-TO-DATE

:android:preDebugBuild UP-TO-DATE

:android:checkDebugManifest

:android:preReleaseBuild UP-TO-DATE

:android:prepareComAndroidSupportMultidex101Library UP-TO-DATE

:android:prepareDebugDependencies

:android:compileDebugAidl UP-TO-DATE

:android:compileDebugRenderscript UP-TO-DATE
:android:generateDebugBuildConfig UP-TO-DATE

:android:mergeDebugShaders UP-TO-DATE

:android:compileDebugShaders UP-TO-DATE

:android:generateDebugAssets UP-TO-DATE

:android:mergeDebugAssets UP-TO-DATE

:android:generateDebugResValues UP-TO-DATE


:android:generateDebugResources UP-TO-DATE

:android:mergeDebugResources UP-TO-DATE

:android:processDebugManifest UP-TO-DATE

:android:processDebugResources UP-TO-DATE

:android:generateDebugSources UP-TO-DATE

:android:incrementalDebugJavaCompilationSafeguard UP-TO-DATE

:android:compileDebugJavaWithJavac UP-TO-DATE

:android:compileDebugNdk UP-TO-DATE

:android:compileDebugSources UP-TO-DATE

:android:prePackageMarkerForDebug
:android:transformClassesWithJarMergingForDebug FAILED
Error:Execution failed for task 
':android:transformClassesWithJarMergingForDebug'.

> com.android.build.api.transform.TransformException: 
java.util.zip.ZipException: duplicate entry: 
org/apache/commons/io/CopyUtils.class

Information:BUILD FAILED

Information:Total time: 11.208 secs

Information:1 error

Information:0 warnings

Information:See complete output in console
Run Code Online (Sandbox Code Playgroud)

还有我的build.gradle:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"


    defaultConfig {
        applicationId "org.wildstang.wildrank.android"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

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

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.+'
    compile 'com.android.support:support-v13:19.+'
    compile 'de.congrace:exp4j:0.3.+'
    compile 'org.apache.commons:commons-io:1.3.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
Run Code Online (Sandbox Code Playgroud)

我好像现在得到了

错误:(43,0)未找到Gradle DSL方法:'com.android.support:support-v4:24.1.1()'可能的原因:

  • 项目'wildrank-android-master'可能正在使用不包含该方法的Gradle版本.打开Gradle包装文件
  • 构建文件可能缺少Gradle插件.应用Gradle插件
  • 我的Gradle包装器

    2016年7月6日星期三21:02:27 PDT 2016

    distributionBase = GRADLE_USER_HOME distributionPath = wrapper/dists

    zipStoreBase = GRADLE_USER_HOME

    zipStorePath =包装/ dists中

    distributionUrl = HTTPS://services.gradle.org/distributions/gradle-2.10-all.zip

    我的新gradle文件

    apply plugin:'com.android.application'//重要提示:'com.android.application'不是

    android {compileSdkVersion 24 //编译sdk应该总是最新的buildToolsVersion"24.0.1"//不知道这是否重要

    defaultConfig {
        applicationId "org.wildstang.wildrank.android"
        minSdkVersion 14
        targetSdkVersion 19 //Looks like this is a new app, why are you using 19 and not 24?
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    
    lintOptions {
        abortOnError false
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

    dependencies {compile fileTree(include:['*.jar'],dir:'libs')//重要

    //Ignore these, I don't think they are important for you, but I needed them to get my test project to compile
    // These version numbers worked for me:
    compile 'com.android.support:support-v4:24.1.1'{exclude group: 'org.apache.commons', module: 'commons-io' }
    compile 'com.android.support:support-v13:24.1.1'{ exclude module: 'commons-io' }
    compile 'de.congrace:exp4j:0.3.11'{ exclude module: 'commons-io' }
    compile 'org.apache.commons:commons-io:1.3.2'{ exclude module: 'commons-io' }
    
    Run Code Online (Sandbox Code Playgroud)

    }

    Ser*_*kyi 22

    可以选择在gradle依赖关系解析级别上修复它

    configurations.all {
        resolutionStrategy.dependencySubstitution {
            substitute module('org.apache.commons:commons-io:1.3.2') with module('commons-io:commons-io:1.3.2')
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    冲突的原因是org.apache.commons:commons-io:1.3.2错误推送/sf/answers/2619525611/

    您可以看到依赖关系的来源

    gradle :main:dependencyInsight --configuration compile --dependency commons-io

    • 这解决了问题,只需在声明maven库后放入此代码即可 (2认同)

    Ras*_*med 12

    请在build.gradle文件中使用以下代码.

    configurations {
        all*.exclude group: 'org.apache.commons'
    }
    
    Run Code Online (Sandbox Code Playgroud)

    请享用!!!


    SUP*_*LEX 1

    编辑2:

    我创建了该项目的一个分支,可以在 Android Studio 2.2 预览版 7 上编译和运行。如果您不想重新下载该项目,也可以查看差异

    编辑:

    这是build.gradle为我编译的文件。我已经评论了所有重要的变化:

    apply plugin: 'com.android.application' //IMPORTANT: 'com.android.application' not 
    
    android {
        compileSdkVersion 24 // compile sdk should always be latest
        buildToolsVersion "24.0.1" // Don't know if this matters
    
    
        defaultConfig {
            applicationId "org.wildstang.wildrank.android"
            minSdkVersion 14
            targetSdkVersion 19 //Looks like this is a new app, why are you using 19 and not 24?
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
        }
        dexOptions {
            javaMaxHeapSize "4g"
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        lintOptions {
            abortOnError false
        }
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs') //IMPORTANT
    
        //Ignore these, I don't think they are important for you, but I needed them to get my test project to compile
        compile 'com.android.support:appcompat-v7:24.1.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'
        compile 'com.android.support:design:24.1.1'
    
        // These version numbers worked for me:
        compile 'com.android.support:support-v4:24.1.1'
        compile 'com.android.support:support-v13:24.1.1'
        compile 'de.congrace:exp4j:0.3.11'
        compile 'org.apache.commons:commons-io:1.3.2'
    }
    
    Run Code Online (Sandbox Code Playgroud)

    试试Maxence Barroy所说的。如果这不起作用,请查看这个答案

    compile('com.example:some-dependency:4.2') {
        exclude module: 'commons-io'
    }
    
    Run Code Online (Sandbox Code Playgroud)

    由于我不知道你的build.gradle文件是什么样的,所以我无法真正帮助你,但也请查看这个答案。另外,请确保您拥有最新版本的 Gradle'com.android.tools.build:gradle:2.2.0-alpha6'