Publishing Android library to maven central
31 Aug 2021All my libraries has a script to publish the artifact to bintray
and from there auto release to mavenCentral
. But recently, Bintray
was shutdown and hence I have to publish directly to maven central repository. This is how I did it.
Requirements
- You need to have access to Sonatype OSSRH, i.e, you should be able to login to https://oss.sonatype.org. If you don’t, register for an account. This involves you creating a JIRA ticket for a new project.
- Username (
OSS_USERNAME
) and Password (OSS_PASSWORD
) used to log in to Sonatype account. - StagingProfileId (
OSS_STAGING_PROFILE_ID
). You get this by clicking onStaging Profiles
on the left pane of your Sonatype Account, and then selecting a profile in the right pane. Check the url in the address bar. It should appear something likehttps://oss.sonatype.org/#stagingProfiles;721f909fe89978
. So721f909fe89978
is your StagingProfileId. - PGP Key, specifically,
keyId
,key
andpassword
. If you are not sure how to generate PGP key pair, refer this. If you have already generated the key pair, then executegpg --list-keys
to get a list of keys in your system. Choose the one you are going to sign your artifacts.
keyId
(OSS_SIGNING_KEY_ID
) - this would be something likeCA925CD6C9E8D064FF05B4728190C4130ABA0F98
. Either use the full string or the last 8 digits0ABA0F98
.
key
(OSS_SIGNING_KEY
) - get this by executinggpg --export-secret-keys 0ABA0F98 | base64
.password
(OSS_SIGNING_PASSWORD
) - this is the passphrase that you used when generating your key pair
Project level Gradle script
- Add the
gradle-nexus-publish-plugin
to yourbuild.gradle
script at the root level.plugins { id("io.github.gradle-nexus.publish-plugin").version("1.1.0") }
- Add the publishing configuration to access your Sonatype account. Refer documentation
if you have registered your Sonatype account after 24 Feb 2021.
// Publish to Maven Central nexusPublishing { repositories { sonatype { username = System.getenv("OSS_USERNAME") password = System.getenv("OSS_PASSWORD") stagingProfileId = System.getenv("OSS_STAGING_PROFILE_ID") } } }
Module level Gradle script
- Add the below
publish.gradle
script to your Module.
apply plugin: 'maven-publish'
apply plugin: 'signing'
// If you want to publish your sources as well
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
from android.sourceSets.main.kotlin.srcDirs
}
artifacts {
archives androidSourcesJar
}
group = GROUP_ID
version = VERSION
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId GROUP_ID
artifactId ARTIFACT_ID
version VERSION
from components.release
artifact androidSourcesJar
pom {
name = ARTIFACT_ID
description = 'An android library that ...'
url = SITE_URL
licenses {
// Your licensing information
license {
name = 'The Apache Software License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'instrap'
name = 'Madrapps'
email = 'madrasappfactory@gmail.com'
}
// More developers if any...
}
scm {
connection = GIT_URL
developerConnection = GIT_URL
url = SITE_URL
}
}
}
}
}
}
signing {
useInMemoryPgpKeys(
System.getenv("OSS_SIGNING_KEY_ID"),
System.getenv("OSS_SIGNING_KEY"),
System.getenv("OSS_SIGNING_PASSWORD"),
)
sign publishing.publications
}
- Now add the below to your
build.gradle
at your module level
ext {
GROUP_ID = "com.github.madrapps" // your project id registered in Sonatype
ARTIFACT_ID = "plot" // name of your library
VERSION = "0.1.1"
SITE_URL = 'https://github.com/Madrapps/plot'
GIT_URL = 'https://github.com/Madrapps/plot.git'
}
apply from: 'publish.gradle'
Publishing your artifact
-
Add all the variables below as Environment Variables
OSS_USERNAME
,OSS_PASSWORD
,OSS_STAGING_PROFILE_ID
,OSS_SIGNING_KEY_ID
,OSS_SIGNING_KEY
andOSS_SIGNING_PASSWORD
- Build your project first using
./gradlew build
- Execute
./gradlew publishReleasePublicationToSonatypeRepository
to publish your artifact toStaging Repositories
in your Sonatype account. From here you can publish your artifact using the Sonatype UI itself (you will have toclose
and thenrelease
the repository inStaging Repositories
). Or, you can execute the below command to do that from your terminal. - Execute
./gradlew closeAndReleaseSonatypeStagingRepository
to publish from staging to production.
Working Project
For a working example, please refer to the plot android project. It also has the CI/CD configured using Github Actions. Go crazy!