【问题标题】:Kotlin Multiplatform Configuration issueKotlin 多平台配置问题
【发布时间】:2021-03-30 00:56:55
【问题描述】:

我在我的 KMP + Jetpack Compose 项目中继续收到 Gradle 配置错误

配置项目 ':shared' 时出现问题。

找不到名为“testApi”的配置。

我的设置是:

  1. Android Studio 北极狐 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)

注意: 通过部分删除配置,我似乎弄清楚了 w 问题似乎与android配置本身有关, 所以如果我从

中删除 android() 部分
kotlin {
    android()
    ....

只要使用简单的 jvm() 就可以了

【问题讨论】:

  • 哦,真不幸,最好的处理方法是什么?我应该回滚到以前的 android studio 版本并使用以前的 gradle 插件吗?也形成了那里的问题,我可以看到一些 AGP 修复在 android studio 的 Canary 5 中出现,是否有一些可以跟踪它何时到来?
  • 我还没有在这里更新,但我听说其他人现在回滚以解决这个问题。

标签: android kotlin gradle kotlin-multiplatform


【解决方案1】:

您可以在共享模块 Gradle 文件中使用以下代码作为解决方法

android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}

注意:这必须放在 kotlin {} 块之前

【讨论】:

  • 谢谢,这对我有用。你能分享一个资源来理解为什么会这样吗?
  • 这是由 kotlin KMM 配置中的错误引起的 - 已修复的错误应该在 1.5.0 中。 youtrack.jetbrains.com/issue/KT-43944
  • 谢谢。它对我有用。那么,为什么会这样呢?
【解决方案2】:

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

问题出在 Canary 或 AGP 7.0.0 中:

  • IDE:金丝雀 11
  • distributionUrl: 6.8.2
  • 7.0.0-alpha11

解决方法 1:

重要提示:确保文件是 groovy 或 dsl

PRECONDITION:这些配置必须在项目的所有 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.zip
  • classpath 'com.android.tools.build:gradle:4.2.0-beta06'

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2019-05-01
    • 1970-01-01
    • 2021-07-04
    • 2020-09-05
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多