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

时间:2016-08-01 19:20:35

标签: java android gradle android-gradle build.gradle

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

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

该错误似乎表明CopyUtils.class中的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

我的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'])
}

我好像现在得到了

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

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

    Wed Jul 06 21:02:27 PDT 2016

    distributionBase = GRADLE_USER_HOME distributionPath =包装/ 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
    }
    

    }

    依赖{     编译fileTree(包括:['* .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' }
    

    }

    5 个答案:

    答案 0 :(得分: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')
        }
    }
    

    冲突的原因是org.apache.commons:commons-io:1.3.2错误地推动了gradle :main:dependencyInsight --configuration compile --dependency commons-io

    您可以通过

    查看依赖关系的来源

    `Regex regex = new Regex("[^0-9-]+"); TextP1_TextChanged = regex.IsMatch(TextP1.Text);`

    答案 1 :(得分:12)

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

    configurations {
        all*.exclude group: 'org.apache.commons'
    }
    

    享受!!!

    答案 2 :(得分:1)

    编辑2:

    我创建了一个fork项目,可以在Android Studio 2.2预览版7上编译和运行。如果您不想重新下载项目,还可以查看diff。< / p>

    编辑:

    这是为我编译的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'
    }
    

    尝试Maxence Barroy所说的内容。如果这不起作用,请查看this answer

    compile('com.example:some-dependency:4.2') {
        exclude module: 'commons-io'
    }
    

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

    答案 3 :(得分:0)

    另外,您可以查看gradle版本。上面的属性应该导致合并冲突。

    <强> gradle-wrapper.properties

    distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
    

    <强>的build.gradle

    classpath 'com.android.tools.build:gradle:2.1.3'
    

    请尝试以上属性。

    <强> gradle-wrapper.properties

    distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
    

    build.gradle

    classpath 'com.android.tools.build:gradle:2.3.3'
    

    答案 4 :(得分:0)

     compile('org.apache.commons:commons-io:1.3.2') {
            exclude module: 'commons-io'
     }
    

    试试..