Android Kotlin项目Duplicate Classes构建失败问题求助
解决
:app:checkDebugDuplicateClasses重复类冲突问题 问题原因
com.intellij:annotations:12.0和org.jetbrains:annotations-java5:15.0是同一注解库的不同命名版本——JetBrains后续将原com.intellij:annotations重命名为org.jetbrains:annotations-java5,两者包含大量重复类,同时引入就会触发构建检查失败。
解决方案
1. 定位依赖来源
先执行Gradle命令排查哪个第三方依赖引入了这两个重复库:
# macOS/Linux ./gradlew app:dependencies # Windows gradlew.bat app:dependencies
在输出结果中搜索com.intellij:annotations和org.jetbrains:annotations-java5,找到它们的依赖路径,确定是哪个库带进来的。
2. 精准排除重复依赖
推荐排除旧版本的com.intellij:annotations:12.0,保留更新的org.jetbrains:annotations-java5:15.0。找到引入旧依赖的第三方库,在build.gradle.kts(或build.gradle)的dependencies块中添加排除配置:
// 示例:假设是some.library引入了旧注解库 implementation("some.library:xxx:1.0") { exclude(group = "com.intellij", module = "annotations") }
如果多个依赖都引入了旧版本,也可以全局排除(优先精准排除,全局排除仅作备选):
configurations.all { exclude(group = "com.intellij", module = "annotations") }
3. 强制统一依赖版本
如果排除操作无效,可以通过约束强制统一注解库版本,在项目根目录的build.gradle.kts中添加:
dependencyResolutionManagement { constraints { implementation("com.intellij:annotations:15.0") { because("统一注解库版本,解决重复类冲突") } } }
或者在app模块的build.gradle.kts的dependencies块中添加约束:
dependencies { constraints { implementation("com.intellij:annotations:15.0") } }
4. 清理缓存并重新构建
执行以下命令清理构建缓存,再重新构建项目:
# macOS/Linux ./gradlew clean build # Windows gradlew.bat clean build
内容的提问来源于stack exchange,提问作者MbDevIn01
相关产品推荐
相关产品推荐

