Android与WearOS模块Kotlin Compose编译器版本不兼容求助
问题背景
应用包含Android和WearOS两个Jetpack Compose模块,构建时出现以下版本冲突问题:
场景1:使用kotlin-gradle-plugin:1.7.20
Android模块可正常构建,但Wear模块报错:
This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.7.20 which is not known to be compatible. Please fix your configuration (or
suppressKotlinVersionCompatibilityCheckbut don't say I didn't warn you!).
场景2:降级Kotlin版本至1.6.10
所有模块构建报错:
jetified-savedstate-ktx-1.2.1-api.jar!/META-INF/savedstate-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.
核心冲突点
- Android模块依赖
kotlin-gradle-plugin:1.7.20才能正常构建 - WearOS模块因Compose Compiler 1.1.1限制,需要
kotlin-gradle-plugin:1.6.10 - 全局降级Kotlin版本后,第三方库
savedstate-ktx的元版本与Kotlin 1.6.10不兼容 kotlin-gradle-plugin只能在项目根gradle文件全局配置,无法为单个模块单独指定版本
可行解决方案
方案1:升级WearOS模块的Compose版本适配Kotlin 1.7.20
Compose Compiler与Kotlin版本严格绑定,当前Wear模块使用的Compose Compiler 1.1.1对应Kotlin 1.6.10,直接升级到适配Kotlin 1.7.20的版本即可:
- 修改Wear模块的
build.gradle.kts(或build.gradle)中的Compose配置:
composeOptions { kotlinCompilerExtensionVersion = "1.3.2" // 该版本适配Kotlin 1.7.20 }
- 同步升级其他Compose库版本,保持与Compiler版本一致(示例):
implementation "androidx.compose.ui:ui:1.3.3" implementation "androidx.compose.material:material:1.3.1" implementation "androidx.wear.compose:compose-material:1.1.1"
此方案从根源解决版本冲突,是优先推荐的做法。
方案2:在Wear模块中启用版本兼容性检查抑制(临时方案)
若暂时无法升级Compose版本,可通过配置跳过版本检查:
修改Wear模块的build.gradle.kts:
// 抑制Kotlin元版本检查 tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { kotlinOptions { freeCompilerArgs += "-Xsuppress-version-warnings" freeCompilerArgs += "-Xskip-metadata-version-check" } } // 抑制Compose Compiler版本检查 composeOptions { kotlinCompilerExtensionVersion = "1.1.1" suppressKotlinVersionCompatibilityCheck = true }
注意:此方案为临时 workaround,可能引发潜在编译或运行时问题,仅作为过渡使用。
方案3:降级savedstate-ktx版本适配Kotlin 1.6.10
若坚持全局使用Kotlin 1.6.10,需替换为与Kotlin 1.6.x兼容的savedstate-ktx版本:
修改项目依赖为:
implementation "androidx.savedstate:savedstate-ktx:1.1.0"
该版本使用Kotlin 1.6.x编译,可解决元版本不兼容报错。
内容的提问来源于stack exchange,提问作者BlazeCodeDev

