Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Gradle ignores JAVA_HOME and starts a different JDK

ErrorAndroid Gradle plugin requires Java 17 to run. You are currently using Java 11.

Short answer

Gradle does not read JAVA_HOME first. A org.gradle.java.home line in gradle.properties wins over it, the IDE run buttons use the JDK from the IDE settings, and a shell that never exported JAVA_HOME falls back to the java on PATH. Run ./gradlew --version to see which JDK really starts.

  • Applies to Gradle 8.x and 9.x, Android Gradle plugin 8.x, Ubuntu 24.04, macOS, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the wrapper which JVM it launches, then compare it with what your shell exports.

    ./gradlew --version
    echo "$JAVA_HOME"
    which java
  2. 2Match what you see, then jump to its fix

    1. 1The JDK shown by ./gradlew --version differs from JAVA_HOME and you or a teammate once set a path in gradle.propertiesorg.gradle.java.home in a gradle.properties file overrides JAVA_HOMEFix
    2. 2The terminal build works, but the green run button or a sync fails with the wrong Java versionThe IDE run button uses the JDK from the IDE settings, not JAVA_HOMEFix
    3. 3echo "$JAVA_HOME" prints an empty line, or a CI step shows a different value than your terminalJAVA_HOME is not exported in the shell that runs GradleFix

What does the error look like?

  • Android Gradle plugin requires Java 17 to run. You are currently using Java 11.

Cause 1 of 3

org.gradle.java.home in a gradle.properties file overrides JAVA_HOME

1Check

A Gradle property outranks an environment variable. Look in the project file and in the user-level file.

grep -n "org.gradle.java.home" gradle.properties ~/.gradle/gradle.properties

2Fix

Delete the line, or point it at the JDK you want. Without the property Gradle derives the JDK from JAVA_HOME or from the java it finds.

# gradle.properties
org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64

3Verify

Restart the daemon and print the version again.

./gradlew --stop
./gradlew --version

Confirmed by docs.gradle.org

Did this fix it?

Cause 2 of 3

The IDE run button uses the JDK from the IDE settings, not JAVA_HOME

1Check

Studio runs Gradle with the JDK chosen under the Gradle settings. Only a plain terminal command follows JAVA_HOME. The choice is saved as gradleJvm in the project.

grep -n gradleJvm .idea/gradle.xml

2Fix

Open Settings, Build, Execution, Deployment, Build Tools, Gradle, and set Gradle JDK to the JAVA_HOME macro or to the same JDK your terminal uses.

# Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK > JAVA_HOME

3Verify

Sync the project; the run button now matches the terminal.

./gradlew help

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

JAVA_HOME is not exported in the shell that runs Gradle

1Check

If JAVA_HOME is not set, Gradle falls back to the java command on PATH. A variable set without export, or set only in an interactive profile, never reaches a script.

env | grep JAVA_HOME
readlink -f "$(which java)"

2Fix

Export it in the profile the shell actually loads, then open a new shell.

# ~/.profile or ~/.zshenv
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"

3Verify

A fresh shell reports the JDK you expect.

echo "$JAVA_HOME"
./gradlew --version

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Keep one source of truth: JAVA_HOME in the shell and the same JDK in the IDE Gradle settings.
  • Avoid committing a machine-specific org.gradle.java.home path to gradle.properties.
  • Print ./gradlew --version in CI logs so a wrong JDK is visible at the top of every run.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Flutter's Android build stops: JAVA_HOME is not set

ERROR: JAVA_HOME is set to an invalid directory

The Gradle wrapper script needs a Java runtime and looks in two places: the JAVA_HOME variable, then a java command on PATH.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.