In Android Studio Ladybug 2024.2.1 or IntelliJ IDEA, this error can happen even if you have Java 21 installed and enabled by default. For example, you could set your $JAVA_HOME environment variable to use the JDK that comes from Android Studio, using this guide Using JDK that is bundled inside Android Studio as JAVA_HOME on Mac :
# For ~/.bash_profile or ~/.zshrc
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
But for some reason, an Android project that declares that it needs Java 17 in a build.gradle file cannot be compiled with Java 21.
java {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlin {
jvmToolchain(17)
}
You'll see an error like this when you try to build the app:
org.gradle.jvm.toolchain.internal.NoToolchainAvailableException:
No matching toolchains found for requested specification:
{languageVersion=17, vendor=any, implementation=vendor-specific} for MAC_OS on aarch64.
Could not determine the dependencies of task ':app:compileKotlin'.
> No locally installed toolchains match and toolchain download repositories have not been configured.
The solution is to download a specific Java 17 JDK/SDK manually, and make your project use it: