Gradle fails because it does not support the Java version you run
Could not determine java version from '11.0.2'.Short answer
Gradle can only run on JDK versions it knows. Each Gradle release supports a range: Java 17 from Gradle 7.3, Java 21 from 8.5, Java 24 from 8.14, Java 25 from 9.1. Either run Gradle on a JDK in range, or raise the wrapper to a version that supports your JDK.
Fix it fast
1Find out which cause you have
Compare the two versions side by side: the JDK in the shell and the Gradle in the wrapper.
java -version ./gradlew --version2Match what you see, then jump to its fix
- 1
Could not determine java version from '21...' on a project with an old distributionUrlThe wrapper's Gradle is older than your JDKFix - 2
Works in Android Studio but fails in the terminalThe shell uses a newer JDK than the project supportsFix - 3
The newest Java is not yet supported to run GradleYou want the newest Java for compiling, not for running GradleFix
- 1
What does the error look like?
Could not determine java version from '11.0.2'.Could not determine java version from '21.0.1'.Unsupported class file major version 65
Cause 1 of 3
The wrapper's Gradle is older than your JDK
1Check
Gradle's compatibility table: Java 17 needs Gradle 7.3 or later, Java 21 needs 8.5, Java 24 needs 8.14 and Java 25 needs 9.1.0.
grep distributionUrl gradle/wrapper/gradle-wrapper.properties2Fix
Raise the wrapper to a version that supports your JDK. Run it twice so the JAR updates too.
./gradlew :wrapper --gradle-version 8.14.3
./gradlew :wrapper3Verify
The version output works on your JDK.
./gradlew --versionConfirmed by docs.gradle.org
Cause 2 of 3
The shell uses a newer JDK than the project supports
1Check
Android Studio runs Gradle on the JDK in its settings. A terminal uses JAVA_HOME, or java on PATH, which may be newer.
echo $JAVA_HOME
java -version2Fix
Point JAVA_HOME at a JDK within the supported range. Android's documentation recommends setting JAVA_HOME and the IDE's Gradle JDK to the same JDK.
export JAVA_HOME=/path/to/jdk-173Verify
The terminal now reports the same JDK as the IDE.
./gradlew --versionConfirmed by developer.android.com
Cause 3 of 3
You want the newest Java for compiling, not for running Gradle
1Check
Gradle's documentation says a newest Java version may be supported for compilation or testing only. Toolchains let compilation use a different JDK.
grep -rn "toolchain" --include=build.gradle* .2Fix
Run Gradle on a supported JDK and declare the Java version for compilation as a toolchain.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}3Verify
Build with Gradle on the supported JDK.
./gradlew buildConfirmed by developer.android.com
How do I stop it coming back?
- Pin JAVA_HOME and the IDE's Gradle JDK to the same JDK, and write it in the README.
- Check Gradle's compatibility table before installing a new JDK.
- Set a Java toolchain so results do not depend on which JDK started Gradle.
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
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix