Kotlin and Java compile to different JVM targets in a React Native Android build
Task :react-native-gradle-plugin:compileKotlin FAILEDShort answer
Gradle refuses to build when the Java task and the Kotlin task target different JVM versions. Kotlin falls back to 1.8 when you set nothing, while Java follows the JDK running Gradle. Set one JDK toolchain so both tasks use the same version, and run Gradle on the JDK React Native recommends, which is 17.
Fix it fast
1Find out which cause you have
Find out which JDK Gradle itself is running on. The mismatch always starts from that number.
cd android ./gradlew --version echo $JAVA_HOME2Match what you see, then jump to its fix
- 1
One task reports target 1.8 and the other reports your JDK versionNothing is set, so Kotlin defaults to 1.8 and Java follows the JDKFix - 2
Both numbers are explicit in your gradle files, for example Java 8 and Kotlin 11You set different values for Java and KotlinFix - 3
./gradlew --version shows a JDK newer than 17Gradle runs on a JDK newer than React Native supportsFix
- 1
What does the error look like?
Task :react-native-gradle-plugin:compileKotlin FAILED'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11)
Cause 1 of 3
Nothing is set, so Kotlin defaults to 1.8 and Java follows the JDK
1Check
With no jvmTarget in the build script, Kotlin uses 1.8. The Java task uses the version of the JDK Gradle runs on. On any JDK other than 8 the two differ, and since Gradle 8.0 the plugin fails the build instead of warning.
./gradlew --version
grep -rn "jvmTarget\|sourceCompatibility\|targetCompatibility" build.gradle app/build.gradle2Fix
Declare one toolchain in the module that fails. It sets the Kotlin jvmTarget to the toolchain version when you have not set it yourself.
// build.gradle.kts of the failing module
kotlin {
jvmToolchain(17)
}3Verify
Run the build again.
./gradlew assembleDebugConfirmed by kotlinlang.org
Cause 2 of 3
You set different values for Java and Kotlin
1Check
The check pairs compileKotlin with compileJava. If jvmTarget and targetCompatibility are set to different values anywhere in the build, it fails.
grep -rn -E "jvmTarget|JavaVersion|targetCompatibility" --include=*.gradle --include=*.kts .2Fix
Remove the separate values and use the single toolchain from the previous cause. Gradle 8.0.2 and later may also need the foojay toolchain resolver plugin so it can download the JDK.
// settings.gradle.kts
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("1.0.0")
}3Verify
Run the build again.
./gradlew assembleDebugConfirmed by kotlinlang.org
Cause 3 of 3
Gradle runs on a JDK newer than React Native supports
1Check
React Native documents JDK 17 and warns that higher versions may cause problems. See which JDK your shell and Android Studio use.
java -version
/usr/libexec/java_home -V2Fix
Point JAVA_HOME at JDK 17 (the React Native docs install Zulu 17 on macOS) and stop the old Gradle daemons.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
cd android && ./gradlew --stop3Verify
Confirm Gradle now reports JDK 17.
./gradlew --versionConfirmed by reactnative.dev
How do I stop it coming back?
- Pin one JDK per project (a toolchain) instead of relying on whatever JAVA_HOME the machine has.
- Do not set kotlin.jvm.target.validation.mode to warning or ignore to silence this; the Kotlin docs say that only hides the mismatch.
- Run ./gradlew --version in CI so the JDK is printed in every log.
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Mobile apps
Gradle fails because it does not support the Java version you run
Could not determine java version from '21.0.1'.
Gradle can only run on JDK versions it knows.
3 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 fixMobile apps
Java and Kotlin compile to different JVM targets in an Android module
Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)
Java and Kotlin tasks in the same module are compiling for different JVM targets, for example 1.8 and 17.
2 causes, each with a fix