Gradle ignores JAVA_HOME and starts a different JDK
Android 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.
Fix it fast
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 java2Match what you see, then jump to its fix
- 1
The 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
The 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
echo "$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
- 1
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.properties2Fix
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-amd643Verify
Restart the daemon and print the version again.
./gradlew --stop
./gradlew --versionConfirmed by docs.gradle.org
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.xml2Fix
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_HOME3Verify
Sync the project; the run button now matches the terminal.
./gradlew helpConfirmed by developer.android.com
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 --versionConfirmed by developer.android.com
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
Android Studio or its Gradle build runs out of Java heap memory
Android Studio shows a notification about recommended memory settings
Two separate Java processes can run out of heap: the IDE itself and the Gradle daemon that builds your app.
3 causes, each with a fixMobile 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 fixMobile apps
flutter run hangs for minutes on Running Gradle task 'assembleDebug'
Downloading https://services.gradle.org/distributions/gradle-8.14-all.zip
That line only says Gradle started; the wait is inside Gradle.
3 causes, each with a fixMobile apps
flutter doctor cannot find the Java that comes with Android Studio
Unable to find bundled Java version.
Flutter looks for the Java inside Android Studio first, then JAVA_HOME, then any java on PATH.
3 causes, each with a fixMobile apps
Flutter Android build fails with Unsupported class file major version
Unsupported class file major version 65
The number is the Java version that tried to read a file: 61 is Java 17, 65 is Java 21.
3 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fix