You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决Android项目kaptGenerateStubsDebugKotlin任务JVM版本兼容错误

解决':app:kaptGenerateStubsDebugKotlin'任务JVM版本不匹配错误

执行:app:kaptGenerateStubsDebugKotlin任务失败,核心原因是Java编译任务与Kotlin kapt任务的JVM目标版本不一致:compileDebugJavaWithJavac使用的是1.8版本,而kaptGenerateStubsDebugKotlin使用的是17版本,两者必须统一为相同的Java版本。

修正后的app模块build.gradle配置如下:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.secrets_gradle_plugin' version '0.4'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
    id 'androidx.navigation.safeargs'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
}

android {
    namespace 'com.example.appnews'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.appnews"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        buildConfigField "String", "API_KEY", "\"${properties.getProperty('API_KEY')}\""
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }

    buildFeatures{
        viewBinding = true
    }
}

// 依赖部分保持不变

核心修改说明:

  • 在compileOptions中统一Java源码和目标编译版本为17
  • 在kotlinOptions中指定jvmTarget为17,确保Kotlin编译与Java编译版本对齐

内容的提问来源于stack exchange,提问作者mattew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 15:10:59