./gradlew build:Could not find tools.jar. 解决方法

在Android Studio中执行./gradlew build命令时报以下错误:

* What went wrong:
Execution failed for task ':ble_printer: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.

解决方法(在终端Terminal中执行以下命令):

先通过如下命令查看JDK位置

/usr/libexec/java_home -V

(升级OSX11.0.1后,执行这个命令可能看到两个路径,一个是JDK的实际路径,
另一个是:/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home,(网上有资料说这个是JRE的路径)。

jdk路径.png

方法一:修改环境变量为jdk所在路径

打开 .bash_profile 配置文件:

open -e ~/.bash_profile

添加JDK路径:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/
export PATH=${JAVA_HOME}:${PATH}

输入以下命令保存刷新:

source ~/.bash_profile

解决方法二:手动复制 tools.jar

sudo cp /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/lib/tools.jar /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib

注:根据自己的实际JDK路径修改上面的JDK路径

重新启动会话重新执行gradlew 命令即可。

你可能感兴趣的:(./gradlew build:Could not find tools.jar. 解决方法)