tyczj
tyczj

Reputation: 73996

Can only add Kotlin Multiplatform Mobile library from maven by using releaseImplementation and 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

Example

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'

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")

The full source for this library can be found here

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

Upvotes: 6

Views: 1853

Answers (1)

esentsov
esentsov

Reputation: 6522

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

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.

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.

Upvotes: 2

Related Questions