草庐IT

android - 应用程序 :transformClassesWithJarMergingForDebug'. TransformException : java. util.zip.ZipException:运行 gradlew assembleDebug 时出现重复条目

coder 2023-06-10 原文

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

What went wrong:

Execution failed for task ':app:transformClassesWithJarMergingForDebug'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/slf4j/impl/StaticLoggerBinder.class

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

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

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 文件的内容?

最佳答案

只需从 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/**')  
}

关于android - 应用程序 :transformClassesWithJarMergingForDebug'. TransformException : java. util.zip.ZipException:运行 gradlew assembleDebug 时出现重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41460178/

有关android - 应用程序 :transformClassesWithJarMergingForDebug'. TransformException : java. util.zip.ZipException:运行 gradlew assembleDebug 时出现重复条目的更多相关文章

随机推荐