gradleandroid-librarygradle-kotlin-dslmaven-publish

How do I migrate my Android library's usage of the "maven-publish" and "signing" Gradle plugins from the Groovy DSL to the Kotlin DSL?


I am using the maven-publish and signing Gradle plugins in my Android library's Groovy DSL build.gradle file as follows:

plugins {
    id("maven-publish")
    id("signing")
}

android {
    publishing {
        singleVariant("release") {
            withJavadocJar()
            withSourcesJar()
        }
    }
}

publishing {
    publications {
        release(MavenPublication) {
            afterEvaluate {
                from components.release
            }

            groupId getProperty("groupId")
            artifactId getProperty("artifactId")
            version getProperty("version")
        }
    }

    repositories {
        maven {
            url "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username getProperty("sonatypeUsername")
                password getProperty("sonatypePassword")
            }
        }
    }
}

signing {
    sign publishing.publications.release
}

I have omitted the parts of my build.gradle file which are unrelated to or optional for publishing and signing.

How do I convert these declarations to the Kotlin DSL?


Solution

  • Here are the declarations converted to the Kotlin DSL:

    plugins {
        `maven-publish`
        signing
    }
    
    android {
        publishing {
            singleVariant("release") {
                withJavadocJar()
                withSourcesJar()
            }
        }
    }
    
    publishing {
        publications {
            create<MavenPublication>("release") {
                afterEvaluate {
                    from(components["release"])
                }
    
                groupId = properties["groupId"].toString()
                artifactId = properties["artifactId"].toString()
                version = properties["version"].toString()
            }
        }
    
        repositories {
            maven {
                url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
                credentials {
                    username = properties["sonatypeUsername"].toString()
                    password = properties["sonatypePassword"].toString()
                }
            }
        }
    }
    
    signing {
        sign(publishing.publications["release"])
    }
    

    Notes

    1. The declarations above work for me with version 8.6 of Gradle and version 8.3 of the Android Gradle Plugin.
    2. You can find a complete build.gradle.kts file which includes all of the declarations above here.
    3. The Android Gradle Plugin > Publish your library Android Developers documentation has Kotlin DSL examples for most of the declarations above.