Inconsistent JVM Target Compatibility in Gradle
Problem Statement
When working with Kotlin-based Android projects in Android Studio, developers often encounter the following Gradle sync error:
Execution failed for task ':buildSrc:compileKotlin'.
Inconsistent JVM-target compatibility detected for tasks 'compileJava' (21) and 'compileKotlin' (1.8)
This error occurs when your Java and Kotlin compilation tasks are configured with different JVM target versions. Specifically, it indicates that:
- The
compileJava
task is targeting Java 21 (JVM 21) - The
compileKotlin
task is targeting Java 8 (JVM 1.8)
Java and Kotlin compilation targets must be aligned because Java bytecode generated for different versions may not be compatible. This incompatibility prevents Gradle from compiling your code successfully.
Solutions
1. Standard Configuration in Gradle Script
Add these configurations to your app-level build.gradle.kts
or build.gradle
file to synchronize JVM targets:
// In app-level build.gradle.kts for Kotlin DSL
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}
// For pure Kotlin projects (without Android)
kotlin {
compilerOptions {
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11)
}
}
// In app-level build.gradle for Groovy DSL
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}
Explanation:
These configurations explicitly set both Java and Kotlin compilation to target Java 11. The versions must match exactly (11
= 11
, not 11
and 1.11
).
2. Using JDK 21 (Latest Version)
Configure Gradle to use Java 21 consistently throughout:
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
kotlinOptions {
jvmTarget = "21"
}
}
3. Fixing buildSrc Configuration
For projects using a buildSrc
directory, add a JVM toolchain setting:
// In buildSrc/build.gradle.kts
kotlin {
jvmToolchain(17) // Set to your desired Java version
}
TIP
Projects using buildSrc
often cause this error because the module inherits JDK settings differently from main modules. Always check these files first if the error references :buildSrc:compileKotlin
.
4. Configuring Kotlin Multiplatform Projects
For Kotlin Multiplatform (KMP) projects, configure JVM targets explicitly:
kotlin {
androidTarget {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}
jvm("desktop") {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}
}
5. Verify Android Studio JDK Settings
Update Java versions in Android Studio settings:
- Open File > Settings (Windows/Linux) or Android Studio > Preferences (macOS)
- Navigate to Build, Execution, Deployment > Build Tools > Gradle
- Check Gradle JDK and select a compatible Java version
- Delete
.idea
directory and restart Android Studio to reset caches
6. Resolve Plugin Version Conflicts
When using multiple modules, ensure consistent Kotlin plugin versions:
// In project-level build.gradle
buildscript {
ext {
kotlinVersion = "1.9.22" // Consistent across all modules
}
}
Check plugin version compatibility:
// Module build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
}
WARNING
Plugin version mismatches like "Plugin version (1.9.22) is not the same as library version (1.8.0)" cause target incompatibility. Verify $kotlinVersion
resolves identically everywhere.
Recommended Practices
Java Toolchain Alignment: Use the Java toolchain specification instead of per-task configuration:
kotlinkotlin { jvmToolchain(17) }
Update Dependencies: Use recent Kotlin and Gradle versions:
kotlinplugins { kotlin("jvm") version "1.9.22" }
Project-wide Consistency: Define a version constant in your project's
gradle.properties
:JAVA_VERSION = 17 KOTLIN_VERSION = 1.9.22
Conclusion
The "inconsistent JVM-target compatibility" error consistently stems from mismatches between Java and Kotlin compilation targets. Key strategies include configuring explicit versions in build files, resolving buildSrc
configurations, ensuring plugin consistency, and verifying IDE settings. For new projects, target Java 17 or 21 to maximize long-term compatibility and feature access.