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

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。
http://www.xdnf.cn/news/4241.html

相关文章:

  • 网络安全等级保护有关工作事项[2025]
  • BGP优化
  • 【计算机网络-应用层】HTTP服务器原理理解以及C++编写
  • 从设备交付到并网调试:CET中电技术分布式光伏全流程管控方案详解
  • QT异步线程通信
  • Linux 更改内存交换 swap 为 zram 压缩,减小磁盘写入
  • Android学习总结之Java和kotlin区别
  • Listremove数据时报错:Caused by: java.lang.UnsupportedOperationException
  • 深度解读 ARM 全新白皮书——《重塑硅基:AI 时代的新基石》
  • RabbitMQ-api开发
  • 美团Java高级配送员面经分享|玩梗版
  • Python实例题:高德API+Python解决租房问题
  • Spring Web MVC————入门(1)
  • 15.Spring Security对Actuator进行访问控制
  • 2025年北京市职工职业技能大赛第六届信息通信行业网络安全技能大赛初赛-wp
  • OpenGl实战笔记(2)基于qt5.15.2+mingw64+opengl实现纹理贴图
  • Mysql order by 用法
  • 国标GB28181视频平台EasyCVR安防系统部署知识:如何解决异地监控集中管理和组网问题
  • Latex排版问题:图片单独占据一页
  • 极狐GitLab 如何将项目共享给群组?
  • k倍区间--线段树60/map+思维100
  • ​​6 .数据库规范化与关系理论复习大纲​
  • 64.微服务保姆教程 (七) RocketMQ--分布式消息中间件
  • 常见汇编代码及其指定
  • MySQL 8.0 深度优化:从索引革命到事务增强
  • C语言结构体内存对齐使用场景
  • 飞牛云如何开启及使用ssh:小白用户上手指南-家庭云计算专家
  • Laravel 12 基于 EMQX 实现 MQTT 消息发送与接收
  • 电商数据接口开发进阶:京东 API 实时商品信息采集技术解析​
  • 通过 ModernBERT 实现零样本分类的性能提升