Kotlin多平台配置问题
我的 KMP + Jetpack Compose 项目中继续出现 Gradle 配置错误
配置项目“:shared”时出现问题。
未找到名为“testApi”的配置。
我的设置是:
- Android Studio 北极狐 2020.3.1 Canary 3
- 项目级别设置
dependencies {
classpath("com.android.tools.build:gradle:7.0.0-alpha03")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
}
- '共享模块'
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.1")
}
}
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(21)
targetSdkVersion(30)
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
注意:通过逐个删除配置,我似乎发现问题似乎出在 android 配置本身周围,所以如果我从中删除 android() 部分
kotlin {
android()
....
只需使用简单的 jvm() 就可以了
回答
您可以在共享模块 Gradle 文件中使用以下代码作为解决方法
android {
configurations {
create("androidTestApi")
create("androidTestDebugApi")
create("androidTestReleaseApi")
create("testApi")
create("testDebugApi")
create("testReleaseApi")
}
}
注意:这必须放在 kotlin {} 块之前
- Thank you, this worked for me. Can you share a resource to understand why this works?
- It's caused by a bug in kotlin KMM config - the bugs fixed and should be in 1.5.0. https://youtrack.jetbrains.com/issue/KT-43944
回答
已修复Kotlin 1.5 M1(待定)中的问题
问题出在 Canary 或 AGP 7.0.0 中:
- IDE:金丝雀 11
- 分发网址:6.8.2
- 7.0.0-alpha11
解决方法 1:
重要提示:确保文件是 groovy 或 dsl
前提条件:这些配置必须在项目的所有模块/子模块中完成,这些模块/子模块是 KMM,并且android {}块必须在kotlin {}块
对于 Kotlin DSL:
build.gradle.kts (:kmm_shared)
android {
configurations {
create("androidTestApi")
create("androidTestDebugApi")
create("androidTestReleaseApi")
create("testApi")
create("testDebugApi")
create("testReleaseApi")
}
}
kotlin { }
对于 Groovy:
build.gradle (:kmm_shared)
android {
configurations {
androidTestApi {}
androidTestDebugApi {}
androidTestReleaseApi {}
testApi {}
testDebugApi {}
testReleaseApi {}
}
}
kotlin { }
此外,您应该使用 AGP 7.0,因为以前版本的 gradle 会产生问题。
build.gradle.kts (:project) && build.gradle.kts (:buildSrc)
dependencies {
implementation("com.android.tools.build:gradle:7.0.0-alpha11")
}
解决方法 2(已弃用)
暂时最多使用 beta 版本:
- IDE:测试版 6
distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-all.zipclasspath 'com.android.tools.build:gradle:4.2.0-beta06'
祝你好运
- 问题/KT-43944
- 在莫比乌斯提交