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

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。

相关文章:

  • 从零搭建体育比分网站完整步骤
  • 高等数学第六章---定积分(§6.1元素法6.2定积分在几何上的应用1)
  • 【C++游戏引擎开发】第30篇:物理引擎(Bullet)—软体动力学系统
  • 【Linuc】深入理解 Linux 文件权限
  • 【MySQL】-- 数据库约束
  • SPP 和 yolo 中的SPP
  • 栈与队列详解及模拟实现
  • spring cloud gateway(网关)简介
  • 【HTML5】显示-隐藏法 实现网页轮播图效果
  • 路线 北大国际医院
  • Deepseek流式操作与用户行为数据分析day01
  • MySQL中MVCC指什么?
  • SQL大场笔试真题
  • 笔记本外接显示器检测不到hdmi信号
  • RabbitMq(尚硅谷)
  • 基于docker使用showdoc搭建API开发文档服务器
  • python + whisper 读取蓝牙耳机, 转为文字
  • 如何把阿里云a账号下面的oss迁移到阿里云b账号下面(同区域)
  • vue插槽类禁止绑定class样式等(纪录篇)
  • C++ vector 介绍与使用
  • 纪念苏联伟大卫国战争胜利80周年阅兵彩排,解放军仪仗队亮相
  • 印度袭击巴基斯坦已致至少3人死亡
  • 专家解读《人源类器官研究伦理指引》:构建类器官研究全过程伦理治理框架
  • 景点变回监狱,特朗普下令重新启用“恶魔岛”
  • 黔西游船倾覆事故84名落水人员已全部找到,10人不幸遇难
  • 深入景区、文化街区及消费一线,多地省委书记调研文旅市场