You’ve built an awesome library, and now you want to share it with the world! Publishing to Maven Central makes your library easily accessible to developers through Gradle, Maven, and other build tools.
This guide walks you through the process of publishing your Android library to Maven Central using both Groovy and Kotlin DSL in your Gradle builds.
1. Set Up Your Sonatype Account
Maven Central is hosted by Sonatype. You’ll need to create a Sonatype account and a JIRA ticket to request permission to publish your library.
- Create an Account: Head over to Sonatype’s JIRA and sign up.
- Create a JIRA Ticket: After logging in, create a new issue in the “Community Support – Open Source Project Repository Hosting (OSSRH)” project.
- Request access to publish your library.
- Provide your Group ID (usually your domain name in reverse, e.g.,
com.example
). - Sonatype will review your request and grant you access.
2. Prepare Your Library
build.gradle
(Groovy) orbuild.gradle.kts
(Kotlin)- Apply Plugins:
plugins { id 'maven-publish' id 'signing' // For signing your artifacts }
- Project Information:
group = "com.yourdomain.yourlibrary" // Replace with your Group ID version = "1.0.0" // Your library's version android { //... your Android configurations }
- Publishing Configuration:
afterEvaluate { publishing { publications { release(MavenPublication) { from components.release groupId = "com.yourdomain.yourlibrary" artifactId = 'your-library-name' // Replace with your artifact ID version = '1.0.0' pom { name = 'Your Library Name' description = 'A description of your library' url = 'https://github.com/your-github/your-library' // Your project URL licenses { license { name = 'The Apache License, Version 2.0' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id = 'your-github-username' name = 'Your Name' email = 'your.email@example.com' } } scm { connection = 'scm:git:github.com/your-github/your-library.git' developerConnection = 'scm:git:ssh://github.com/your-github/your-library.git' url = 'https://github.com/your-github/your-library' } } } } repositories { maven { name = "sonatype" url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" credentials { username = System.getenv("OSSRH_USERNAME") // Set these environment variables password = System.getenv("OSSRH_PASSWORD") } } } } } signing { sign publishing.publications.release }
- Kotlin DSL (
build.gradle.kts
)//... (Similar to Groovy, but with Kotlin syntax) publishing { publications { create<MavenPublication>("release") { //... (configure publication as above) } } //... (repositories and signing) }
- Apply Plugins:
- Generate a GPG Key: You’ll need a GPG key to sign your artifacts. Follow the instructions in the Maven Central Guide to generate one.
- Upload Your Public Key: Upload your public key to a keyserver (e.g., keyserver.ubuntu.com).
3. Publish Your Library
- Set Environment Variables: Set the
OSSRH_USERNAME
andOSSRH_PASSWORD
environment variables with your Sonatype credentials. - Run Gradle Tasks:
./gradlew publishReleasePublicationToSonatypeRepository
(or the equivalent for your setup)- This will build your library, sign it, and upload it to Sonatype’s staging repository.
4. Release to Maven Central
- Close and Release: Log in to the Sonatype Nexus Repository Manager.
- Find your staged repository.
- Close it (this performs some checks).
- Release it.
- Sync to Maven Central: It may take a few hours for your library to appear in Maven Central after it’s released from the staging repository.
Congratulations! You’ve successfully published your Android library to Maven Central. Now, developers around the world can easily include it in their projects.
Important Notes:
- Documentation: Include clear documentation (README, Javadocs, etc.) with your library.
- Versioning: Follow semantic versioning principles (major.minor.patch).
- Testing: Thoroughly test your library before publishing.
By following these steps, you can share your Android library with the world and contribute to the vibrant open-source ecosystem. Happy publishing!
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.