“kaptGenerateStubsDebugKotlin”任务(当前目标是 17)jvm 目标兼容性应设置为相同的 Java 版本

cod*_*bie 7 kotlin

将 Jetpack Compose 项目从 AGP 7.4.0 升级到 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

Run Code Online (Sandbox Code Playgroud)

我尝试使用指定 JVM 工具链

java {
//...
}
Run Code Online (Sandbox Code Playgroud)

但它一直抱怨同样的错误。类似的问题建议使用 java 块并将 sourceCompatibility 和 targetCompatibility 设置为 Java 11。这也不起作用。

cod*_*bie 12

正如错误中所暗示的,从 AGP 8.0 开始,当前 JVM 目标似乎设置为 17。然而,我们历史上在 Compose 项目中(甚至在官方 Compose 文档中)将 targetCompability 和 sourceCompatibility 设置为 Java 8。该解决方案涉及 app/build.gradle 中的两处更改:
1- 将compileOptions 中的 Java 版本从 1_8 更改为 17:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}
Run Code Online (Sandbox Code Playgroud)

2-更改 kotlinOptions 中的 jvmTarget:

kotlinOptions {
    jvmTarget = '17'
}
Run Code Online (Sandbox Code Playgroud)

同步项目,项目现在应该成功构建。