React Native启动报错::app:compileDebugKotlin任务执行失败求助
React Native Android端Kotlin编译失败问题排查方案
问题描述
启动React Native应用时持续报错,错误信息如下:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org
Android目录下的build.gradle文件内容:
buildscript { ext { buildToolsVersion = "34.0.0" minSdkVersion = 21 compileSdkVersion = 34 targetSdkVersion = 34 ndkVersion = "25.1.8937393" kotlinVersion = "1.9.22" } repositories { google() mavenCentral() maven { url 'https://jitpack.io' } } dependencies { classpath("com.android.tools.build:gradle:8.1") classpath("com.facebook.react:react-native-gradle-plugin") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22") } } apply plugin: "com.facebook.react.rootproject"
排查与解决方案
获取详细错误日志
当前日志仅提示编译失败,未给出具体原因。执行以下命令获取完整报错信息:./gradlew assembleDebug --stacktrace或使用
--info参数获取更详细的编译过程日志,根据日志定位具体的代码错误或依赖问题。验证Kotlin与Gradle插件版本兼容性
当前使用的Gradle插件版本为8.1,Kotlin版本为1.9.22,二者理论上兼容,但若问题持续,可尝试:- 将Kotlin版本降级至1.8.20(Gradle 8.0+官方推荐的稳定版本)
- 或升级Gradle插件至8.2.0+,保持版本匹配性
清理缓存并重新构建
缓存损坏可能导致编译异常,执行以下命令清理各类缓存后重试:./gradlew clean rm -rf node_modules && npm install rm -rf android/.gradle android/build android/app/build清理完成后重新运行
npx react-native run-android检查项目中的Kotlin代码
若项目包含自定义Kotlin模块或第三方库引入的Kotlin代码,可能存在语法错误、API兼容问题(如使用已废弃的Kotlin语法/API)。根据详细日志定位到具体文件,修复代码问题。排查依赖冲突
执行./gradlew app:dependencies查看项目依赖树,检查是否存在多版本Kotlin相关依赖(如kotlin-stdlib、kotlin-reflect)冲突。若有冲突,需在app/build.gradle中统一指定版本或排除冲突依赖。
内容的提问来源于stack exchange,提问作者Kingsley
相关产品推荐
相关产品推荐

