Create an Android jar library with gradle using AAR info

This post describes how to create Android jar library.Some posts ago, I talked about how to use gradle to push an aar to maven central. If you remember, we had to modify some files and so on, but the work we have to do helps other developers to simplify their development when they want to use our code/library. When our code is pushed to the maven central as aar we can resuse it as libray simply setting a gradle dependency.
For others IDE or for those does not use gradle as build enviroment system, it is necessary to distribute the library as a JAR so that it can be included in the projects.

Create Android jar library with gradle

For example, if we want to use Weatherlib we have to write:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ''
    compile 'com.survivingwithandroid:weatherlib:1.3.1'

very simple!
Anyway this is true if we use Android Studio, but what if we use Eclipse or something else. In some cases it is easier to have a classic jar that can be imported in our Eclipse project and add a dependency in our project to this jar.
If we use Android Studio this jar isn’t created easily (AFAIK) and I had to slightly modify the build.gradle to create the jar. I looked around on the net and I found a solution that I re-adapted so that we can reuse the information stored in the properties file. If you remember from the post about aar and gradle (if not look here), there are two properties files that I show it for simplicity:

POM_NAME=Android Weather Library



POM_DESCRIPTION=Android Weather Lib
POM_LICENCE_NAME=The Apache Software License, Version 2.0

So I would like to use this information to create a jar with the name equals to the POM_ARTIFACT_ID combined with the VERSION_NAME, and this jar should be created under a specific directory. So we have to add under android section in build.gradle:

android {

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            resources {
                srcDir 'src/../lib'

and after the dependencies section:

task clearJar(type: Delete) {
    delete 'build/libs/' + POM_ARTIFACT_ID + '_' + VERSION_NAME + '.jar'

task makeJar(type: Copy) {
    rename ('classes.jar', POM_ARTIFACT_ID + '_' +  VERSION_NAME + '.jar')

makeJar.dependsOn(clearJar, build)

Now if you run the task makeJar, AS will create a jar under the directory called release.

If you want to have the build.gradle file you can get it here


More posts...

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.