Vandal Software

  • Archive
  • RSS

Publishing an Android Library (AAR) to a Maven Repository

This article explains how to set up an Android Library project to publish an AAR artifact to a Maven repository, specifically, Sonatype OSS.

An AAR artifact is simply a Zip archive that contains the library project’s classes in a Jar file, its resource definitions, its AndroidManifest.xml file, as well as some ancillary files like renderscript.

Set up the Android plugin

First, configure your build script dependencies to import the android gradle build tool into your classpath.

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

Now, apply the android-library plugin.

This plugin is distinct from the android plugin in that it only defines two build types: debug, which is used for instrumenting tests with the check tasks; and release, which will be used by projects that depend on this library. Let’s go ahead and configure the plugin.

build.gradle

apply plugin: 'android-library'

android {
    compileSdkVersion 17
    buildToolsVersion '17.0.0'
}

Publishing artifacts

To publish, declare the archives configuration, a unique group and version so that your artifacts can be identified, and finally the artifact signing and its requirement.

build.gradle

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

version = "1.0.0"
group = "com.vandalsoftware"

configurations {
    archives {
        extendsFrom configurations.default
    }
}

signing {
    required { has("release") && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

Note that signing is only required in this setup when a release version is built and the uploadArchives task is used.

Finally, we will configure the uploadArchives task.

build.gradle

uploadArchives {
    configuration = configurations.archives
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

        repository(url: sonatypeRepo) {
            authentication(userName: sonatypeUsername,
                    password: sonatypePassword)
        }

        pom.project {
            name 'Android Cache Library'
            packaging 'aar'
            description 'Cache library for Android applications'
            url 'https://github.com/VandalSoftware/android-cache-lib'

            scm {
                url 'scm:git@github.com:VandalSoftware/android-cache-lib.git'
                connection 'scm:git@github.com:VandalSoftware/android-cache-lib.git'
                developerConnection 'scm:git@github.com:VandalSoftware/android-cache-lib.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 'jle'
                    name 'Jonathan Le'
                    email 'jonathanle@gmail.com'
                }
            }
        }
    }
}

With that you will have configured the very basic setup that Sonatype OSS requires. There are, however, a couple of things to notice:

  1. The packaging type is declared as aar instead of jar.
  2. The POM file is declared within the mavenDeployer property.

Environment configuration

Authentication is provided by gradle properties, which I’ve declared in my home directory:

~/.gradle/gradle.properties

signing.keyId=94B86DB8
signing.password=PrivateKeyPassword
signing.secretKeyRingFile=/path/to/gpg/secring.gpg

    sonatypeRepo=https://oss.sonatype.org/service/local/staging/deploy/maven2/
sonatypeUsername=YourSonatypeJiraUsername
sonatypePassword=YourSonatypeJiraPassword

Upload the artifacts

Run the build using the uploadArchives task.

$ gradle uploadArchives

If successful, you should see something similar to this:

:cache-library:mergeReleaseProguardFiles UP-TO-DATE
:cache-library:packageReleaseAidl UP-TO-DATE
:cache-library:prepareReleaseDependencies
:cache-library:compileReleaseAidl UP-TO-DATE
:cache-library:generateReleaseBuildConfig UP-TO-DATE
:cache-library:mergeReleaseAssets UP-TO-DATE
:cache-library:compileReleaseRenderscript UP-TO-DATE
:cache-library:mergeReleaseResources UP-TO-DATE
:cache-library:processReleaseManifest UP-TO-DATE
:cache-library:processReleaseResources UP-TO-DATE
:cache-library:compileRelease UP-TO-DATE
:cache-library:processReleaseJavaRes UP-TO-DATE
:cache-library:packageReleaseJar UP-TO-DATE
:cache-library:packageReleaseLocalJar UP-TO-DATE
:cache-library:packageReleaseRenderscript UP-TO-DATE
:cache-library:bundleRelease UP-TO-DATE
:cache-library:signArchives UP-TO-DATE
:cache-library:uploadArchives
Uploading: com/vandalsoftware/android/cache-library/1.0.0/cache-library-1.0.0.aar to repository remote at https://oss.sonatype.org/service/local/staging/deploy/maven2/
Transferring 17K from remote
Uploaded 17K
Uploading: com/vandalsoftware/android/cache-library/1.0.0/cache-library-1.0.0.aar.asc to repository remote at https://oss.sonatype.org/service/local/staging/deploy/maven2/
Transferring 0K from remote
Uploaded 0K
Uploading: com/vandalsoftware/android/cache-library/1.0.0/cache-library-1.0.0.pom.asc to repository remote at https://oss.sonatype.org/service/local/staging/deploy/maven2/
Transferring 0K from remote
Uploaded 0K
:example:uploadArchives

BUILD SUCCESSFUL

Total time: 15.449 secs

Adding a dependency

Publishing the AAR allows a dependency on your library to be added by an app project.

build.gradle

apply plugin: 'android'

dependencies {
    compile 'com.vandalsoftware.android:android-cache-lib:1.0.0'
}

Resources

  • For more information on setting up your Gradle project to publish Java jars to Sonatype OSS, please see this excellent article by Yennick Trevels.
  • Sonatype OSS Maven Repository Usage Guide
  • Android Gradle Plugin User Guide
    • #android
    • #maven
    • #android library
    • #aar
  • jonathanqle-deactivated20150919 Avatar Posted by jonathanqle-deactivated20150919
  • 2 years ago
  • 6
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

6 Notes/ Hide

  1. saltfactorystory liked this
  2. avinashkolluru reblogged this from vandalblog
  3. markrymedia liked this
  4. kyungw00k reblogged this from vandalblog and added:
    Gradle로 만들어진 Closed Source Library인 AAR을 Maven에 Deploy 하는 법. (Gradle에서 Local AAR를 참조할 수 있다면 좋을텐데.)
  5. vandalblog posted this
← Previous • Next →

Follow us

  • @vandalsoftware on Twitter
  • VandalSoftware on github

Group members

  • vandalsoftware
  • RSS
  • Random
  • Archive
  • Mobile

Vandal Software © 2013.