今天打开一个先前的react-native项目,yarn run android后出现如下错误:
yarn run v1.22.4
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1466 file(s) to forward-jetify. Using 8 workers...
info JS server already running.
info Installing the app...
> Task :react-native-community_art:compileDebugJavaWithJavac FAILED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.2/userguide/command_line_interface.html#sec:command_line_warnings
12 actionable tasks: 2 executed, 10 up-to-date
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':react-native-community_art:compileDebugJavaWithJavac'.
> Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation.
* 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 3s
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
找了一圈,后来在stockover flow找到了正解:https://stackoverflow.com/questions/64968851/could-not-find-tools-jar-please-check-that-library-internet-plug-ins-javaapple
1.在终端输入
/usr/libexec/java_home -V | grep jdk
会得到以下结果:
Matching Java Virtual Machines (1):
1.8.0_272 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8"
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
(最后一行的路径可能因人而异,把它复制一下,接下来要用)
2.编辑.zshrc
vim ~/.zshrc
增加如下两行:
export JAVA_HOME=(这里是贴上面“/Livrary/Java/.../Home”,每个人的路径未必一样)
export PATH=$JAVA_HOME/bin:$PATH
保存退出:
esc :wq
3.让修改生效
source ~/.zshrc