

Create an Android Library (AAR)
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.

Create an Android Library (AAR)
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.
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
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 :)
Recommend
-
80
WIP: python-shortcuts :green_apple: + :snake: = :heart: python-shortcutsis a library to create Siri Shortcuts on...
-
47
Build a Slack App to Create and Apply Stripe Coupons in 4 Steps with Standard Library and Node.js
-
11
Flutter混合开发之打包aar并上传到nexus 今天我必须得写篇博客来记录下这“历史性”的时刻,耗时3天终于算是搞出来了。此时此刻只有一张图能形容我现在的状态。
-
3
Create an Android library An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including so...
-
6
昨天把 gradle 升级到 6.0 以后,发现这傻逼的 google 又直接干 API,以前上报的全部脚本都没法用了。我们原先上报一个 aar,是直接引入 apply plugin: 'maven' 结果新版本...
-
7
Android Studio 打包及引用 aar – Android开发中文站你的位置:Android开发中文站 > Android开发 >
-
5
Can not create a multi-user discussion room (MUC) with a support library for Android: & ldquo; Packet.DefaultPacketExtension can not be served on packet.MUCUser & rdquo; Can not create a multi-user discussion room (MUC) with...
-
6
更新于 2021/08/8 | 创建于 2021/08/8 如何在flutter插件中使用本地aar库 当你使用Golang语言编写一个android安卓的库,并且编译为android的arr格式库时,你需要...
-
8
How to Create an Android LibraryMarch 29th 2022 new story4When developing an Android application,...
-
2
Copy link Author
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK