当前位置: 首页 > news >正文

Android Execution failed for task ‘:app:mergeDebugJavaResource

错误提示

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.jarAdding a packagingOptions block may help, please refer tohttps://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/ResourcesPackagingOptionsfor 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取消传递依赖项。
在这里插入图片描述
在工程目录下可以找到相关的依赖
在这里插入图片描述
在这里插入图片描述
这里的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

http://www.lryc.cn/news/34322.html

相关文章:

  • spring事物源码分析
  • 炫龙游戏本Win10系统总是蓝屏崩溃怎么办?
  • 华为OD机试题,用 Java 解【数字加减游戏】问题
  • C++ 手写一个高性能json生成与解析器
  • java——了解反射
  • The Sandbox 中的独特体验——《奥米加》
  • 76 Python写入csv文件时出现空行_newline参数解决
  • 高等数学——定积分和不定积分
  • imx6 usb增强信号强度
  • 深入理解性能压测工具原理
  • Java的概述和运行方式
  • 【C语言】每日刷题 —— 牛客
  • JavaEE课程实践-Servlet的部署(tomcat服务器)
  • Java 中的拆箱和装箱
  • 从0开始自制解释器——实现多位整数的加减法计算器
  • (12)C#传智:File类,泛型,字典,FileStream,StreamReader,多态
  • Dubbo的服务暴漏与服务发现源码详解
  • Python 的IDE——PyCharm
  • 01 C语言使用链表实现队列(Queue、FIFO)模块
  • 2.2操作系统-进程管理:前趋图、前趋图与PV操作
  • 凤凰游攻略
  • Nginx 高可用方案
  • Linux基本指令
  • Linux系统基础命令(二)
  • 【C++】C++11——简介|列表初始|简化声明|nullptr与范围for|STL中的变化
  • Python -- 函数
  • Pytorch中utils.data 与torchvision简介
  • 学习 Python 之 Pygame 开发魂斗罗(十)
  • Keepalive+LVS群集部署
  • 数组、指针总结【面试题】