Kotlin多平台配置问题 [英] Kotlin Multiplatform Configuration issue
问题描述
我的KMP + Jetpack Compose项目中继续出现Gradle配置错误
I continue getting Gradle configuration error in my KMP + Jetpack Compose project
配置项目':shared'时出现问题.
A problem occurred configuring project ':shared'.
找不到名称为"testApi"的配置.
Configuration with name 'testApi' not found.
我的设置是:
- Android Studio Arctic Fox 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)
注意:通过部分删除配置,我似乎可以找出w问题似乎与android配置本身有关,所以如果我从中删除android()部分
Note: By removing the configuration part by part, I seem to figure out w that the problem seems to be around the android configuration itself, so if I remove android() part from
kotlin {
android()
....
只需使用简单的jvm()就可以了
and just go with simple jvm() it goes well
推荐答案
您可以在共享模块Gradle文件中使用以下代码作为解决方法
You can use the below code as a workaround in your shared module Gradle file
android {
configurations {
create("androidTestApi")
create("androidTestDebugApi")
create("androidTestReleaseApi")
create("testApi")
create("testDebugApi")
create("testReleaseApi")
}
}
注意:必须将其放置在kotlin {}块之前
NOTE: This has to be put before the kotlin {} block
这篇关于Kotlin多平台配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!