Android AGP 8 + Gradle 8 + Kotlin 1.8 causes error in Kapt
Asked Answered
C

4

16

I just updated to Android Studio Flamingo | 2022.2.1. Now I get this 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.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

I am using the AS included Kotlin which is 1.8.0 but this was working fine with AGP 7.4.2 and Gradle 7.5 - it only broke with the Gradle and AGP update coming from AS Flamingo. Also:

  • if I downgrade Kotlin to 1.7.20 it works again
  • if I update Kotlin to 1.8.20 it gives the error above

I do have the compile options:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

I tried also the sample AS apps but they have Kotlin 1.7.20. They will work with Kotlin 1.8.0 too - unless you introduce kapt in dependencies! (e.g. Dagger)

So what is the combination that should work - including kapt - and has the latest stable recommended versions from Android Studio?

  • Android Studio version?
  • AGP version?
  • Gradle version?
  • Kotlin version?

Please no untested answers. I know it "should" work but it doesn't.

Chasten answered 16/4, 2023 at 22:2 Comment(2)
I believe this might be the same problem as here?Portland
It's different, that is about using the wrong JDK to compile. This is to do with the preprocessor and compile options.Chasten
P
13

There is a compatibility issue between the latest Android Gradle plugin and Kotlin kapt. As a result, the jvmTarget that you specify in the Android configuration will be set on the Kotlin compilation tasks but not on the kapt task, which by default uses the toolchain version (currently JDK 17).

As a workaround, set the jvmTarget on the kapt task manually (in your case, the target is Java 1.8):

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Principalities answered 18/4, 2023 at 22:30 Comment(3)
Do you have a link to the relevant compatibility issue between the latest Android Gradle plugin and Kotlin kapt?Phraseograph
Thank you for this! I already had a kotlinOptions to deal with compileDebugKotlin, but needed this additional tasks.withType to resolve the kaptGenerateStubsDebugKotlin issue!Lightman
It's a bug in the kapt part of the Kotlin Gradle Plugin, introduced in version 1.8.0. Jetbrains seems to be unwilling to fix it, especially since kapt is deprecated in favor of KSP. youtrack.jetbrains.com/issue/KT-55947/…Principalities
D
6

I think use the jvmToolchain as the error info saying would solve this problem:

Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

With the following version and config my project works well.

  • Android Studio Flamingo 2022.2.1
  • AGP version 8.0.0
  • Gradle version 8.0
  • Kotlin version 1.8.0

config in build.gradle:

kotlin {
    jvmToolchain(8)
}


android {

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
}

If want to know more details, you can see the related official doc and explanation in below two links:

https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support

Unable to set kapt jvm target version:

https://youtrack.jetbrains.com/issue/KT-55947

Dearr answered 18/4, 2023 at 3:27 Comment(9)
Using jvmToolchain(8) requires you to have a JDK 8 installed on your machine, which is probably not supported by recent versions of the Android Gradle plugin. For Android it's best to use the default toolchain and manually set the jvmTarget to a lower version.Principalities
@Principalities how?Chasten
@yufeng wu doesn't work, I get: > No matching toolchains found for requested specification: {languageVersion=8, vendor=any, implementation=vendor-specific}.Chasten
@Principalities is right! I will accept his answer. Thanks!Chasten
@Chasten As BladeCoder saying, jvmToolchain need user to have the corresponding jdk of the version(jdk8 in your case),I have various installed jdks for using so it can works well.His answer is more adaptable indeed.Dearr
The documentation suggests not to use JVM toolchains before AGP 8.1.0-alpha09.Cockrell
@Cockrell As the documentation says, jvm toolchains can be use before AGP8.1.0-alpha09 if use with compileOptions,which is already contains in most of the android projects. compileOptions { sourceCompatibility = <MAJOR_JDK_VERSION> targetCompatibility = <MAJOR_JDK_VERSION> }Dearr
The warning implies you should use compile options instead of jvmToolchain, not at the same time.Cockrell
On my understanding,the warning implies that user use AGP8.1.0-aplha09 above is recommended,because if you use below this version,you should configure two things, and configure two things need a litte more work(not should not use this two ways)and all of those is caused by bug. Besides, use compile options only is apparently cannnot fixes this problem. So the way to solve this problem is to use jvmToolchain or configure kapt jvmTaret use task as RumburaK.It is just a choice depend on each users real condition. warnning is not says this using may have bad affects.Dearr
A
2

I use the following versions in my project, with which the project builds without problems:

  • Android Studio Flamingo 2022.2.1
  • AGP version 8.0.0
  • Gradle version 8.0
  • Kotlin version 1.8.0

To get rid of the error, change your code to the following:

android {
    ...
    compileOptions {
        sourceCompatibilityJavaVersion.VERSION_17
        targetCompatibilityJavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    ...
}

Here you can see the Gradle 8.0.0 compatibility table with the version of the JDK that should be used in the project

UPD:

If you don't want to change your Java Version and you are using Groovy DSL in your project then @BladeCoder's answer is the best solution.

However, if you are using the Kotlin DSL, then the syntax for this solution will look a little different:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs> {
    kotlinOptions {
        jvmTarget="1.8"
    }
}
Attractive answered 16/4, 2023 at 23:0 Comment(7)
Thanks! It seems to work, but I am puzzled, I thought older versions of Android would only run Java 7 or 8, my app supports down to API 19 (KitKat) so how can it support an app set compiled for Java 17?Chasten
I checked, and it works well with both API 19 and earlier API 16Attractive
Me too, though I would like to understand. I never saw Java 17 mentioned in the docs, only Java 8. Maybe something to do with desugaring? But would it support all the language features? And if not, will it actually warn if something goes wrong?Chasten
Yes, older versions of the API will work thanks to desugaring. I think java version upgrade is done to improve gradle build performanceAttractive
But one thing is Java version used for building and completely different the Java version of the code produced from the source. In this case we are trying to make sure a VM system from 2013 can run Java 17.Chasten
I was able to run an app with API 16 and JDK 17 on a virtual device in AVD Manager with API 16 Android 4.1 (Google API) image. I think there should be no problems on a physical device either.Attractive
This is not a proper fix, it forces you to use a target compatibility version equal to the current toolchain version which is not what most people want.Principalities
S
1

This worked for me perfectly ╰( ͡° ͜ʖ ͡° )つ──☆*:・゚

project's build.gradle file:

allprojects {    tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
            apiVersion = "1.8"
            languageVersion = "1.8"
        }
    }
}
Stewardess answered 24/4, 2023 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.