releaseImplementation和debugImple

releaseImplementation和debugImple

本文介绍了只能使用releaseImplementation和debugImplementation从Maven添加Kotlin Multiplatform Mobile库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我将Kotlin Multiplatform Mobile库发布到Maven Central时,我似乎唯一可以在Android应用程序中添加/使用Android依赖项的方法是同时添加 releaseImplementation debugImplementation

Every time I publish a Kotlin Multiplatform Mobile library to maven central the only I can seem to add/use the Android dependency in an Android app is by adding both the releaseImplementation and debugImplementation

示例

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

代替正常"您只有一个实现

Instead of the "normal" way where you just have a single implementation

implementation 'io.github.tyczj.lumberjack:Lumberjack-android:1.0.0'

这是我的build.gradle文件

Here is my build.gradle file

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("[email protected]")
                    }
                }
                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")

可以在此处

我如何在必须指定发布和调试实现的地方构建/发布KMM库,这有什么问题?

What is wrong with how I am building/publishing KMM libraries where I have to specify the release and debug implementations?

推荐答案

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

You should not specify -android postfix, just use implementation("io.github.tyczj.lumberjack:Lumberjack:1.0.0").

之所以可行,是因为依赖项变体解析基于 Gradle模块元数据.此元数据只是随您的库发布的另一个文件(扩展名为 .module ),其中包含所有变体的描述.在整个发布库时,顶级工件 io.github.tyczj.lumberjack:Lumberjack 包含整个库的元数据,从而允许gradle选择正确的变体.

This works because dependency variant resolution is based on Gradle Module Metadata. This metadata is just another file published with your library (it has .module extension) and it contains description of all variants. As you are publishing the library as a whole, the top-level artifact io.github.tyczj.lumberjack:Lumberjack contains the metadata for the whole library, allowing gradle to choose the right variant.

另一个选择是确保您的 -android 工件包含带有发布和调试变体的正确模块元数据.我相信 publishLibraryVariantsGroupedByFlavor 是判断的方法发布者插件可以通过这种方式实现,但我还没有尝试过.

Another option would be to make sure your -android artifact contains proper module metadata with both release and debug variants. I believe publishLibraryVariantsGroupedByFlavor is the way to tell the publisher plugin to make it this way, but I have not tried it.

这篇关于只能使用releaseImplementation和debugImplementation从Maven添加Kotlin Multiplatform Mobile库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:04