2

I faced the following error when upgrading a Jetpack Compose project from AGP 7.4.0 to 8.1.0-beta05:

    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.   Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

I tried to specify a JVM toolchain using

java {
//...
}

but it kept complaining with the same error. The similar question suggests using a java block and setting sourceCompatibility and targetCompatibility to Java 11. This also didn't work.

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

1 Answers1

3

As hinted in the error, the current JVM target seems to be set to 17 in AGP 8.0 onwards. However, we have historically set the targetCompability and sourceCompatibility to Java 8 in our Compose projects (even in official Compose documentation). The solution involves two changes in app/build.gradle:
1- Change Java version in compileOptions from 1_8 to 17:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

2- Change jvmTarget in kotlinOptions:

kotlinOptions {
    jvmTarget = '17'
}

Sync the project and the project should build successfully now.

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91