Monday, September 28, 2015

How to create and use an Android Archive (*.aar) using Android Studio

Besides a plain old Java archive file (*.jar), Android Studio can be used to create an Android archive file (*.aar) that can contain classes and methods that make use of Android classes and related files. Like creating the Jar file, an Android project must be created first, then the Android library module can be created and added. The following steps illustrate the process in creating a Android archive file.

Generating the Android archive (*.aar) file

  1. Start up Android Studio.
  2.  Select Start a new Android Studio project.

    The Create New Project dialog box appears.
  3. Type in an Application name and a Company Domain. Select a Project location. Click Next.

    The Target Android Device page appears.
  4. Choose a Minimum SDK, e.g. API 14. Click Next.

    The Add an activity to Mobile page appears.
  5. Select Add No Activity. Click Finish.

    The Android project is created.
  6. Select File | New | New Module.

    The Create New Module dialog box appears.

  7. Select Android library. Click Next.

    The Configure your new module page appears.

  8. In the Application/Library name field, type in e.g. MyAarLibrary.
  9. In the Module name field, type in e.g. myaarlibrary.
  10. If necessary, edit the Package name and/or change the Minimum SDK.
  11. Click Next.

    The myaarlibrary library is created.
  12. In the newly created myaarlibrary module node, create the classes and methods of the library in the editor, e.g. LibraryClass class with a printLog method.

    Note: the example printLog uses the Android.Util.Log class to print a debug log message.

  13. In the Project tree view, select the newly created module node, e.g. myaarlibrary. Then open the Gradle projects pane on the right side. Expand the Tasks node, as shown below.

  14. Double click on assembleRelease.

    The Android archive file is generated, e.g. myaarlibrary-release.aar.

Using the Android archive (*.aar) file
  1. In Android Studio, create a new project or open up an existing project that you want to use the Android archive file library.
  2. In the Project view, expand the libs node of the app.
  3. Using the Mac Finder or Windows Explorer, select and copy the Android archive file (*.aar) e.g. myaarlibrary.aar. Paste the aar file into the libs node in Android Studio.

    The Copy dialog box appears.

  4.  If necessary, change the name. Click OK.

    The Aar file is added to the project.
  5. In Android Studio, open the app's build.gradle file in the editor.

  6. Add in the following line to the dependencies section.

    implementation files ( 'libs/myaarlibrary-release.aar')

    Now the classes and methods in the Android archive file can be used in your app, as shown in the example below.


jp said...

Works perfectly. Thanks.
PS : nice profile picture :p

Unknown said...

Perfect for me. No trick left behind. good job :)

Lucas Daniel said...

Thank you!
Very good tutorial

Kameswari said...

Thanks for the detailed explanation. Can we create resource files like layouts, styles,...etc and reuse them with this?

Anonymous said...

I feel compelled to tell you how happy I am to have found this blog - without the gradle instructions and descriptions, nowhere does it say ON THE ENTIRE INTERNET how you're supposed to get an AAR in a library. Thank you so much!

(There are significant steps missing from this page:

riChArDfu said...

Great tutorial, been finding this for a while and finally got this easy and clear instruction.

Unknown said...

anyone would like to please help me here .. THANX

Hemu said...

Great post. Can you tell me how to include multiple aar files?

Unknown said...

I am fresher for android, So pls tell me how to build assemblyRelease and no any output folder shown in my project. actually, I am a xamarin developer and I want to convert aar file to dll.

Anonymous said...

It's very useful for beginners.

Eli said...

Thanks a lot for the detailed explanation.
it was very helpful for me.
nice job :)

Pranish said...

Thank you for the tutorial. One thing I want to know is how to mention the dependencies that my sdk project uses so that they get downloaded whenever someone imports my sdk?

jobyabraham1983 said...

I have a custom library(AAR) and after integration with the target app, it reveal whole classes to integrated app. How can we prevent it. lets say Class A, B, C,...etc all these are inter dependent
... but I want integrator should see only Class A implementation details. How can I hide remaining classes? Other thing is I have native code.

dominoc925 said...

@jobyabraham1983, have you tried to obfuscate the classes in your library using Proguard? I haven't done it myself.

jobyabraham1983 said...

I am using "minifyEnabled true" in Gradle and "-keep public class com.package.class** { *; }" with proguard file. But its throws class not found exception. Is it like after obfuscate that class object will not available for the other library classes?

Unknown said...

very helpful thank you.