Steps to create an Android library, blockers while creating it and how to resolve them
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:
- Go to File -> New Module
- 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.
plugins { | |
id 'com.android.library' | |
... | |
} |
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:
- Open module level build.gradle file
- Remove applicationId element
- Replace plugin id ‘com.android.application’ with id ‘com.android.library’
- 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
Job Offers
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.
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.
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 🙂
Thanks to Andy Dyer.