简体   繁体   English

Kotlin 多平台配置问题

[英]Kotlin Multiplatform Configuration issue

I continue getting Gradle configuration error in my KMP + Jetpack Compose project我在我的 KMP + Jetpack Compose 项目中继续收到 Gradle 配置错误

A problem occurred configuring project ':shared'.配置项目 ':shared' 时出现问题。

Configuration with name 'testApi' not found.未找到名称为“testApi”的配置。

My setup is:我的设置是:

  1. Android Studio Arctic Fox 2020.3.1 Canary 3 Android Studio北极狐2020.3.1金丝雀3
  2. Project level setup项目级别设置
dependencies {
   classpath("com.android.tools.build:gradle:7.0.0-alpha03")
   classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
}
  1. 'shared module' '共享模块'
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)

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注意:通过部分删除配置,我似乎发现问题似乎与 android 配置本身有关,所以如果我从

kotlin {
    android()
    ....

and just go with simple jvm() it goes well只需 go 和简单的 jvm() 就可以了

You can use the below code as a workaround in your shared module Gradle file您可以在共享模块 Gradle 文件中使用以下代码作为解决方法

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

NOTE : This has to be put before the kotlin {} block注意:这必须放在 kotlin {} 块之前

Fixed issue in Kotlin 1.5 M1 (pending)修复了Kotlin 1.5 M1中的问题(待定)

The problem is in Canary or AGP 7.0.0:问题出在 Canary 或 AGP 7.0.0 中:

  • IDE: Canary 11 IDE:金丝雀 11
  • distributionUrl: 6.8.2分布网址:6.8.2
  • 7.0.0-alpha11 7.0.0-alpha11

Workaround 1:解决方法 1:

IMPORTANT: Make sure the file is groovy or dsl重要提示:确保文件是 groovy 或 dsl

PRECONDITION : These configurations have to be done in all modules / sub-modules of the project that are KMM and the android {} block has to be before the kotlin {} block前提条件:这些配置必须在项目的所有模块/子模块中完成,即 KMM 和android {}块必须在kotlin {}块之前

For Kotlin DSL:对于 Kotlin DSL:

build.gradle.kts (:kmm_shared) build.gradle.kts (:kmm_shared)

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

For Groovy:对于 Groovy:

build.gradle (:kmm_shared) build.gradle (:kmm_shared)

android {
    configurations {
        androidTestApi {}
        androidTestDebugApi {}
        androidTestReleaseApi {}
        testApi {}
        testDebugApi {}
        testReleaseApi {}
    }
}
kotlin { }

Also, you should to use AGP 7.0 because previous versions of gradle generate problems.此外,您应该使用 AGP 7.0,因为以前版本的 gradle 会产生问题。

build.gradle.kts (:project) && build.gradle.kts (:buildSrc) build.gradle.kts (:project) && build.gradle.kts (:buildSrc)

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

Workaround 2 (Deprecated)解决方法 2(已弃用)

Temporarily use a maximum of the beta versions:暂时使用最多的 beta 版本:

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

Good Luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM