Publishing Android or Java Binaries to Maven Central

Mark Dappollone
4 min readJun 6, 2017

Github has pretty robust release publishing, allowing you to post binaries right to Github with a tag and description. But for libraries that are intended to be used in Android projects, you also want to publish your binaries so that users can use them without downloading the .jar or .aar from github. One popular place for hosting your dependencies is Maven Central.

Create an Account with Sonatype JIRA

Sonatype manages a Nexus that publishes to the Maven Central Repository. The first step to publishing your project binary is to create an account in their JIRA instance.

Log a Ticket to create your Group ID

You only need to do this step if the Group ID you want to publish to doesn’t already exist. If that’s the case, log a ticket to have your group id created. Here’s a sample.

Create a GPG key

In order to publish to Maven Central, your binaries need to be signed with Gnu PG. The official instructions for generating keys are here, but I find that everything is much easier if you use the GPG Suite for Mac, which adds a convenient GUI for generating keys. Once you generate your keys, make sure to note your public key id, and secret key-ring file path. Your secret key ring file will be published to

~/.gnupg/secring.gpg

To get your public key, open up the GPG Keychain tool on your Mac, and look at the “Key ID” column in the GUI.

Publishing with Gradle

gradle.properties

The first thing you need to do is add your sonatype credentials and key info to your gradle.properties file. You’ll want to keep this file local, and not merge it to your repo. Add the following lines:

signing.keyId=your_key_id
signing.password=your_key_pw
signing.secretKeyRingFile=/Users/username/.gnupg/secring.gpg
sonatypeUsername=your_un
sonatypePassword=your_pw

Plugins

Gradle provides a few plugins to support maven publishing. But because of Maven Central’s signing requirements, currently the best solution (IMO) is to use the maven plugin and the signing plugin. You’ll want to add the plugins to the build.gradle for any library module you want to publish:

apply plugin: 'maven'
apply plugin: 'signing'

GroupId and Version

This also goes in your module level build.gradle, outside any closures:

group = 'com.group'
version = "your_version"

Component Tasks

This part is going to be slightly different for Java Libraries (jar) vs Android Libraries (aar). We’ll take them one at a time:

Java Libraries

The component tasks for java libraries look like this:

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from tasks.javadoc.destinationDir
}
task sourcesJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts {
archives jar
archives javadocJar
archives sourcesJar
}
signing {
sign configurations.archives
}

Android Libraries

task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadoc/")
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from tasks.javadoc.destinationDir
}
//Creating sources with comments
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives javadocJar
archives androidSourcesJar
}

UploadArchives

The uploadArchives task is the task you’ll actually run to publish your binaries to the staging area of the Sonatype Nexus. This task is also very slightly different for jar’s vs aar’s

JAR

uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
pom.project {
name "My Awesome Artifact"
packaging 'jar'
description 'My awesome artifact does cool stuff'
artifactId "my-awesome-artifact"
url 'https://github.com/Org/awesome-artifact'
scm {
url 'scm:git@github.com:Org/awesome-artifact.git'
connection 'scm:git@github.com:Org/awesome-artifact.git'
developerConnection 'scm:git@github.com:Org/awesome-artifact.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'userId'
name 'Your Name'
}
}
}
}
}
}

AAR

The uploadArchive task for AARs is identical to the one above, except for the “packaging” field, which should be changed to ‘aar’. Run the task from the command line using:

./gradlew :module_name:uploadArchives

Promote to Release

Once your uploadArchives task successfully completes, your binaries and pom will be published to Sonatype’s staging Nexus. Login to the Nexus using your Sonatype ID and Password, and in the Sidebar under “Build Promotion” click on “Staging Repositories.” Scroll the list for your new upload. Your upload will have an identifier consisting of your group name, plus a numeric suffix. Verify that the repo is yours by clicking on it and exploring the contents in the Content Tab of the explorer window:

Once you verify that you have the right repo, to release it, first you have to close it, by clicking the “Close” button in the “Staging Repositories” tab. Closing the repo will initiate some verification steps that will take a few minutes. Refresh the page a little later to see the results.

Once your repo is closed, you should see that the “Release” button will no longer be greyed out and disabled. To release to the public repo, hit that, and you’re done.

Last Steps

Sonatype requests that you leave a comment on your Group ID JIRA ticket when you publish your first release, so make sure to do that. When that’s done, just go back to your project’s github page and update the README or release notes with the dependency info. Now you’re really done. Congrats on your new Open Source publication.

--

--

Mark Dappollone

Director, Mobile Product Engineering at Anywhere Real Estate