Generating the Android archive (*.aar) file
- Start up Android Studio.
- Select Start a new Android Studio project.
The Create New Project dialog box appears.
- Type in an Application name and a Company Domain. Select a Project location. Click Next.
The Target Android Device page appears.
- Choose a Minimum SDK, e.g. API 14. Click Next.
The Add an activity to Mobile page appears.
- Select Add No Activity. Click Finish.
The Android project is created.
- Select File | New | New Module.
The Create New Module dialog box appears.
- Select Android library. Click Next.
The Configure your new module page appears.
- In the Application/Library name field, type in e.g. MyAarLibrary.
- In the Module name field, type in e.g. myaarlibrary.
- If necessary, edit the Package name and/or change the Minimum SDK.
- Click Next.
The myaarlibrary library is created.
- 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.
- 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.
- Double click on assembleRelease.
The Android archive file is generated, e.g. myaarlibrary-release.aar.
Using the Android archive (*.aar) file
- In Android Studio, create a new project or open up an existing project that you want to use the Android archive file library.
- In the Project view, expand the libs node of the app.
- 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.
- If necessary, change the name. Click OK.
The Aar file is added to the project.
- In Android Studio, open the app's build.gradle file in the editor.
- As shown above, add in the repositories section.
- Add in the following line to the dependencies section.
compile (name: 'myaarlibrary-release', ext:'aar')
Now the classes and methods in the Android archive file can be used in your app, as shown in the example below.