Windows下构建Kotlin多平台模块遇kotlin-native-prebuilt依赖缺失问题
问题:Windows环境下KMM添加Native目标后构建失败
我需要开发支持Android和iOS的Kotlin Multiplatform模块,在Windows环境下将其作为Android应用的Gradle依赖构建。仅以Android为目标时构建正常,但添加任何Native目标后构建失败,报错信息如下:
Could not resolve all dependencies for configuration ':shared-mobile-lib:detachedConfiguration3'. The project declares repositories, effectively ignoring the repositories you have declared in the settings. You can figure out how project repositories are declared by configuring your build to fail on project repositories. Could not find :kotlin-native-prebuilt-windows-x86_64:1.7.10. Required by: project :shared-mobile-lib
对应的模块build.gradle.kt代码如下:
plugins { kotlin("multiplatform") id("com.android.library") } repositories { google() mavenCentral() } group = "com.agragps.mobile" version = "0.1.0" kotlin { android() // The below targets fail iosArm32 { binaries { framework { baseName = "library" } } } iosArm64 { binaries { framework { baseName = "library" } } } iosX64 { binaries { framework { baseName = "library" } } } androidNativeArm64() androidNativeArm32() sourceSets { val commonMain by getting val commonTest by getting { dependencies { implementation(kotlin("test")) } } val androidMain by getting { dependencies { implementation("com.google.android.material:material:1.8.0") } } val androidTest by getting { dependencies { implementation("junit:junit:4.13.2") } } val iosArm32Main by getting val iosArm32Test by getting val iosArm64Main by getting val iosArm64Test by getting val iosX64Main by getting val iosX64Test by getting val androidNativeArm64Main by getting val androidNativeArm64Test by getting val androidNativeArm32Main by getting val androidNativeArm32Test by getting } } android { compileSdkVersion(33) sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") defaultConfig { minSdkVersion(21) targetSdkVersion(33) } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } }
该项目基于Intellij IDEA示例创建,已验证Kotlin Native安装在.konan目录下,尝试配置构建失败时的项目仓库规则也未解决问题。
解决方案
补充Kotlin Native专属仓库
Kotlin Native的预构建构件存储在JetBrains专属仓库中,当前配置的google()和mavenCentral()无法获取到kotlin-native-prebuilt-windows-x86_64:1.7.10,需在模块的build.gradle.kt的repositories块中添加该仓库:repositories { google() mavenCentral() maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-native") } }修复仓库优先级冲突
报错提示项目声明的仓库覆盖了settings中的配置,需确保根目录settings.gradle.kts的pluginManagement块包含所有必要仓库,避免优先级冲突:pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-native") } } }处理Windows下iOS目标的构建限制
Windows环境无法本地构建iOS Native目标:- 若仅需构建Android及Android Native目标,可暂时注释掉
iosArm32、iosArm64、iosX64相关配置; - 若需保留iOS目标,需配置远程Mac构建:在根目录
gradle.properties中添加远程参数(替换为你的Mac机器信息):
同时在Mac机器上启动远程构建服务:kotlin.native.remote.build=true kotlin.native.remote.host=192.168.x.x kotlin.native.remote.port=5050 kotlin.native.remote.username=your_mac_username kotlin.native.remote.password=your_mac_passwordkonan startServer --port 5050
- 若仅需构建Android及Android Native目标,可暂时注释掉
清理缓存并重构建
执行以下命令清除Gradle缓存,强制重新下载依赖:./gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者cj-
相关产品推荐
相关产品推荐

