Android Core

Create a jar library with gradle using AAR info

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 example, if we want to use Weatherlib we have to write:
 
 
 
 

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    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_ARTIFACT_ID=weatherlib
POM_PACKAGING=aar

and

VERSION_NAME=1.3.1
VERSION_CODE=6
GROUP=com.survivingwithandroid

POM_DESCRIPTION=Android Weather Lib
POM_URL=https://github.com/survivingwithandroid/WeatherLib
POM_SCM_URL=https://github.com/survivingwithandroid/WeatherLib
POM_SCM_CONNECTION=scm:git@github.com:survivingwithandroid/weatherlib.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:survivingwithandroid/weatherlib.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=survivingwithandroid
POM_DEVELOPER_NAME=Francesco Azzola

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) {
    from('build/bundles/release/')
    into('release/')
    include('classes.jar')
    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.
Reference: Create a jar library with gradle using AAR info from our JCG partner Francesco Azzola at the Surviving w/ Android blog.

Francesco Azzola

He's a senior software engineer with more than 15 yrs old experience in JEE architecture. He's SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. He is an android enthusiast and he has worked for long time in the mobile development field.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Brad Rhoads
Brad Rhoads
10 years ago

This doesn’t seem to work for libs with resources, such as ActionBarSherlock. I was trying this so that I could get my separate test project to compile. But apparently they don’t need to separate anymore, so going to just one project.

Back to top button