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

React Native编译报错:':gradle-plugin:compileKotlin'任务执行失败求助

解决':gradle-plugin:compileKotlin'编译失败问题

核心原因

JDK 21与当前React Native版本、Gradle及Kotlin版本兼容性不匹配,React Native对应compileSdk 33的版本尚未完全支持JDK 21,且未指定版本的Gradle插件和React Native Gradle插件易引发版本冲突。

具体修复步骤

1. 切换兼容的JDK版本

使用React Native官方明确支持的JDK 11或JDK 17:

  • Android Studio中打开File > Project Structure > SDK Location,修改JDK location为对应JDK路径
  • 或设置系统环境变量JAVA_HOME指向JDK 11/17目录

2. 明确指定插件版本

修改项目根目录build.gradle,为依赖添加明确版本号(以React Native 0.71.x为例,适配compileSdk 33):

ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "23.1.7779620"
        kotlinVersion = "1.8.0"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.3.1")
        classpath("com.facebook.react:react-native-gradle-plugin:0.71.14")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${ext.kotlinVersion}")
    }
}

3. 修正配置文件拼写错误

你提供的settings.gredle存在拼写错误,应改为settings.gradle,修正后内容:

rootProject.name = 'MyReactNativeProject'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle");
applyNativeModulesSettingsGradle(settings)
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')

4. 清理缓存重新编译

执行命令清除缓存并重建项目:

./gradlew clean
rm -rf node_modules package-lock.json yarn.lock
npm install
./gradlew assembleDebug

验证要点

  • 执行java -version确认JDK版本为11或17
  • 检查gradle/wrapper/gradle-wrapper.properties中,distributionUrl需与AGP版本匹配(AGP 7.3.1对应Gradle 7.5)

内容的提问来源于stack exchange,提问作者Никита Ломейко

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 01:33:21