Introduction
This guide explains how to publish an Android library to Maven Central using the Gradle Maven Publish Plugin.
Setting Up Build Configuration
First, update your module’s build.gradle
file with the following configuration:
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import com.vanniktech.maven.publish.SonatypeHost
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
id("com.vanniktech.maven.publish") version "0.30.0"
}
mavenPublishing {
configure(
AndroidSingleVariantLibrary(
variant = "release",
sourcesJar = true,
publishJavadocJar = true,
)
)
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
signAllPublications()
coordinates("com.example.mylibrary", "mylibrary-runtime", "1.0.3-SNAPSHOT")
pom {
name.set("My Library")
description.set("A description of what my library does.")
inceptionYear.set("2020")
url.set("https://github.com/username/mylibrary/")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("username")
name.set("User Name")
url.set("https://github.com/username/")
}
}
scm {
url.set("https://github.com/username/mylibrary/")
connection.set("scm:git:git://github.com/username/mylibrary.git")
developerConnection.set("scm:git:ssh://git@github.com/username/mylibrary.git")
}
}
}
Testing with Local Maven
To test your configuration locally:
- Run the following command:
./gradlew publishToMavenLocal
- Verify the publication in your local Maven repository:
ls ~/.m2/repository/com.example.mylibrary/mylibrary-runtime
Publishing to Maven Central
Domain Verification
If you’re using your own domain, you’ll need to verify it by adding a TXT record to your domain’s DNS and check for namespace status at https://central.sonatype.com/publishing/namespaces
GPG Key Setup
- Export your secret keys at
~/.gnupg/
gpg --export-secret-keys -o secring.gpg
- List your GPG keys:
gpg --list-keys --keyid-format short
Fill this as the signing.keyId
in next step.
Configure Gradle Properties
Add the following to ~/.gradle/gradle.properties
:
mavenCentralUsername=username
mavenCentralPassword=the_password
signing.keyId=12345678
signing.password=some_password
signing.secretKeyRingFile=~/.gnupg/secring.gpg
Publishing
- Run the publish command:
./gradlew publishAllPublicationsToMavenCentral
- Monitor your deployment status at https://central.sonatype.com/publishing/deployments
Additional Configuration
If your library includes Jetpack Compose code, add this to your module’s build.gradle
:
plugins {
alias(libs.plugins.kotlin.compose) apply false
}