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,提问作者Никита Ломейко
相关产品推荐
相关产品推荐

