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

Get started with AAR

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Get started with AAR

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

Page 2: Get started with AAR

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

Architect of the apponsormonetization platform.

Email: [email protected]

g+: +ReneMertinsHH

www.apponsor.com

www.bitforce-it.de

Page 3: Get started with AAR

WTF is AAR?- Introduced I/O 2013

- Library project packaging format

- AAR is Android ARchive

Page 4: Get started with AAR

It‘s a simple zip

Page 5: Get started with AAR

Benefits1. Never handle complete library projects any more

2. No need to publish a library in source

Page 6: Get started with AAR

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

Page 7: Get started with AAR

For developers- reference like support lib or

play services

- Clean project structure

- Online update if remote repository

Page 8: Get started with AAR

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

Page 9: Get started with AAR

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

Page 10: Get started with AAR

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

Page 11: Get started with AAR

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

Page 12: Get started with AAR

Good to knowOr how we failed the first time

Page 13: Get started with AAR

Creating AAR is easy- Create lib project

- Call „bundleRelease“

Page 14: Get started with AAR

But now?- Place in project structure?

Fail

- Gradle include?Fail

- Gradle file dependency?Fail

Page 15: Get started with AAR

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

Page 16: Get started with AAR

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

Page 17: Get started with AAR

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

Page 18: Get started with AAR

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

Page 19: Get started with AAR

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

Page 20: Get started with AAR

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

Page 21: Get started with AAR

Tweak gradle- Add the android-maven plugin

- Apply adroid-maven and maven plugin

- Define maven meta data

- Define upload archive task

Page 22: Get started with AAR

Use gradle tasks- Call bundleRelease to create

AAR in build folder

- Call uploadArchives to upload the AAR to your locale repository

Page 23: Get started with AAR

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

Page 24: Get started with AAR

A new appCreate a simple app to test our library.

Page 25: Get started with AAR

Define dependency- Apply the maven plugin

- Define your locale repository

- Setup the dependency to the new library

Page 26: Get started with AAR

Use it

Page 27: Get started with AAR

Test

Page 28: Get started with AAR

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!

Page 29: Get started with AAR

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/

Page 30: Get started with AAR

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

Page 31: Get started with AAR

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

Page 32: Get started with AAR

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

Page 33: Get started with AAR

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

Page 34: Get started with AAR

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.

Page 35: Get started with AAR

Thank youFeel free to approach me for any question.

[email protected]

G+: ReneMertinsHH