./gradlew 无法完成操作。无法找到 Java 运行时

./gradlew The operation couldn’t be completed. Unable to locate a Java Runtime

当我从项目文件夹中 运行 ./gradlew 时,我得到:

./gradlew
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

同java:

which java
/usr/bin/java

/usr/bin/java
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

我在 Android studio 安装了 JDK,好像安装在 ~/Library/Android/sdk/sources/android-28

设置 export JAVA_HOME=/Users/mrgloom/Library/Android/sdk/sources/android-28/export JAVA_HOME=/Users/mrgloom/Library/Android/sdk 产生如下错误:

ERROR: JAVA_HOME is set to an invalid directory: /Users/mrgloom/Library/Android/sdk/sources/android-28/

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

如何为 gradlew 指定 java 路径?

使用 Android Studio 附带的 JDK 和设置 export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home 帮助了我。