androidandroid-studiogradleintellij-idea

How do I set GRADLE_LOCAL_JAVA_HOME?


Whenever I open an existing Android project in Android Studio for the first time, Android Studio sets the Gradle JDK to GRADLE_LOCAL_JAVA_HOME which points to the Android Studio's built-in JDK. I would like GRADLE_LOCAL_JAVA_HOME to point to a different JDK location (the same that I use in my JAVA_HOME and the one I have in PATH) but I can't figure out how to do it.

It's annoying that whenever I open a project I get a wrong JDK by default and I must open the settings and choose the one under JAVA_HOME manually. I would like Android Studio to pick up my custom default automatically instead.

Android Studio Gradle JDK dropdown Android Studio GRADLE_LOCAL_JAVA_HOME dropdown

This documentation says that:

GRADLE_LOCAL_JAVA_HOME: uses the java.home property in the .gradle/config.properties file which defaults to the JetBrains Runtime.

I hoped that I could change it globally somewhere and tried creating $HOME/.gradle/config.properties and putting java.home there but it doesn't work. Does it mean Android Studio respects only the config.properties file in the project's .gradle directory?


Solution

  • It looks like Android Studio only uses the project's .gradle/config.properties.

    You can attest this by deleting the java.home property from the project's config.properties file and selecting the GRADLE_LOCAL_JAVA_HOME macro on Android Studio. Once you try to build the project you will encounter the following error:

    Invalid Gradle JDK configuration found. Undefined java.home on the project gradle/config.properties file when using the gradleJvm #GRADLE_LOCAL_JAVA_HOME macro.

    This error occurs even when we define java.home property on our home's .gradle/config.properties.

    This Android documentation says:

    Note: In most cases, we recommend using GRADLE_LOCAL_JAVA_HOME, which is the default for newly created projects. This lets you define a project-specific JDK without opening the project first.

    As said above "[...] define a project-specific JDK [...]". That is why we cannot override it.

    You can read a little bit more about this macro here.