Android gradle 构建任务期间的重复条目:transformClassesWithJarMergingAndroidTest

Duplicate Entry during Android gradle build in Task :transformClassesWithJarMergingAndroidTest

我在使用 Gradle 构建我的 Android 应用程序时遇到以下异常:

Execution failed for task ':transformClassesWithJarMergingForGoogleGermanDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDesc

问题似乎是在我的 build.gradle 文件中我声明了:

testCompile 'org.hamcrest:hamcrest-all:1.3'
androidTestCompile 'org.hamcrest:hamcrest-all:1.3'

但是,我需要单元测试和集成测试的依赖项。如何解决?

尝试为重复条目添加 exclude 参数。

androidTestCompile 'org.hamcrest:hamcrest-all:1.3' {
    exclude module: 'BaseDesc'
}
 buildTypes {
       release {
          multiDexEnabled true;
      }
 }

试试这个应该有用

问题是另一个 jar (Mockito) 包含 hamcrest-core 作为传递依赖项。此模块包含软件包名称 org.hamcrest.* 下的所有 类。因此发生了冲突。解决方案是:

configurations {
    all*.exclude group: 'org.hamcrest', module: 'hamcrest-core'
}

如此处所述:https://docs.gradle.org/current/userguide/dependency_management.html第 23.4.7 章