由于 android 目标,Kotlin Multiplatform 构建失败

Kotlin Multiplatform build failure due to android target

我正在创建一个支持 Android 的 Kotlin 多平台库。

当我尝试使用 gradle 构建时出现错误 (./gradlew build --warning-mode all)。

或者这个:

Could not determine the dependencies of task ':lib:compileDebugAidl'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk

或者:

Could not determine the dependencies of task ':lib:verifyReleaseResources'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk

索引后的第一个和我做的第二个./gradlew clean

但是,无论如何都存在这种依赖性,因为我已经基于它创建了 Android 应用程序。 我也尝试过Target 28和30的RC版本(因为30还没有正式发布,但我还是创建了应用程序)

我的 gradle 文件结构是这样的:

root
|-build.gradle.kts
|-settings.gradle.kts
|-gradlew
|-gradlew.bat
|-gradle
   |-wrapper
      |-gradle-wrapper.jar
      |-gradle-wrapper.properties
|-lib
   |-build.gradle.kts

settings.gradle.kts:

rootProject.name = "Library"

include(":lib")

root build.gradle.kts:

buildscript {
    repositories {
        addRepos()
        maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
            metadataSources {
                gradleMetadata()
                mavenPom()
            }
        }
    }

    dependencies {
        classpath("com.android.tools.build:gradle:4.1.0-beta04")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
    }
}

allprojects {
    repositories {
        addRepos()
        maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
            metadataSources {
                gradleMetadata()
                mavenPom()
            }
        }
    }
}

lib build.gradle.kts:

plugins {
    id("com.android.library")
    kotlin("multiplatform")
    id("maven-publish")
}
group = "de.datlag"
version = "1.0"

android {
    compileSdkVersion = 29.toString()
    buildToolsVersion = "29.0.3"

    defaultConfig {
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }

    buildTypes {
        val debug by getting {
            isMinifyEnabled = false
            isDebuggable = true
            isShrinkResources = false
        }

        val release by getting {
            isMinifyEnabled = false
            isDebuggable = false
            isShrinkResources = false
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

repositories {
    addRepos()
}

kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    }
    js {
        browser()
        nodejs()
    }
    android {
        publishAllLibraryVariants()
    }
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val jvmMain by getting {
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
            }
        }
        val jvmTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-junit"))
            }
        }
        val androidMain by getting {
            dependsOn(jvmMain)
        }
        val androidTest by getting {
            dependsOn(jvmTest)
        }
        val jsMain by getting {
            dependencies {
                implementation(kotlin("stdlib-js"))
            }
        }
        val jsTest by getting {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
        val nativeMain by getting { }
        val nativeTest by getting { }
    }
}

addRepos 方法:

fun RepositoryHandler.addRepos() {
    mavenLocal {
        metadataSources {
            gradleMetadata()
            mavenPom()
        }
    }
    mavenCentral {
        metadataSources {
            gradleMetadata()
            mavenPom()
        }
    }
    jcenter {
        metadataSources {
            gradleMetadata()
            mavenPom()
        }
    }
    google {
        metadataSources {
            gradleMetadata()
            mavenPom()
        }
    }
    gradlePluginPortal()
}

在 lib.build.gradle.kts 中,您应该使用 compileSdkVersion(29) 语法而不是设置字符串。您可以在这些扩展的源代码中看到,如果调用字符串版本,行为会有所不同。

/** @see #getCompileSdkVersion() */
public void compileSdkVersion(String version) {
    checkWritability();
    this.target = version;
}

/** @see #getCompileSdkVersion() */
public void compileSdkVersion(int apiLevel) {
    compileSdkVersion("android-" + apiLevel);
}

传递 int 时,它会为您附加“android-”。所以你可以 compileSdkVersion = "android-29" 如果你愿意,但只传递 int 更容易