Skip to content

JVM Target Compatibility Error in Android Studio

Problem Statement

When building Android projects with both Java and Kotlin code, you might encounter the following error:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.

This error occurs when there's a mismatch between the Java versions configured for:

  1. Java compilation (compileDebugJavaWithJavac)
  2. Kotlin compilation (kaptGenerateStubsDebugKotlin)

The build fails because these two tasks must target the same Java version to ensure compatibility.

Solutions

1. Configuration in Module-Level build.gradle

Modify your app-level build.gradle file to ensure Java and Kotlin configurations match:

groovy
android {
    // ... other configurations

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17  // Match this
        targetCompatibility JavaVersion.VERSION_17   // and this
    }
    
    kotlinOptions {
        jvmTarget = "17"  // Must match compatibility versions
    }
}

2. Multi-Module Project Configuration

For projects with multiple modules, add this to your project-level build.gradle:

groovy
subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_17
                    targetCompatibility JavaVersion.VERSION_17
                }
                tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
                    kotlinOptions.jvmTarget = "17"
                }
            }
        }
    }
}

For projects using AGP 8.1.0+ and Kotlin 1.9.0+, enable the toolchain in your project-level build.gradle.kts:

kotlin
subprojects {
    afterEvaluate {
        plugins.withId("org.jetbrains.kotlin.android") {
            kotlin {
                jvmToolchain(17)
            }
        }
    }
}

4. Full Java 17 Configuration

For a complete Java 17 setup:

groovy
android {
    namespace "com.your.package"
    compileSdk 34

    defaultConfig {
        minSdk 24
        targetSdk 34
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlin {
        jvmToolchain(17)
    }

    kotlinOptions {
        jvmTarget = "17"
    }
}

Additional Considerations

  1. Gradle JDK Synchronization Ensure Android Studio's Gradle JDK matches your build configuration:

    • Settings → Build, Execution, Deployment → Build Tools → Gradle
    • Set Gradle JDK to a compatible version (e.g., 17)
  2. Version Consistency Maintain consistent version references:

    • JavaVersion.VERSION_17 (compileOptions)
    • "17" (kotlinOptions.jvmTarget)
    • 17 (jvmToolchain)
  3. Kotlin Multiplatform Projects For KMP, configure both shared and platform-specific modules:

kotlin
// shared module
kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions.jvmTarget = "17"
        }
    }
}

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

Best Practices

  • Use the JVM toolchain approach for new projects
  • Avoid mixing version references (don't combine VERSION_1_8 with "17")
  • Update all modules consistently when changing Java versions
  • Verify your Kotlin plugin version supports your Java target
groovy
plugins {
    id 'org.jetbrains.kotlin.android' version '1.9.0' // Use latest stable version
}

TIP

The error message specifically recommends using the JVM toolchain approach as it automatically aligns Java and Kotlin compilation targets