Skip to content
Latchkey

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.

gradle output
> Task :app:compileJava FAILED
error: invalid source release: 21
# or, with --release:
error: release version 21 not supported

Common 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.

build.gradle.kts
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

Provision the matching JDK in CI

Ensure a JDK 21 is available for the toolchain to resolve.

.github/workflows/ci.yml
- 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 languageVersion aligned with an available JDK.

Frequently asked questions

What causes ""invalid source 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.
How do I fix "invalid source release"?
Let Gradle select (and download) a JDK that matches your target, independent of the JDK running Gradle.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card