How to publish an Android library to Maven

A walk through of the Gradle config required to publish a Android library to Maven Central using https://oss.sonatype.org.

The build.gradle

Add the following lines bellow apply plugin: 'com.android.library'.

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

Add artifact info for Maven.

group = "com.example.mylib"
archivesBaseName = "mylib"
version = "1.0"

Add archive configuration by extending from the default one.

configurations {
    archives {
        extendsFrom configurations.default
    }
}

Add signing configs. More on this later.

signing {
    sign configurations.archives
}

Add deployment config.

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                name 'AboutIt'
                packaging 'jar'
                // optionally artifactId can be defined here
                description 'A About-page creator'
                url 'http://www.example.com/example-application'

                scm {
                    connection 'scm:git:https://github.com/victorhaggqvist/AboutIt.git'
                    developerConnection 'scm:git:https://github.com/victorhaggqvist/AboutIt.git'
                    url 'https://github.com/victorhaggqvist/AboutIt'
                }

                licenses {
                    license {
                        name 'The Apache License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }

                developers {
                    developer {
                        id 'atriix'
                        name 'Victor Häggqvist'
                        email '[email protected]'
                    }
                }
            }
        }
    }
}

Signing and GPG

Back to the signing config. Artifacts are signed using GPG, that is not your regular Android keys. You will need to create yourself GPG keys for publishing to Maven.

The config is read from ~/gradle/gradle.properties. Add your keyinfo and Jira login config like this.

signing.keyId=YourKeyId  // something like WERJFK23
signing.password=YourPublicKeyPassword
signing.secretKeyRingFile=/path/to/my/secret/key/file.gpg

ossrhUsername=your-jira-id
ossrhPassword=your-jira-password

You may find your keyId by

$ gpg --list-packets keyfile.gpg

That key will also need to be published to a keyserver in order to actually release the package. You may publish your keys to MIT PGP Public Key Server like this.

$ gpg --keyserver pgp.mit.edu --send-keys keyid

Deploy

Now to actually upload this to staging.

$ gradle uploadArchives

Or you may do the same thing from Android Studio by running the Gradle task uploadArchives.

Once deployed follow this steps to release the deployment, http://central.sonatype.org/pages/releasing-the-deployment.html.

If you like to release with a command instead you can use a plugin called nexus-workflow. To use it, edit the root build.gradle in the project and add classpath 'com.adaptc.gradle:nexus-workflow:0.6' to dependencies. This should give something like this.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.adaptc.gradle:nexus-workflow:0.6'
    }
}

Now add some more properties in ~/gradle/gradle.properties. Edit username and password.

oss-releases.username=mySonatypeUsername
oss-releases.password=mySonatypePassword
oss-releases.url=https://oss.sonatype.org/index.html#stagingRepositories

In the artifacts build.gradle you then add apply plugin: 'nexus-workflow'. Now to release your deployment you run.

$ gradle nexusStagingRelease

Complete files

The complete files now look like this.

project/atrifact/build.gradle

apply plugin: 'com.android.library'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'nexus-workflow'

group = "com.snilius"
archivesBaseName = "aboutit"
version = "1.0.1"

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 2
        versionName version
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
}

configurations {
    archives {
        extendsFrom configurations.default
    }
}

signing {
    sign configurations.archives
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                name 'AboutIt'
                packaging 'jar'
                // optionally artifactId can be defined here
                description 'A About-page creator'
                url 'https://github.com/victorhaggqvist/AboutIt'

                scm {
                    connection 'scm:git:https://github.com/victorhaggqvist/AboutIt.git'
                    developerConnection 'scm:git:https://github.com/victorhaggqvist/AboutIt.git'
                    url 'https://github.com/victorhaggqvist/AboutIt'
                }

                licenses {
                    license {
                        name 'The Apache License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }

                developers {
                    developer {
                        id 'atriix'
                        name 'Victor Häggqvist'
                        email '[email protected]'
                    }
                }
            }
        }
    }
}

project/build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.adaptc.gradle:nexus-workflow:0.6'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Victor Häggqvist

Stockholm, Sweden https://victorhaggqvist.com