应用:transformClassesWithJarMergingForDebug&#39 ;. TransformException:java.util.zip.ZipException:运行gradlew assembleDebug

时间:2017-01-04 09:21:41

标签: android gradle

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

  

出了什么问题:

     

任务':app:transformClassesWithJarMergingForDebug'执行失败。 com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:   组织/ SLF4J / IMPL / StaticLoggerBinder.class

在我的项目中有两个jar文件: slf4j-android-1.6.1-RC1.jar slf4j-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'
}

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

1 个答案:

答案 0 :(得分:9)

只需从slf4j-android-1.6.1-RC1 jar

中删除以下类
org/sl4j/impl/StaticLoggerBinder.class
org/sl4j/impl/StaticMarkerBinder.class
org/sl4j/impl/StaticMDCBinder.class

您可以在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"
   }
}

注意当您的代码正在编译时,它会每隔一段时间执行一次。

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

jar {
    exclude('org/sl4j/impl/**')  
}
相关问题