应用:transformClassesWithJarMergingForDebug".TransformException:java.util.zip.ZipException:运行gradlew assembleDebug时重复的条目

bah*_*tan 16 android gradle

当我gradlew assembleDebug在android studio项目root上运行:command.构建过程失败,我收到此消息:

什么地方出了错:

任务':app:transformClassesWithJarMergingForDebug'的执行失败.com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:org/slf4j/impl/StaticLoggerBinder.class

在我的项目中有两个jar文件:slf4j-android-1.6.1-RC1.jarslf4j-log4j12-1.7.21.jar.这两个罐子都包含两个包含org.sl4j.impl.StaticLoggerBinder的罐子.

这是我的gradle文件内容,它位于app文件夹中:

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.ias.caniasandroid"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        jumboMode true
        javaMaxHeapSize "4g"
    }
    productFlavors {
    }
}

dependencies {
    debugCompile fileTree(include: ['*.jar'], dir: 'libs')
    debugCompile files('libs/commons-lang3-3.4.jar')
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}
Run Code Online (Sandbox Code Playgroud)

如何修复问题并gradlew assembleDebug成功运行而不更改jar文件的内容?

ziL*_*iLk 9

只需从slf4j-android-1.6.1-RC1jar中删除以下类

org/sl4j/impl/StaticLoggerBinder.class
org/sl4j/impl/StaticMarkerBinder.class
org/sl4j/impl/StaticMDCBinder.class
Run Code Online (Sandbox Code Playgroud)

您可以在gradle依赖项中从jar中排除特定类.

为此,使用Copy任务解压缩jar ,排除所需的类,然后在提取的类上添加文件依赖项.

task unzipJar(type: Copy) {
   from zipTree('slf4j-android-1.6.1-RC1.jar')
   into ("$buildDir/libs/slf4j") //TODO: you should update this line
   include "**/*.class"
   exclude "org/sl4j/impl/StaticLoggerBinder.class"
   exclude "org/sl4j/impl/StaticMarkerBinder.class"
   exclude "org/sl4j/impl/StaticMDCBinder.class"
}

dependencies {
   compile files("$buildDir/libs/slf4j") {
      builtBy "unzipJar"
   }
}
Run Code Online (Sandbox Code Playgroud)

注意:编译代码时,它会每隔一段时间执行一次.

另一方面,如果你不想编译包,但如果你想编译它们并从你的JAR中排除你可以使用

jar {
    exclude('org/sl4j/impl/**')  
}
Run Code Online (Sandbox Code Playgroud)