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 main difference between a Jar and an AAR is that AAR s include resources such as layouts, drawables, etc. . For example if you have multiple apps that use the same login screen, with Jar’s you could share classes but not the layout, styles, etc., you still had to duplicate them. So in this article, we are going to see what are .aar files, how to open them in Android Studio, and five different ways to create .aar files in Android Studio.
What is the .aar file in Android Studio?
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. AAR files offer the following functionality for Android applications:
- AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
- AAR files can contain C/C++ libraries for use by the app module’s C/C++ code.
In addition to JAR files, Android uses a binary distribution format called Android Archive(AAR). The .aar bundle is the binary distribution of an Android Library Project. An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code. AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
How to open .aar files in Android Studio?
- Open Android Studio
- go to Project Files view.
- Find the .aar file and double click, choose “archive” from the ‘open with’ list that pops up.
- This will open a window in android studio with all the files, including the classes, manifest, etc
Different Ways To Generate .aar Files in Android Studio
Step 1: Creating AAR
If you have already created AAR then you can skip this step and jump to generate AAR. If you haven’t created AAR then follow these steps: Navigate to File > New > New Module.
Then select “Android Library” from the options and click the next button.
Step 2: Generate AAR
Go to Gradle at the top-right pane in android studio follow the below steps. Gradle > Drop down library name > tasks > build > assemble or assemble release
AAR will be generated in build/outputs/aar/
If you have set-up your library as an Android library that is if you have done with then follow the below steps:
- Apply plugin:‘com.android.library’ statement in its build.gradle file
- This will output an .aar when it’s built.
- It will show up in the build/outputs/aar/ directory in your module’s directory.
You can create a new Android Library using File > New Module and selecting the “Android Library” type as already shown in Method 1.
If you cannot find .aar files under build/outputs/aar/ directory then type this command in terminal:
and after hitting ENTER button you should see the “Build Successful” message on the screen.
Try to simply Build > Rebuild project. AAR file should appear in ~/Source/build/outputs/aar