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

React Native安卓构建报错:checkDebugDuplicateClasses失败(Kotlin类重复)

解决React Native安卓构建checkDebugDuplicateClasses冲突问题

问题根源

项目依赖中同时存在org.jetbrains.kotlin:kotlin-stdlib:1.8.0和org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21两个版本的Kotlin标准库,导致类重复冲突。高版本的kotlin-stdlib已经整合了jdk7扩展模块的内容,无需单独引入旧版本的stdlib-jdk7。

解决方案

1. 统一项目Kotlin版本

在项目根目录的android/build.gradle的buildscript.ext中添加统一的Kotlin版本配置,确保所有依赖使用同一版本:

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 31
        googlePlayServicesAuthVersion = "19.2.0"
        kotlinVersion = "1.8.0" // 添加统一Kotlin版本
    }
    repositories {
        google()
        mavenCentral() // 替换已停止维护的jcenter
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.1.0")
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" // 添加Kotlin Gradle插件
    }
}

2. 强制依赖统一Kotlin版本

在app模块的android/app/build.gradle中添加依赖解析策略,强制所有依赖使用指定版本的Kotlin标准库:

android {
    // ... 原有配置
}

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

dependencies {
    // ... 原有依赖
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    // 移除所有显式的kotlin-stdlib-jdk7/jdk8依赖,高版本stdlib已包含这些内容
}

3. 清理构建缓存

执行以下命令清理缓存后重新构建:

cd android
./gradlew clean
./gradlew assembleDebug

额外说明

如果升级Kotlin版本后仍有问题,需检查gradle版本与Kotlin版本的兼容性:

  • Gradle 4.1.0最高兼容Kotlin 1.5.x版本,若要使用Kotlin 1.8.0,需升级Gradle到7.0以上,同时将Android Gradle Plugin升级到对应版本(比如com.android.tools.build:gradle:7.0.4)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:35:30