解决AGP升级到8.0后编译报错kaptGenerateStubsDebugKotlin

问题描述

升级了Gradle插件到8.0,运行报以下错误.

shell 复制代码
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

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

解决方案

看错误是Kapt和java版本不兼容,根据提示的网站地址(https://kotl.in/gradle/jvm/toolchain),AGP版本小于8.1.0,需要显式的设置jdk的兼容版本。如果不指定版本,Android默认使用的是JDK1.8。

shell 复制代码
A warning for Android users. To use Gradle toolchain support, use the Android Gradle plugin (AGP) version 8.1.0-alpha09 or higher.

Gradle Java toolchain support is available only from AGP 7.4.0. Nevertheless, because of this issue, AGP did not set targetCompatibility to be equal to the toolchain's JDK until the version 8.1.0-alpha09. If you use versions less than 8.1.0-alpha09, you need to configure targetCompatibility manually via compileOptions. Replace the placeholder <MAJOR_JDK_VERSION> with the JDK version you would like to use:

android {
    compileOptions {
        sourceCompatibility = <MAJOR_JDK_VERSION>
        targetCompatibility = <MAJOR_JDK_VERSION>
    }
}

打开app的build.gradle,在android块中添加以下代码,设置java的版本为本地对应的版本,因为我本地的是17,所以都为17.

gradle 复制代码
android {
	....
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }
    ....
}

重新运行,报错消失。

相关推荐
悲凉的紫菜1 小时前
dariy
android·ide·android studio
2501_915909061 小时前
IPA 深度混淆是什么意思?从混淆强度到实际效果的解读
android·ios·小程序·https·uni-app·iphone·webview
LT10157974442 小时前
2026年手机机型兼容性测试选型指南:安卓机型碎片化、APP闪退UI错乱如何解决
android·测试工具·ui·智能手机
阿pin3 小时前
Android随笔-view是如何显示到屏幕上
android·view
恋猫de小郭3 小时前
Flutter material/cupertino 解耦最新进展,已经在做新样式了
android·前端·flutter
在书中成长3 小时前
HarmonyOS 小游戏《对战五子棋》开发第17篇 - AI的两种评估策略
android·java·javascript
私人珍藏库4 小时前
[Android] Npatch-免Root框架+可内置模块
android·人工智能·智能手机·工具·软件
黄林晴4 小时前
KMP 正式支持 SwiftPM:CocoaPods 迁移配置指南
android·ios·kotlin
AFinalStone5 小时前
Android 7系统网络(二)内核层—netfilter_iptables与路由策略
android·网络
吠品5 小时前
Mac定时关机、开机自动化:用shutdown和pmset就够了
android·java·开发语言