Kotlin Multiplatform与Compose的Gradle同步依赖解析问题求助
Kotlin Multiplatform + Compose Gradle依赖同步问题排查
问题描述
频繁遭遇Gradle同步失败,示例错误:
:composeApp:iosArm64Main: Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3.
Required by:
project :composeApp > com.google.android.material:material:1.12.0 > androidx.activity:activity:1.8.0 > androidx.lifecycle:lifecycle-viewmodel-savedstate:2.8.5
此类依赖解析问题涉及多个库,甚至一周前正常使用的库也出现报错。
项目配置代码
build.gradle.kts
plugins { alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.androidApplication) alias(libs.plugins.composeMultiplatform) alias(libs.plugins.composeCompiler) alias(libs.plugins.ksp) alias(libs.plugins.ktorfit) alias(libs.plugins.buildkonfig) alias(libs.plugins.serialization) } kotlin { androidTarget { @OptIn(ExperimentalKotlinGradlePluginApi::class) compilerOptions { jvmTarget.set(JvmTarget.JVM_22) } } listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach { iosTarget -> iosTarget.binaries.framework { baseName = "ComposeApp" isStatic = true } } sourceSets { androidMain.dependencies { implementation(compose.preview) implementation(libs.androidx.activity.compose) } commonMain.dependencies { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material) implementation(compose.ui) implementation(compose.components.resources) implementation(compose.components.uiToolingPreview) implementation(libs.androidx.lifecycle.viewmodel) implementation(libs.androidx.lifecycle.runtime.compose) implementation(libs.androidx.compose.google.fonts) implementation(libs.ktorfit) implementation(libs.kotlinx.serialization.json) implementation(libs.ktorfit.converters.call) implementation(libs.qr.kit) implementation(libs.androidx.navigation) implementation(libs.androidx.compose.material) } } } android { namespace = "xxx" compileSdk = libs.versions.android.compileSdk.get().toInt() defaultConfig { applicationId = "xxx" minSdk = libs.versions.android.minSdk.get().toInt() targetSdk = libs.versions.android.targetSdk.get().toInt() versionCode = 1 versionName = "0.1" } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } } buildTypes { getByName("release") { isMinifyEnabled = false } } compileOptions { sourceCompatibility = JavaVersion.VERSION_22 targetCompatibility = JavaVersion.VERSION_22 } composeOptions { kotlinCompilerExtensionVersion = "1.5.15" } } dependencies { debugImplementation(compose.uiTooling) } buildkonfig { packageName = "xxx" xxx } }
libs.versions.toml
[versions] agp = "8.7.2" android-compileSdk = "35" android-minSdk = "31" android-targetSdk = "34" androidx-activityCompose = "1.9.3" androidx-appcompat = "1.7.0" androidx-constraintlayout = "2.2.0" androidx-core-ktx = "1.15.0" androidx-espresso-core = "3.6.1" androidx-lifecycle = "2.8.4" androidx-test-junit = "1.2.1" androidx-material = "1.12.0" androidx-compose-google-fonts="1.7.5" androidx-navigation = "2.8.0-alpha10" compose-multiplatform = "1.7.0" junit = "4.13.2" kotlin = "2.0.21" ktorfit = "2.2.0" ksp = "2.0.21-1.0.28" okio = "3.9.1" buildkonfig = "0.15.2" serialization-json = "1.7.3" qrkit = "3.0.5" [libraries] kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" } androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" } androidx-compose-google-fonts = { group = "androidx.compose.ui", name = "ui-text-google-fonts", version.ref = "androidx-compose-google-fonts"} androidx-compose-material = { group = "com.google.android.material", name = "material" , version.ref = "androidx-material"} androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" } androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" } androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } androidx-navigation = { group = "org.jetbrains.androidx.navigation", name = "navigation-compose", version.ref = "androidx-navigation"} ktorfit = { group = "de.jensklingenberg.ktorfit", name = "ktorfit-lib", version.ref = "ktorfit"} squareup-okio = { group = "com.squareup.okio", name = "okio", version.ref = "okio" } buildkonfig = { group = "com.codingfeline.buildkonfig", name = "buildkonfig-gradle-plugin", version.ref = "buildkonfig"} kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization-json"} ktorfit-converters-call = { group = "de.jensklingenberg.ktorfit", name = "ktorfit-converters-call", version.ref = "ktorfit"} qr-kit = { group = "network.chaintech", name = "qr-kit", version.ref = "qrkit"} [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } androidLibrary = { id = "com.android.library", version.ref = "agp" } composeMultiplatform = { id = "org.jetbrains.compose", version.ref = "compose-multiplatform" } composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } ktorfit = { id = "de.jensklingenberg.ktorfit", version.ref = "ktorfit" } buildkonfig = { id = "com.codingfeline.buildkonfig", version.ref = "buildkonfig"} jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
排查修复方案
1. 修正依赖声明位置与groupId错误
- 移动Android专属依赖:将
libs.androidx.compose.material从commonMain.dependencies移至androidMain.dependencies。该库是Android平台专属组件,你已在commonMain中声明跨平台的compose.material,重复引入会导致iOS源码集错误拉取Android依赖。 - 修复Lifecycle依赖groupId:libs.toml中
androidx-lifecycle-viewmodel和androidx-lifecycle-runtime-compose的groupId错误,应改为androidx.lifecycle,修正后如下:androidx-lifecycle-viewmodel = { group = "androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" } androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
2. 匹配Compose Compiler与Kotlin版本
Kotlin 2.0.21对应的Compose Compiler版本应为1.6.x系列(如1.6.2),修改android块中的composeOptions:
composeOptions { kotlinCompilerExtensionVersion = "1.6.2" }
3. 统一依赖版本
在libs.versions.toml中锁定Lifecycle版本为2.8.4,避免传递依赖引入不一致版本:
[versions] androidx-lifecycle = "2.8.4"
4. 清理依赖缓存
- 执行命令强制刷新依赖:
./gradlew clean build --refresh-dependencies - 删除本地Gradle缓存目录:
- macOS/Linux:
~/.gradle/caches - Windows:
C:\Users\<你的用户名>\.gradle\caches
- macOS/Linux:
5. 添加跨平台依赖约束
在kotlin块中配置约束,禁止iOS源码集拉取Android专属库:
kotlin { // ... 原有代码 configurations.all { if (name.contains("ios")) { exclude(group = "androidx.*") exclude(group = "com.google.android.material") exclude(group = "org.jetbrains.kotlinx", module = "kotlinx-coroutines-android") } } }
内容的提问来源于stack exchange,提问作者Nogi
相关产品推荐
相关产品推荐

