13

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.

RumburaK
  • 1,985
  • 1
  • 21
  • 30
  • I believe this might be the same problem as [here](https://stackoverflow.com/questions/75972855/android-gradle-plugin-requires-java-11-to-run/75972873#75972873)? – JustSightseeing Apr 16 '23 at 22:46
  • 1
    It's different, that is about using the wrong JDK to compile. This is to do with the preprocessor and compile options. – RumburaK Apr 16 '23 at 23:43

4 Answers4

12

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"
    }
}
BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • 2
    Do you have a link to the relevant compatibility issue between the latest Android Gradle plugin and Kotlin kapt? – chrisfey May 16 '23 at 15:10
  • 1
    Thank you for this! I already had a kotlinOptions to deal with compileDebugKotlin, but needed this additional tasks.withType to resolve the kaptGenerateStubsDebugKotlin issue! – Bink May 18 '23 at 16:55
  • 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. https://youtrack.jetbrains.com/issue/KT-55947/Unable-to-set-kapt-jvm-target-version – BladeCoder Aug 23 '23 at 15:22
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

yufeng wu
  • 61
  • 2
  • 1
    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. – BladeCoder Apr 18 '23 at 22:33
  • @BladeCoder how? – RumburaK Apr 19 '23 at 13:09
  • @yufeng wu doesn't work, I get: `> No matching toolchains found for requested specification: {languageVersion=8, vendor=any, implementation=vendor-specific}.` – RumburaK Apr 19 '23 at 13:10
  • @BladeCoder is right! I will accept his answer. Thanks! – RumburaK Apr 19 '23 at 13:24
  • @RumburaK 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. – yufeng wu Apr 20 '23 at 03:28
  • 3
    The [documentation](https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support) suggests not to use JVM toolchains before AGP 8.1.0-alpha09. – argenkiwi Apr 21 '23 at 00:41
  • @argenkiwi 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 = targetCompatibility = } – yufeng wu Apr 21 '23 at 03:05
  • The warning implies you should use compile options *instead* of jvmToolchain, not at the same time. – argenkiwi Apr 22 '23 at 04:17
  • 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. – yufeng wu Apr 22 '23 at 10:14
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"
    }
}
Victor Sklyarov
  • 827
  • 2
  • 3
  • 24
  • 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? – RumburaK Apr 17 '23 at 00:43
  • 1
    I checked, and it works well with both API 19 and earlier API 16 – Victor Sklyarov Apr 17 '23 at 09:43
  • 1
    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? – RumburaK Apr 17 '23 at 10:53
  • Yes, older versions of the API will work thanks to [desugaring](https://developer.android.com/studio/write/java11-minimal-support-table). I think java version upgrade is done to improve gradle build performance – Victor Sklyarov Apr 17 '23 at 12:18
  • 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. – RumburaK Apr 17 '23 at 14:01
  • 1
    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. – Victor Sklyarov Apr 17 '23 at 23:53
  • 2
    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. – BladeCoder Apr 18 '23 at 18:20
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"
        }
    }
}
GFPF
  • 959
  • 14
  • 19