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通常是依赖传递引入的旧版本,按以下步骤处理:
- 执行依赖分析命令,定位版本来源:
在输出中搜索# macOS/Linux ./gradlew app:dependencies # Windows gradlew.bat app:dependenciescompose-compiler,找到间接引入旧版本的依赖。 - 在app模块的
build.gradle中强制指定Compiler版本(需放在其他Compose依赖之前):dependencies { implementation "androidx.compose.compiler:compiler:$compose_version" // 其他Compose依赖(如ui、material等) } - (可选)在根目录
build.gradle中全局强制统一版本:dependencyResolutionManagement { versions { force "androidx.compose.compiler:compiler:$compose_version" } }
3. 正确配置Kotlin版本
IntelliJ的编译器偏好不影响Gradle构建,必须在Gradle配置中指定版本:
- 在根目录的
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 } - 确保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
相关产品推荐
相关产品推荐

