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

React Native打包AAB遇Kotlin版本不兼容错误,寻求解决方法

解决React Native编译AAB时Kotlin版本不兼容问题

问题根源

你的错误显示Gradle自带的Kotlin标准库(1.8.0-RC2)和项目期望的1.6.0版本冲突,同时你使用的AGP是测试版(8.0.0-alpha11),和Gradle、Kotlin版本组合不匹配,再加上react-native-gradle-plugin的依赖特性,导致了批量编译错误。

修复步骤

1. 统一Gradle、AGP和Kotlin版本

Gradle 8.x需搭配稳定版AGP,Kotlin版本也要和AGP兼容。AGP 8.0稳定版推荐搭配Kotlin 1.8.20+,同时把Gradle换成正式版(别用RC测试版):

  • 修改根目录build.gradle的buildscript部分:
buildscript {
    ext.kotlin_version = '1.8.20'
    ext {
        buildToolsVersion = "33.0.2" // 升级到最新稳定版,避免旧版本bug
        minSdkVersion = 21
        compileSdkVersion = 33 // 和targetSdkVersion保持一致
        targetSdkVersion = 33
        ndkVersion = System.properties['os.arch'] == "aarch64" ? "24.0.8215888" : "21.4.7075529"
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.0.2") // 换成稳定版AGP
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("de.undercouch:gradle-download-task:5.0.1")
    }
}
  • 打开gradle/wrapper/gradle-wrapper.properties,修改distributionUrl为正式版:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

2. 强制所有模块用统一Kotlin版本

在根目录build.gradle的allprojects块里添加依赖强制规则,避免不同模块引入不同版本的Kotlin库:

allprojects {
    repositories {
        // 保留你原有的仓库配置
        maven { url("$rootDir/../node_modules/react-native/android") }
        maven { url("$rootDir/../node_modules/jsc-android/dist") }
        mavenCentral {
            content { excludeGroup "com.facebook.react" }
        }
        google()
        maven { url 'https://www.jitpack.io' }
    }

    // 新增这段强制依赖规则
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
        }
    }
}

3. 清理缓存重新同步

  • 执行命令:Windows用gradlew clean,Mac/Linux用./gradlew clean
  • 删除项目根目录下的.gradle文件夹,以及用户目录(比如C:\Users\USERNAME\.gradle)里的缓存
  • 打开Android Studio,点击「File -> Sync Project with Gradle Files」重新同步

4. 检查React Native版本适配

确保你的RN版本和AGP、Gradle匹配:RN 0.71+用AGP 7.4+,RN 0.72+用AGP 8.0+。如果RN版本太低,得降级AGP和Kotlin到对应兼容版本。

验证

重新执行./gradlew bundleRelease(Windows用gradlew bundleRelease)打包AAB,要是没再出现Kotlin版本错误,就说明问题解决了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 02:55:18