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

Android Studio中Kotlin重复类冲突问题如何解决?

问题描述

项目构建时出现重复类冲突:

  • 模块jetified-kotlin-stdlib-1.8.0(org.jetbrains.kotlin:kotlin-stdlib:1.8.0)与jetified-kotlin-stdlib-jdk7-1.3.31(org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31)中存在重复类kotlin.internal.jdk7.JDK7PlatformImplementations
  • 上述两个模块还存在重复类kotlin.jdk7.AutoCloseableKt

已执行./gradlew clean命令,问题未解决。当前项目根目录build.gradle内容如下:

buildscript {
    ext {
        buildToolsVersion = "30.0.0"
        minSdkVersion = 23
        compileSdkVersion = 33
        targetSdkVersion = 30
        googlePlayServicesAuthVersion = "16.0.1"
        kotlinVersion = "1.8.0"
    }
    
    firebase: [
        bom           : "26.0.0"
    ]
    
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {

        classpath "com.android.tools.build:gradle:4.2.0" 
        classpath "com.google.gms:google-services:4.3.4"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

    }
}
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
allprojects {
    repositories {
        configurations.all {
            resolutionStrategy {

                force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
            }
        }
        mavenLocal()
        maven {

            url("$rootDir/../node_modules/react-native/android")
        }
        maven {

            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven { url 'https://maven.google.com' }
        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}

解决方案

冲突根源是项目同时引入了不同版本的Kotlin标准库,且从Kotlin 1.6开始,kotlin-stdlib-jdk7的功能已经合并到主kotlin-stdlib中,低版本的kotlin-stdlib-jdk7和高版本主库内容重叠导致冲突。以下是两种可行解决办法:

方案1:全局强制统一Kotlin标准库版本

修改根目录build.gradle的allprojects块,在resolutionStrategy中添加规则,强制所有依赖使用项目指定的Kotlin版本:

allprojects {
    repositories {
        configurations.all {
            resolutionStrategy {
                force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
                // 强制所有Kotlin相关依赖统一使用指定版本
                force "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
                force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
                force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
            }
        }
        // 保留原有的仓库配置
        mavenLocal()
        maven {
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven { url 'https://maven.google.com' }
        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}

方案2:排查并排除低版本依赖

如果不想全局强制版本,先通过命令查看依赖树,定位引入低版本kotlin-stdlib-jdk7的具体依赖:

./gradlew app:dependencies

找到对应依赖后,在该依赖声明中排除kotlin-stdlib-jdk7,示例如下:

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

这种方式需要逐个排查依赖,相对繁琐,推荐优先使用方案1。

最后执行构建

修改配置后,重新执行清理和构建命令:

./gradlew clean build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 13:40:42