错误提示

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'kotlin/kotlin.kotlin_builtins' from inputs:
      - E:\202112\gradle-repository\caches\transforms-3\eec51480f0c4097df5888fd5dc97d23a\transformed\jetified-unityLibrary-release\jars\classes.jar
      - E:\202112\gradle-repository\caches\transforms-3\b3ae48471aeb3840f38fca5b419d2ac3\transformed\jetified-kotlin-stdlib-1.7.10.jar
     Adding a packagingOptions block may help, please refer to
     https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/ResourcesPackagingOptions
     for more information

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

原因:

App项目依赖的三方AAR,而AAR中编译后的字节码class.jar包含相同的kotlin-stdlib-1.7.10.jar文件,导致资源merge出现异常。官方给出了推荐的解决方案

1.方案一 配置exclude策略

首先需要了解 Maven坐标(GAV)的查找详解
GAV坐标是用来定位Maven仓库中指定资源位置适应的一套规则。

  • groupId:定义当前Maven组织名称
  • artifactId:定义实际项目名称
  • version:定义当前项目的当前版本

配置exclude需要配合着GAV坐标来使用,比如下面配置

dependencies {
    implementation('com.github.bumptech.glide:glide:4.13.2') {
        exclude group: 'com.github.bumptech.glide', module: 'annotations
    }
}

可以排除com.google.guava:guava所有的传递依赖,只依赖显式声明的依赖项

dependencies {
    implementation('com.google.guava:guava:23.0') {
        transitive = false
    }
}

默认情况下,依赖com.github.bumptech.glide:glide:4.13.2同时会同时依赖红框中的传递依赖项。但是,有些传递项用不到,就可以使用 exclude取消传递依赖项。 maven仓库地址
在这里插入图片描述
在工程目录下可以找到相关的依赖
在这里插入图片描述
在这里插入图片描述
这里的annotations项目中没有使用,就想移除。按照上面的配置重新构建项目,就会发现的annotations:4.13.2 就被移除了。

tips:
这种方式仅仅可以处理单个依赖项的依赖的传递,如果有其他依赖项有exclude 的依赖,最终还是会打包到apk中的。

如果需要自动排除在上声明的所有依赖项的传递依赖项。(官方例子)

configurations {
    implementation {
        exclude group: 'commons-collections', module: 'commons-collections'
    }
}

dependencies {
    implementation 'commons-beanutils:commons-beanutils:1.9.4'
    implementation 'com.opencsv:opencsv:4.6'
}
2.方案二 配置picker策略

顶部贴的代码是项目构建时报错问题,我们打开E:\202112\gradle-repository\caches\transforms-3\eec51480f0c4097df5888fd5dc97d23a\transformed\jetified-unityLibrary-release\jars\classes.jar,aar目录class.jar 发现内部包含kotlin相关目录,和以 kotlin_builtins结尾的文件,这些文件在app主模块中已经包含,所有红圈中的内容不需要打入apk包中,或者进行ignore处理。
在这里插入图片描述
什么是.kotlin_builtins文件

使用这些文件主要有两种场景:

1.编译器从类路径上的 kotlin-stdlib 中查找它们以确定哪些内置声明可用
2.反射库 (kotlin-reflect) 将这些文件作为资源加载,以为内置声明提供反射功能。例如,String::class.members 返回类 kotlin.String
的所有成员,与 Kotlin 编译器看到这些成员的方式完全相同(尽管事实上存在没有 kotlin/String.class
文件,它被删除为字节码中的 java.lang.String

应用内配置文件路径
/app/build.gradle


android {
    packagingOptions {
        pickFirst "kotlin/kotlin.kotlin_builtins"
        pickFirst "kotlin/ranges/ranges.kotlin_builtins"
        pickFirst "kotlin/reflect/reflect.kotlin_builtins"
        pickFirst "kotlin/collections/collections.kotlin_builtins"
        pickFirst "kotlin/annotation/annotation.kotlin_builtins"
        pickFirst "kotlin/coroutines/coroutines.kotlin_builtins"
        pickFirst "kotlin/internal/internal.kotlin_builtins"
        pickFirst "DebugProbesKt.bin"
        resources {
            excludes += ['META-INF/gradle/incremental.annotation.processors']
        }
    }
}

3.方案三 配置 merge策略

Gradle 官方文档
Android Plugin DSL Refereence

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐