4

Create an Android Library (AAR)

 2 years ago
source link: https://proandroiddev.com/create-an-android-library-aar-79d2338678ba
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create an Android Library (AAR)

Steps to create an Android library, blockers while creating it and how to resolve them

Photo by Iñaki del Olmo on Unsplash

As an Android developer, you should have used a lot of libraries in your apps. An Android library, also called as Android Archive, includes everything you need to build an app like source files, resource files, manifest etc. This is the reason why AARs are different from JARs. AARs can contain resource files as well other than compiled byte code. You can create your own Android library which can be used in multiple apps.

In this blog, we’re going to see how to create an Android library, some of the blockers while creating it and how to resolve them. You can learn more about libraries here in the official documentation.

You can either create a new library module or convert the existing module as library module.

Below are the steps to create a new library module:

  1. Go to File -> New Module
  2. Select Android Library and provide the details

3. Click on Finish. A new module with the given module name will be created. It’ll have the same structure as any other modules. You can add resource files and add your logic to the library module which has been created.

If you go to the module level build.gradle file of the newly added library module, you won’t be able to see the applicationId element in it. Since only application modules can define applicationId.

Also you’ll see the plugin for Android library as shown below.

You can even convert an existing app module to a library module, if you already have one.

Below are the steps to convert app module to library module:

  1. Open module level build.gradle file
  2. Remove applicationId element
  3. Replace plugin id ‘com.android.application’ with id ‘com.android.library’
  4. Sync project with Gradle files

You’re done! The structure of your module remains the same but when you build it, an AAR will be created instead of an APK.

Now you’ve a library module created. It’s time to create an AAR file out of it. To do that, you can execute the build task of Gradle.

A task is a piece of work performed by a build. It can be building a JAR or AAR, compile a class etc. Each project is made up of different tasks. you can see the list of tasks either by executing the command:

./gradlew task

Once you get list of available tasks, you can execute it using the following command:

./gradlew <taskname>

To build an AAR, you can use the command,

./gradlew build

Your AAR file will be created!

If you’re not using Gradle command line tools, you can build an AAR using the Gradle navigation tab option within Android Studio. You can open the Gradle tab by pressing shift button twice while you’re in Android Studio.

In the Gradle tab, open our library module -> tasks -> build.

One issue I faced while creating an AAR was that the tasks option was not showing under library module in the Gradle tab as shown below. The version used was Android Studio Bumblebee | 2021.1.1 Canary 12.

1*ZQ2PuRjhRpPrBTdFZ5hDXQ.png?q=20
create-an-android-library-aar-79d2338678ba

This is an experimental change introduced in Android Studio where the Gradle tasks won’t build during Gradle sync. To resolve this issue, disable the following experimental setup in settings.

File -> Settings -> Preferences -> Experimental -> do not build Gradle task list during Gradle sync. And then do a project sync.

File -> Sync Project with Gradle Files.

You’re done. Now you should be able to see tasks under your library module as shown below.

1*dGlil56l-RFqYt-j_Bormg.png?q=20
create-an-android-library-aar-79d2338678ba

Now, go ahead and build your AAR 🎊

Thanks for reading! Share your thoughts and experience in the comments. Let’s learn together !!

Happy learning and happy coding :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK