Skip to content

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:

kotlin
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:

kotlin
java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions {
        jvmTarget = "11"
    }
}

The modern approach uses the jvmToolchain API, which automatically configures both Java and Kotlin compilation:

kotlin
kotlin {
    jvmToolchain(11)
}

Or with more detailed configuration:

kotlin
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:

kotlin
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:

kotlin
// 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"
    }
}
kotlin
// 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 8
  • JavaVersion.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:

properties
# 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:

  1. Go to File → Project Structure → SDK Location → JDK location
  2. Note the JDK version
  3. Ensure your build.gradle.kts matches this version

Troubleshooting

If you continue to experience issues after applying these solutions:

  1. Clean and rebuild: ./gradlew clean build
  2. Invalidate caches: File → Invalidate Caches / Restart
  3. Check module consistency: Ensure all modules have compatible configurations
  4. 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 VersionKotlin jvmTargetJavaVersion 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:

  1. Use the modern jvmToolchain API where possible
  2. Ensure consistency across all modules in multi-module projects
  3. Match your Gradle configuration with your Android Studio JDK version
  4. 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.