Kotlin多平台配置问题



我的KMP+Jetpack Compose项目中继续出现Gradle配置错误

配置项目":共享"时出现问题。

未找到名为"testApi"的配置。

我的设置是:

  1. 安卓工作室Arctic Fox 2020.3.1 Canary 3
  2. 项目级别设置
dependencies {
classpath("com.android.tools.build:gradle:7.0.0-alpha03")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
}
  1. "共享模块">
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{}块之前

修复Kotlin 1.5 M1中的问题(待定(

问题出现在Canary或AGP 7.0.0:中

  • IDE:金丝雀11
  • distributionUrl:6.8.2
  • 7.0.0α11

解决方案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,因为以前版本的渐变会产生问题。

build.gradle.kts(:项目(&amp;build.gradle.kts(:buildSrc(

dependencies {
implementation("com.android.tools.build:gradle:7.0.0-alpha11")
}

解决方案2(已弃用(

暂时最多使用测试版:

  • IDE:Beta 6
  • distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-all.zip
  • classpath 'com.android.tools.build:gradle:4.2.0-beta06'

好运

  • 问题/KT-43944
  • 在Mobius中提交

最新更新