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

Android Native Jetpack Compose依赖问题:JVM与Kotlin版本不兼容

解决Jetpack Compose编译版本不兼容问题

报错信息

Task :app:compileDebugKotlin FAILED
'compileDebugJavaWithJavac' task (current target is 17) and 'compileDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
e: This version (1.2.0-alpha05) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.7.10 which is not known to be compatible.  Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

1. 统一Kotlin与Java的JVM目标版本

Java的compileOptions仅控制Java编译任务,Kotlin编译任务需单独配置JVM目标版本。在app模块的build.gradle(或build.gradle.kts)中添加:

// 新版Gradle推荐写法
kotlin {
    jvmToolchain(17)
}

// 旧版Gradle兼容写法
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        jvmTarget = "17"
    }
}

2. 修正Compose Compiler版本不一致问题

报错中的1.2.0-alpha05通常是依赖传递引入的旧版本,按以下步骤处理:

  1. 执行依赖分析命令,定位版本来源:
    # macOS/Linux
    ./gradlew app:dependencies
    # Windows
    gradlew.bat app:dependencies
    
    在输出中搜索compose-compiler,找到间接引入旧版本的依赖。
  2. 在app模块的build.gradle中强制指定Compiler版本(需放在其他Compose依赖之前):
    dependencies {
        implementation "androidx.compose.compiler:compiler:$compose_version"
        // 其他Compose依赖(如ui、material等)
    }
    
  3. (可选)在根目录build.gradle中全局强制统一版本:
    dependencyResolutionManagement {
        versions {
            force "androidx.compose.compiler:compiler:$compose_version"
        }
    }
    

3. 正确配置Kotlin版本

IntelliJ的编译器偏好不影响Gradle构建,必须在Gradle配置中指定版本:

  1. 在根目录的build.gradle中配置Kotlin插件版本:
    plugins {
        id 'com.android.application' version '7.4.2' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    }
    
  2. 确保app模块的build.gradle中应用的插件与根目录版本一致:
    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
    }
    
    注意:kotlin { version("1.6.10") }是无效写法,必须在plugins块中指定版本。

最后执行清理构建命令,确保配置生效:

./gradlew clean build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 11:05:15