运行npx react-native run-android时compileKotlin任务失败求助
React Native Android构建报错:Kotlin找不到JDK工具的解决方案
问题重现
执行npx react-native run-android或npm run android时出现如下错误:
> myApp@0.0.1 android > react-native run-android info Starting JS server... info Installing the app... > Task :react-native-gradle-plugin:compileKotlin FAILED 1 actionable task: 1 executed FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-gradle-plugin:compileKotlin'. > Failed to query the value of task ':react-native-gradle-plugin:compileKotlin' property 'compilerRunner$kotlin_gradle_plugin'. > Kotlin could not find the required JDK tools in the Java installation. Make sure Kotlin compilation is running on a JDK, not JRE. * 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 BUILD FAILED in 10s error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-gradle-plugin:compileKotlin'. > Failed to query the value of task ':react-native-gradle-plugin:compileKotlin' property 'compilerRunner$kotlin_gradle_plugin'. > Kotlin could not find the required JDK tools in the Java installation. Make sure Kotlin compilation is running on a JDK, not JRE. * 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 BUILD FAILED in 10s at makeError (/Users/ayan/Documents/myApp/node_modules/execa/index.js:174:9) at /Users/ayan/Documents/myApp/node_modules/execa/index.js:278:16 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async runOnAllDevices (/Users/ayan/Documents/myApp/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5) at async Command.handleAction (/Users/ayan/Documents/myApp/node_modules/@react-native-community/cli/build/index.js:142:9) info Run CLI with --verbose flag for more details.
问题原因
核心原因是当前构建环境使用的是JRE而非JDK:
- Kotlin编译React Native Gradle插件时,需要JDK提供的开发工具包(如
javac编译器等) - JRE仅包含Java程序的运行环境,缺少编译所需的核心工具类,导致编译任务无法完成
解决方法
1. 检查当前Java环境类型
打开终端执行以下命令:
java -version javac -version
如果javac命令报错或不存在,说明当前使用的是JRE,需要切换到JDK环境。
2. 安装/切换到JDK环境
- 安装JDK:选择适配系统的JDK版本(推荐11或17,与React Native官方要求匹配),完成安装。
- 配置环境变量:
- macOS/Linux:编辑
~/.bashrc或~/.zshrc文件,添加以下内容(替换为你的JDK实际路径):
执行export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATHsource ~/.zshrc(对应你的shell配置文件)使配置生效。 - Windows:打开系统环境变量设置,添加
JAVA_HOME变量,值为JDK安装路径;再将%JAVA_HOME%\bin添加到Path变量中,重启终端生效。
- macOS/Linux:编辑
3. 验证配置并重新构建
重新打开终端,执行javac -version,若能正常显示版本号则配置成功。回到项目目录,重新执行:
npx react-native run-android
内容的提问来源于stack exchange,提问作者ayanonize
相关产品推荐
相关产品推荐

