在 Mac 设备的终端执行命令 ./gradlew :xxx:clean :xxx:assembleRelease
编译 xxx.aar 时,报错:
> Task :xxx:compileReleaseJavaWithJavac FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':xxx:compileReleaseJavaWithJavac'.
> 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
解决方案:
第一步:查看 java 版本,执行命令:java -version
第二步:查看 javac 版本,执行命令:javac -version
第三步:如果 java 和 javac 版本不一致,或者需要升级 java, javac 版本,在终端执行命令 /usr/libexec/java_home -V | grep jdk
,获取 java 虚拟机列表
/usr/libexec/java_home -V | grep jdk
Matching Java Virtual Machines (2):
1.8.321.07 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_321 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
第四步:移除所有 java 虚拟机
rm -rf /Library/Internet Plug-Ins/JavaAppletPlugin.plugin
rm -rf /Library/Java/JavaVirtualMachines
第五步:下载 JDK(Java SE Developent Kit) 并安装,下载地址:https://www.oracle.com/java/technologies/downloads/#java8-mac
第六步:在 ~/.zshrc
或者 ~/.bash_profile
文件里配置环境变量 JAVA_HOME,此处以 ~/.zshrc
为例,在终端执行命令 vi ~/.zshrc
,配置环境变量
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
温馨提示:
- 不要使用路径 Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home,使用该路径会报错 “Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation.”
- 不配置环境变量 JAVA_HOME,也会报错 “Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation.”
第七步:执行命令 source ~/.zshrc
后,重启下终端
第八步:再次执行编译 xxx.aar 的命令:./gradlew :xxx:clean :xxx:assembleRelease
,成功打包出 .aar