Android 中解决 annotations 库多版本冲突问题
一、问题描述
在 Android 开发过程中,有时在没有手动添加 annotations 库时也会出现如下错误:
Duplicate class org.intellij.lang.annotations.Identifier found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$AdjustableOrientation found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$BoxLayoutAxis found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$CalendarMonth found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$CursorType found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$FlowLayoutAlignment found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$FontStyle found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
Duplicate class org.intellij.lang.annotations.JdkConstants$HorizontalAlignment found in modules annotations-12.0.jar -> annotations-12.0 (com.intellij:annotations:12.0) and annotations-23.0.0.jar -> annotations-23.0.0 (org.jetbrains:annotations:23.0.0)
这个错误表明在你的项目中存在两个不同版本的 annotations 库。这种冲突会导致编译失败,因为 Gradle 不知道应该使用哪个版本的类。
二、解决办法
1、检查依赖树
如果不是手动添加依赖库导致的冲突,那么就是其他依赖库中有引入 annotations 库,通过 gradle 命令可以查看工程的依赖树,找出具体包含 annotations 库的依赖库。
在 Android 项目工程的根目录下输入以下命令:
./gradlew app:dependencies
(1)添加 JAVA_HOME 环境变量
如果电脑中未设置 JAVA_HOME 环境变量,输入gradlew 命令时会无效,提示未设置。设置 JAVA_HOME 环境变量后需重启 Android Studio 方可生效。
(2)查看依赖树
打开 Android Studio 的 Terminal 终端,在工程的根目录下输入 “./gradlew app:dependencies”,会列出每个依赖库中的所有相关依赖库。在输出中,查找 annotations 库的来源。
从依赖树中可以看出,在 “androidx.room:room-compiler:2.7.1” 和 “ androidx.core:core-ktx:1.10.1” 两个依赖库中都包含了 annotations 库,但是版本号不同。
2、排除重复的依赖库
在 build.gradle 的 dependencies 标签中,可以通过 exclude 来排除其中一个版本。
知道在哪个依赖库中包含了 annotations 库时,可以在该依赖库中进行排除:
implementation(libs.androidx.room.compiler) {exclude("com.intellij", "annotations")}
如果不知道在哪个依赖库中包含了 annotations 库,则对所有依赖库进行检查排除:
// 排除旧的注解库 (annotations-12.0.jar 和 annotations-23.0.0 冲突的解决办法)configurations.all {exclude("com.intellij", "annotations")}
3、统一依赖版本
如果你需要使用某个特定版本的 annotations 库,可以在 build.gradle 文件中的 dependencies 标签下强制使用一个版本。
// 解决 com.intellij:annotations:12.0 和 org.jetbrains:annotations:23.0.0 冲突问题configurations.all {resolutionStrategy {eachDependency {val details = this as DependencyResolveDetails// 强制使用 org.jetbrains:annotations:23.0.0if (details.requested.group == "com.intellij" && details.requested.name == "annotations") {details.useTarget("org.jetbrains:annotations:23.0.0")}
// // 强制使用 com.intellij:annotations:12.0
// if (details.requested.group == "org.jetbrains" && details.requested.name == "annotations") {
// details.useTarget("com.intellij:annotations:12.0")
// }}}}
4、清理项目
有时候,Gradle 缓存可能会导致依赖冲突。尝试清理项目并重新同步:
- 在 Android Studio 中,点击 Build -> Clean Project。
- 点击 File -> Invalidate Caches / Restart… -> Invalidate and Restart 重启 Android Studio。