.aar (Android Archive Library) Format Android
Purpose of AAR
Android AAR files build upon this concept to allow you to package not only the source code but also the libraries self contained Android resources. The library can have its own manifest and resources and can also optionally provide assets, NDK built libraries, proguard configuration and even its own custom lint filters. This is all bundled into a zip archive that can be published to a repository for your team (or the world!) to get easy access from a Gradle build.
- The 'aar' bundle is the binary distribution of an Android Library Project.
- The file extension is .aar, and the maven artifact type should be aar as well, but the file itself a simple zip file with the following entries:
- /AndroidManifest.xml (mandatory)
- /classes.jar (mandatory)
- /res/ (mandatory)
- /R.txt (mandatory)
- /assets/ (optional)
- /libs/*.jar (optional)
- /jni/<abi>/*.so (optional)
- /proguard.txt (optional)
- /lint.jar (optional)
These entries are directly at the root of the zip file.
The good news is creating an AAR library is easy and Android Studio will do all the hard work for you.
You have a few choices as to how to include your AAR library in your project:
repositories {
flatDir {
dirs: 'libs'
}
}
dependencies {
compile 'com.kevinpelgrims.library:library:1.0.0@aar'
}
3. Declare your AAR library as a dependency:
dependencies {
compile(name:'yourlibraryname', ext:'aar')
}
Reference Link:
http://tools.android.com/tech-docs/new-build-system/aar-format
https://androidbycode.wordpress.com/2015/02/23/building-an-aar-library-in-android-studio/
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/
Creating an AAR library project
The good news is creating an AAR library is easy and Android Studio will do all the hard work for you.- Start by selecting New Module… from the File menu
- Select Android Library as the module to be created (see Figure )
- Step through the process of using a template for the creation of your library much like creating a new project
One key difference in the created library and an Android application will be the libraries Gradle build script includes apply plugin: ‘com.android.library’ instead of apply plugin: ‘com.android.application’.
When you build your project the AAR file will automatically be assembled and placed in these folders.
| build
| outputs
| aar
| applib-debug.aar
| applib-release.aar
| build
| outputs
| aar
| applib-debug.aar
| applib-release.aar
Using AAR
You have a few choices as to how to include your AAR library in your project:- Publish to a external Maven repository
- Publish to a local repository
- Access from a local folder
While Gradle can be used to directly publish to repositories such as Maven Central, this article doesn’t cover publishing your AAR file to Maven repositories.
To include your AAR library as a dependency from a local folder:
- Copy the AAR file into the libs folder in your project. Create this folder if it doesn’t exist. It needs to be located in the same folder as your application Gradle build file, not the top level project folder.
- Declare the libs folder as a dependency repository in your application Gradle script.
repositories {
flatDir {
dirs: 'libs'
}
}
dependencies {
compile 'com.kevinpelgrims.library:library:1.0.0@aar'
}
The Gradle flatDir repository declaration allows you to declare a file system directory that is a repository for dependencies for the project. In the example above I am using the libs folder which needs to be located relative to the location of the build script. The name of this folder does not have to be libs and it is also possible to declare multiple fileDir folders in the one project.
dependencies {
compile(name:'yourlibraryname', ext:'aar')
}
AARs vs Jars:
The main difference between a Jar and a AAR is that AARs include resources such as layouts, drawables etc. This makes it a lot easier to create self-contained visual components. For example if you have multiple apps that use the same login screen, with Jars you could share classes but not the layout, styles, etc., you still had to duplicate them. With AARs everything is bundled in one neat package.
Advantages of AAR:
- Allow you to package not only the source code but also the libraries self contained Android resources.
- Allow easily distribute and use aar archive file.
- Allow to reuse the code as well as resources.
Reference Link:
http://tools.android.com/tech-docs/new-build-system/aar-format
https://androidbycode.wordpress.com/2015/02/23/building-an-aar-library-in-android-studio/
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/