英文:
Android AGP 8 + Gradle 8 + Kotlin 1.8 causes error in Kapt
问题
I just updated to Android Studio Flamingo | 2022.2.1
. Now I get this error:
> ' 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:
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.
英文:
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.
答案1
得分: 12
最新的Android Gradle插件与Kotlin kapt之间存在兼容性问题。因此,在Android配置中指定的jvmTarget
将设置在Kotlin编译任务上,但不会设置在kapt任务上,kapt任务默认使用工具链版本(目前为JDK 17)。
作为解决方法,在kapt任务上手动设置jvmTarget
(在您的情况下,目标是Java 1.8):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
kotlinOptions {
jvmTarget = "1.8"
}
}
英文:
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"
}
}
答案2
得分: 6
I think use the jvmToolchain as the error info saying would solve this problem:
考虑使用 JVM 工具链: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
build.gradle 中的配置:
kotlin {
jvmToolchain(8)
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
如果想了解更多详情,可以查看以下两个链接中的相关官方文档和解释:
https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
无法设置 kapt 的 JVM 目标版本:
https://youtrack.jetbrains.com/issue/KT-55947
<details>
<summary>英文:</summary>
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
</details>
# 答案3
**得分**: 2
以下是翻译好的部分:
我在我的项目中使用以下版本,该项目可以顺利构建:
- Android Studio Flamingo `2022.2.1`
- `AGP` 版本 `8.0.0`
- `Gradle` 版本 `8.0`
- `Kotlin` 版本 `1.8.0`
要消除错误,请将您的代码更改为以下内容:
```groovy
android {
...
compileOptions {
sourceCompatibilityJavaVersion.VERSION_17
targetCompatibilityJavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
...
}
在此您可以查看与项目中应使用的JDK版本相关的Gradle 8.0.0
兼容性表格。
更新:
如果您不想更改您的Java版本
并且在项目中使用Groovy DSL
,那么@BladeCoder的回答是最佳解决方案。
然而,如果您使用的是Kotlin DSL
,那么此解决方案的语法将略有不同:
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs> {
kotlinOptions {
jvmTarget="1.8"
}
}
英文:
I use the following versions in my project, with which the project builds without problems:
- Android Studio Flamingo
2022.2.1
AGP
version8.0.0
Gradle
version8.0
Kotlin
version1.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"
}
}
答案4
得分: 1
这对我完全有效 ╰( ͡° ͜ʖ ͡° )つ──☆*:・゚
项目的 build.gradle 文件:
allprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
apiVersion = "1.8"
languageVersion = "1.8"
}
}
}
英文:
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"
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论