只能使用 releaseImplementation 和 debugImplementation 从 maven 添加 Kotlin Multiplatform Mobile 库

Can only add Kotlin Multiplatform Mobile library from maven by using releaseImplementation and debugImplementation

每次我将 Kotlin Multiplatform Mobile 库发布到 Maven Central 时,我似乎唯一能 add/use Android 应用程序中的 Android 依赖项是通过添加 releaseImplementationdebugImplementation

例子

releaseImplementation 'io.github.tyczj.lumberjack:Lumberjack-android:1.0.0@aar'
debugImplementation 'io.github.tyczj.lumberjack:Lumberjack-android-debug:1.0.0@aar'

而不是只有一个 implementation

的“正常”方式
implementation 'io.github.tyczj.lumberjack:Lumberjack-android:1.0.0'

这是我的 build.gradle 文件

plugins {
    kotlin("multiplatform") version "1.4.32"
    id("com.android.library")
    id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
    id("maven-publish")
    id("signing")
}

group = "io.github.tyczj.lumberjack"
version = "1.0.2"

ext["signing.keyId"] = ""
ext["signing.password"] = ""
ext["signing.secretKeyRingFile"] = ""

repositories {
    google()
    mavenCentral()
    maven {
        setUrl("https://plugins.gradle.org/m2/")
    }
}

val javadocJar by tasks.registering(Jar::class) {
    archiveClassifier.set("javadoc")
}

val emptyJar by tasks.registering(Jar::class) {
    archiveAppendix.set("empty")
}

kotlin {
    android{
        publishLibraryVariants("release", "debug")
    }
    iosX64("ios") {
        binaries {
            framework {
                baseName = "lumberjack"
            }
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting
        val androidMain by getting
        val androidTest by getting
        val iosMain by getting
        val iosTest by getting
    }
}

android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

afterEvaluate {
    publishing {
        repositories {
            maven {
                name = "sonatype"
                url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
                credentials {
                    username = rootProject.ext["ossrhUsername"]?.toString()
                    password = rootProject.ext["ossrhPassword"]?.toString()
                }
            }
        }

        publications.withType<MavenPublication> {

            artifact(javadocJar.get())

            pom{
                name.set("Lumberjack")
                description.set("Logging library for Kotlin Multiplatform Mobile applications")
                url.set("https://github.com/tyczj/Lumberjack")
                licenses {
                    license {
                        name.set("MIT")
                        url.set("https://opensource.org/licenses/MIT")
                    }
                }
                developers {
                    developer {
                        id.set("tyczj")
                        name.set("Jeff Tycz")
                        email.set("tyczj359@gmail.com")
                    }
                }
                scm {
                    url.set("https://github.com/tyczj/Lumberjack")
                }
            }
        }
    }
}

ext["signing.keyId"] = rootProject.ext["signing.keyId"]?.toString()
ext["signing.password"] = rootProject.ext["signing.password"]?.toString()
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]?.toString()

signing {
    sign(publishing.publications)
}

apply(from = "${rootDir}/scripts/publish-root.gradle")

可以找到此库的完整源代码here

我的 building/publishing KMM 库有什么问题,我必须在其中指定发布和调试实现?

您不应指定 -android 后缀,只需使用 implementation("io.github.tyczj.lumberjack:Lumberjack:1.0.0").

之所以可行,是因为依赖变体解析基于 Gradle Module Metadata。此元数据只是与您的图书馆一起发布的另一个文件(它具有 .module 扩展名)并且包含所有变体的描述。当您将库作为一个整体发布时,顶级工件 io.github.tyczj.lumberjack:Lumberjack 包含整个库的元数据,允许 gradle 选择正确的变体。

另一种选择是确保您的 -android 工件包含具有发布和调试变体的正确模块元数据。我相信 publishLibraryVariantsGroupedByFlavor 是告诉发布者插件这样做的方法,但我没有尝试过。