Get started with AAR

Preview:

DESCRIPTION

A short introduction to the new AAR library format for android app projects. Talk on Droidcon 2014

Citation preview

Get started with AAR archivesHow to create and work with the new AAR archive format using gradle and android studio

René MertinsSenior Java Architect with more then 10 years experience.

Architect of the apponsormonetization platform.

Email: r.mertins@bitforce-it.de

g+: +ReneMertinsHH

www.apponsor.com

www.bitforce-it.de

WTF is AAR?- Introduced I/O 2013

- Library project packaging format

- AAR is Android ARchive

It‘s a simple zip

Benefits1. Never handle complete library projects any more

2. No need to publish a library in source

For distributors- Provide precompiled

- Optimized and obfuscated for best performance

- Be more sure that no one changes what shouldn‘t changed

- Easer publishing and distribution over maven

For developers- reference like support lib or

play services

- Clean project structure

- Online update if remote repository

Actual situation- Full copies of lib projects in

source

- Reference by file path

- Build every time with your app

- Difficult to optimize and obfuscate

- Difficult to manage different lib versions

Actual situation- Full copies of lib projects in

source

- Reference by file path

- Build every time with your app

- Difficult to optimize and obfuscate

- Difficult to manage different lib versions

Actual situation- Full copies of lib projects in

source

- Reference by file path

- Build every time with your app

- Difficult to optimize and obfuscate

- Difficult to manage different lib versions

The ideacompile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar‚

compile 'com.jeremyfeinstein.slidingmenu.lib:slidingmenu:1.3@aar'

- Library projects are separated from app projects

- Published as desired in to AAR

- App developer only declare dependencies

Good to knowOr how we failed the first time

Creating AAR is easy- Create lib project

- Call „bundleRelease“

But now?- Place in project structure?

Fail

- Gradle include?Fail

- Gradle file dependency?Fail

The trick- Provide as maven repo like

google does

- Publish into maven repo is easy with gradle

- Reference AAR in access able repositories

- New trick, import

The trick- Provide as maven repo like

google does

- Publish into maven repo is easy with gradle

- Reference AAR in access able repositories

- New trick, import

The trick- Provide as maven repo like

google does

- Publish into maven repo is easy with gradle

- Reference AAR in access able repositories

- New trick, import

A working exampleDecide what‘s next: live coding or slides ?

Start a libraryUse the project wizard to create an initial android library project.

Add something usefulI added a static method which returns „Hello [name]! How are you?“

Tweak gradle- Add the android-maven plugin

- Apply adroid-maven and maven plugin

- Define maven meta data

- Define upload archive task

Use gradle tasks- Call bundleRelease to create

AAR in build folder

- Call uploadArchives to upload the AAR to your locale repository

reviewThat’s the repository created by gradle task „uploadArchives“

A new appCreate a simple app to test our library.

Define dependency- Apply the maven plugin

- Define your locale repository

- Setup the dependency to the new library

Use it

Test

Note

A referenced AAR library is not shown in Android Studio.

You can use the library, you get auto completion etc.

But you can it inspect it in the project structure!

For Support-Lib and some other stuff from google it‘s working.

I‘m sure it will come in future.

One point I want to address!

Hint

Martin Liersch who is well known for his gradle build system talk is providing a maven repository where he places a lot of useful library projects as AAR artifacts.

GitHub:https://github.com/Goddchen/mvn-repo

Repo URL: https://github.com/Goddchen/mvn-repo/raw/master/

Hint

The gradle project webpage includes a very good documentation.http://www.gradle.org/docs/current/userguide/userguide.html

In the maven plugin chapters is a part about uploading artifacts to repositories. Also explaining using different transports like ssh to a remote repository.http://www.gradle.org/docs/current/userguide/maven_plugin.html#

uploading_to_maven_repositories

Observe

Without the android-maven plugin this solution would be much more complicated.

You can find the project at gitHubhttps://github.com/dcendents/android-maven-plugin

Pull requests

Most off the public library projects for android can be found on github.

Nearly all are still using ADT and are not providing AAR artifacts.

But most often there is a smart guy already done the work to port to android studio and make it easy to publish an aar. So look up the pull requests for your favorite lib and you will find a fork for android studio.

Like stefanrusk did for andengine:

https://github.com/stefanrusek/AndEngine/tree/GLES2

Hint

In one of the last releases of Android Studio they added an import feature for aar packages.

When that happens? I don’t know…

You can find it over:

File -> New Module -> Import JAR or AAR package

New way to define dependency in gradle.build

artifacts.add("default", file('myedulibrary-0.1.aar'))

In 0.9.x is not working,

updated in 1.0 rc1 not tested

Hint

Inspect an aar library in your app project.

There is trick to do that.

1. Close all open files.

2. Select your app module in project view

3. switch to structure view

Then you will see your aar library expanded in the tree view.

Thank youFeel free to approach me for any question.

r.mertins@bitforce-it.de

G+: ReneMertinsHH