You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目checkDebugDuplicateClasses任务Kotlin版本冲突求助

解决kotlin-stdlib版本冲突导致的DuplicateClasses错误

1. 强制统一所有Kotlin依赖版本

在项目根目录的build.gradle(或build.gradle.kts)中添加版本约束,强制所有模块使用指定的Kotlin版本,避免依赖自动引入高版本库:

Groovy语法:

allprojects {
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib:1.7.21"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21"
        }
    }
}

Kotlin DSL语法:

allprojects {
    configurations.all {
        resolutionStrategy.force(
            "org.jetbrains.kotlin:kotlin-stdlib:1.7.21",
            "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21",
            "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21"
        )
    }
}

2. 定位并排除引入高版本的依赖

执行Gradle命令生成依赖树,找出哪个第三方库引入了kotlin-stdlib 1.8.0:

./gradlew app:dependencies --configuration debugCompileClasspath

在输出结果中搜索kotlin-stdlib,找到标记为1.8.0的依赖项,然后在对应模块的build.gradle中排除该依赖的Kotlin标准库:

示例(假设是com.example:some-library:1.0.0引入了高版本):

Groovy语法:

implementation('com.example:some-library:1.0.0') {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk7'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
}

Kotlin DSL语法:

implementation("com.example:some-library:1.0.0") {
    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")
    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk7")
    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
}

3. 明确指定Kotlin插件版本

在项目根build.gradle中明确绑定Android Gradle插件和Kotlin插件版本,避免版本不匹配:

Groovy语法:

plugins {
    id 'com.android.application' version '7.5.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
}

Kotlin DSL语法:

plugins {
    id("com.android.application") version "7.5.0" apply false
    id("org.jetbrains.kotlin.android") version "1.7.21" apply false
}

4. 清理缓存并重构

执行以下命令清理Gradle缓存,避免旧版本依赖残留:

./gradlew clean build --refresh-dependencies

如果以上步骤仍未解决问题,可以提供项目完整的依赖树输出,或者关键模块的build.gradle代码片段,以便进一步排查。

内容的提问来源于stack exchange,提问作者Valentin Cerneanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 20:50:25