如何在Android Studio的Gradle中获取JDK路径?

如何在Android Studio的Gradle中获取JDK路径?

问题描述:

要构建一些Android项目,必须设置环境变量 JAVA_HOME .(请参阅此Stack Exchange问​​题 flutter错误报告.)

In order to build some Android projects, it's necessary to set the environment variable JAVA_HOME. (See this Stack Exchange question and flutter bug report.)

我正在处理的项目就是这种情况.我想更改Gradle文件,因此不必设置 JAVA_HOME .

This is the case for a project I'm working on. I would like to change the Gradle file so it is not necessary to set JAVA_HOME.

Gradle是否有办法获取封闭的Android Studio进程使用的JDK的路径(或使该进程在无需用户干预的情况下通过JDK传递)?尚未设置code> JAVA_HOME .

Is there any way for Gradle to get the path to the JDK used by the enclosing Android Studio process (or for the process to pass in the JDK without user intervention)? This should work when JAVA_HOME had not been set.

您可以在 your_project_path/app/build.gradle

task javaHome {
    println "JAVA_HOME:" + System.getProperty("java.home")
}

运行任务:

./gradlew -q app:javaHome

结果:

JAVA_HOME:/usr/lib/jvm/java-8-openjdk-amd64/jre