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

Android Gradle构建失败:JVM目标版本兼容问题求助

解决Android构建JVM目标版本不兼容问题

错误信息

任务:app:kaptGenerateStubsDebugKotlin执行失败。

'compileDebugJavaWithJavac'任务(当前目标版本为1.8)与'kaptGenerateStubsDebugKotlin'任务(当前目标版本为17)的JVM目标兼容性需设置为同一Java版本。

已尝试方案

使用--stacktrace选项获取堆栈跟踪。
使用--info或--debug选项获取更多日志输出。
使用--scan选项获取完整分析信息。

项目配置

Build.gradle (project)

buildscript {
    ext {
        compose_ui_version = '1.4.0'
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3"
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

Build.gradle(app)

android {
    namespace 'com.sturec.sturecadmin'
    compileSdk 33

    defaultConfig {
        applicationId "com.sturec.sturecadmin"
        minSdk 26
        targetSdk 33
        versionCode 7
        versionName "1.1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

解决方案

方案1:强制kapt使用统一JVM版本

在app/build.gradle中添加kapt配置,显式指定Java版本:

android {
    // 现有配置...
}

// 添加以下配置
kapt {
    javacOptions {
        option("-source", "1.8")
        option("-target", "1.8")
    }
}

方案2:使用JVM工具链统一版本(官方推荐)

通过JVM工具链配置,让所有编译任务自动使用指定的Java版本,避免版本不一致问题。修改app/build.gradle的android块:

android {
    // 现有配置...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    // 添加工具链配置
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(8)
        }
    }
}

该方式会让Gradle自动管理指定版本的JDK,确保Java、Kotlin、kapt等所有编译任务使用同一版本,彻底解决兼容性冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 11:47:09