Gradle "invalid source release" / "release version not supported"
The Java compiler Gradle invoked is older than the language level you asked for. A JDK 17 cannot compile sourceCompatibility = 21 - it does not support that release.
What this error means
Compilation fails with invalid source release: 21 or error: release version 21 not supported. The number is what your build requests; the compiling JDK is older than it.
> Task :app:compileJava FAILED
error: invalid source release: 21
# or, with --release:
error: release version 21 not supportedCommon causes
Compile JDK older than the requested release
A sourceCompatibility/targetCompatibility or release set to 21 needs a JDK 21 compiler. The JDK Gradle is using is older, so the language level is unsupported.
No Java toolchain pinned
Without a java.toolchain block, Gradle compiles with whatever JDK runs Gradle. If that is older than your target, the source release is invalid.
How to fix it
Pin a Java toolchain
Let Gradle select (and download) a JDK that matches your target, independent of the JDK running Gradle.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}Provision the matching JDK in CI
Ensure a JDK 21 is available for the toolchain to resolve.
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'How to prevent it
- Use a Java toolchain so the compile JDK is explicit and reproducible.
- Provision the toolchain JDK in CI with
setup-java. - Keep
languageVersionaligned with an available JDK.