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

出现Duplicate类错误:使用新版Android依赖构建失败如何解决?

解决Kotlin重复类错误并保留新版Android依赖方案

出现Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt错误,核心原因是项目依赖树中存在多个版本的Kotlin标准库(kotlin-stdlib/jdk7/jdk8),新版AndroidX依赖与项目现有Kotlin版本不兼容导致冲突。以下是无需降级依赖的解决方法:

  • 统一项目Kotlin版本
    首先确认新版AndroidX依赖对应的推荐Kotlin版本(例如lifecycle:2.6.1推荐Kotlin 1.8.20及以上),然后在项目根目录的build.gradle(或build.gradle.kts)中统一配置:

    buildscript {
        ext {
            kotlin_version = "1.8.20" // 替换为匹配依赖的版本
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    // 模块级build.gradle中确保所有Kotlin依赖引用该版本
    
  • 强制统一依赖版本
    在根build.gradle中添加依赖解析策略,强制所有Kotlin标准库使用指定版本,覆盖依赖自带的旧版本:

    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
        }
    }
    
  • 针对性排除冲突模块
    若不想全局强制版本,可通过依赖树定位冲突来源:执行命令./gradlew app:dependencies(Windows用gradlew.bat app:dependencies),搜索kotlin-stdlib-jdk8找到重复的依赖项,然后在对应的AndroidX依赖中排除冲突模块:

    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
    }
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
    }
    

完成配置后,执行./gradlew clean build清理并重新构建项目,即可保留新版AndroidX依赖同时解决重复类错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 17:17:20