JVM Target Compatibility in Gradle Kotlin DSL
Problem Statement
When working with Gradle projects that combine both Java and Kotlin code, you may encounter a common compatibility error:
ERROR: 'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
This error occurs when the Java compilation target and Kotlin compilation target are configured to different Java versions. The issue is particularly common when:
- Upgrading Gradle versions (especially to 8.0+)
- Using Android Studio with newer JDK versions
- Working with multi-module projects
- Using kapt (Kotlin Annotation Processing Tool)
Understanding the Error
The JVM target compatibility error arises because Gradle requires consistency between Java and Kotlin compilation targets. When these targets mismatch, it can lead to:
- Incompatible bytecode generation
- Runtime compatibility issues
- Problems with annotation processing
- Build failures during release builds
Solutions
Solution 1: Standard Configuration for Android Projects
For Android projects, configure both Java and Kotlin compilation targets in your module's build.gradle.kts
:
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}
WARNING
Always check your Android Studio JDK version (File → Project Structure → SDK Location → JDK location) and ensure your Gradle configuration matches this version.
Solution 2: Standard Configuration for Non-Android Java/Kotlin Projects
For standard Java/Kotlin projects (without Android plugin), use:
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
}
}
Solution 3: Using jvmToolchain (Recommended for Kotlin 1.6.20+)
The modern approach uses the jvmToolchain
API, which automatically configures both Java and Kotlin compilation:
kotlin {
jvmToolchain(11)
}
Or with more detailed configuration:
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of("11"))
}
}
TIP
The jvmToolchain
approach is recommended for new projects as it ensures consistency across all compilation tasks and handles JDK version management automatically.
Solution 4: Handling kapt Compatibility Issues
If you're using kapt (Kotlin Annotation Processing) and encounter compatibility issues, you may need to explicitly configure the kapt task:
import org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs
tasks.withType<KaptGenerateStubs>().configureEach {
kotlinOptions.jvmTarget = "11"
}
Solution 5: Multi-module Project Configuration
For projects with multiple modules, ensure consistency across all modules:
// Configure for all subprojects
subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "11"
}
}
// Individual module can override if needed
kotlin {
jvmToolchain(11)
}
Common Pitfalls and Solutions
Pitfall 1: Version Number Format Confusion
DANGER
Using incorrect version formats is a common mistake:
JavaVersion.VERSION_1_8
= Java 8JavaVersion.VERSION_11
= Java 11"1.8"
= Java 8 (as string)"11"
= Java 11 (as string)
Pitfall 2: Gradle Version Compatibility
Certain Gradle versions may have compatibility constraints:
# gradle-wrapper.properties
# For Java 11 compatibility, consider these Gradle versions:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
# or
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip
Pitfall 3: Android Studio JDK Mismatch
Verify your Android Studio JDK version matches your Gradle configuration:
- Go to File → Project Structure → SDK Location → JDK location
- Note the JDK version
- Ensure your
build.gradle.kts
matches this version
Troubleshooting
If you continue to experience issues after applying these solutions:
- Clean and rebuild:
./gradlew clean build
- Invalidate caches: File → Invalidate Caches / Restart
- Check module consistency: Ensure all modules have compatible configurations
- Verify plugin versions: Ensure Kotlin and Android Gradle plugin versions are compatible
WARNING
Avoid using kotlin.jvm.target.validation.mode = IGNORE
in gradle.properties
as a permanent solution. This suppresses the error but doesn't fix the underlying compatibility issue.
Version Compatibility Reference
Java Version | Kotlin jvmTarget | JavaVersion Constant |
---|---|---|
Java 8 | "1.8" | JavaVersion.VERSION_1_8 |
Java 11 | "11" | JavaVersion.VERSION_11 |
Java 17 | "17" | JavaVersion.VERSION_17 |
Java 18 | "18" | JavaVersion.VERSION_18 |
Java 19 | "19" | JavaVersion.VERSION_19 |
Conclusion
Maintaining consistent JVM target compatibility between Java and Kotlin compilation tasks is essential for successful builds. The recommended approach is:
- Use the modern
jvmToolchain
API where possible - Ensure consistency across all modules in multi-module projects
- Match your Gradle configuration with your Android Studio JDK version
- Avoid suppressing validation warnings without addressing the root cause
By following these practices, you can eliminate JVM target compatibility errors and ensure your project builds successfully across different environments.