b2cloud

19th March 2014

Using local Android library packages (aar)

Tutorial By 3 years ago

There were two things that had me excited with the release of the preview version of Android Studio. The first was the move away from Eclipse to IntelliJ, and the second was Android library packages (aar).  Android library packages allow developers to package up Android libraries, resource files and all, into binary packages which can be used across multiple projects without providing source code.

There is just one problem with Android library packages , or more importantly Gradle’s current support for them, unlike jar packages, aar packages cannot be used locally which means you can’t drop an aar file into your projects libs directory and ask Gradle to use it as a dependency. Never fear though, all is not lost. Gradle is a fairly flexible build system so flexible in fact it provides a thing called a ‘flat file’ repository, which basically tells Gradle to use a library directory as a simple repository. We can use this ‘flat file’ repository to enable local loading of aar files.

A word of caution

A word of caution before I proceed, unlike jar files the order in which aar files are loaded as dependencies is important due to the way resource merging and overriding works. If you have aar files that depend on other aar files, be very careful about the order of your dependency declarations or better yet move to a library repository.

How To

The first thing you need to do is declare your flat file repository. This repository declaration goes in the gradle.build file in your project module and not the top level gradle.build file in the parent directory.

The below example assumes that you have a libs directory in your project module.

repositories{
   flatDir {
     dirs 'libs'
   }
}

The next step is to declare your dependency on the library, to do this you declare the package the same as you would if you were using it from any code repository. The below code will load a file named ‘library-1.0.0.aar’ from the project modules libs directory.

dependencies {
   compile 'com.example.library:library:1.0.0@aar'
}

The namespace section (com.example.library) of the dependency declaration is ignored by the ‘flat file’ repository, while having an incorrect namespace in the dependency declaration doesn’t effect anything you should ensure the namespace is correct incase you move to a library repository at a later date.

 

  • Joost Funke Kupper

    This works great! Thanks Dwayne.

    Found that you can also add the dependency via “compile name:’filename’, ext:’war'”. That way you don’t have to maintain the version.

  • DwayneHoy

    Hey Joost, glad you found it useful! The main reason I like to maintain the library version is because it’s easier to migrate over to a Maven repository in the future.

  • Vivek Adhikari

    Thanks. Now,I have added .aar like this, but my .aar contains few dependencies, like:

    compile ‘com.xxxxxxxxxxx:1.0.18’
    compile ‘com.xxxxxxxxxxxxxx:1.4.6’
    compile ‘com.xxxxxxxxxxxxxxxxx:1.0.3’
    But my project contains few dependency which is conflicting with these.
    Is there any other place i can put these dependencies, instead of project.gradle?

Recommended Posts

Write JNI wrappers for Android

Post by 3 years ago

Following are the steps that you need to follow in order to make things working – 1. Setting up the NDK on my machine 2. Learn how to write JNI wrappers 3. Learn C++ briefly so

Connect With Us

We're trusted by some of the largest businesses and enterprises to build digital products that matter.