Creating an Android Library (AAR) using modularization

Rodrigo Vianna Calixto de Oliveira
CodandoTV
Published in
4 min readApr 8, 2022

This article is English version from the Gerar biblioteca Android(AAR) utilizando modularização.

This article is complementary to “Construindo um projeto Android com multi repositórios”, resulting in a solution that I did to the Android team adopting an approach other than "monoRepo", and how we benefited from the review of our processes through CI/CD pipeline.

To make the next contents easier to understand, I’m going to designate a different accompanying name to each AAR, to discriminate them:

Centering AAR -> will center all the others AARs in a single one.

Secondary AAR -> will be created to be subsequently added to the centering AAR.

There is a limit when an Android project is created as a library: it is not possible to modularize it since the build.gradle of each project module becomes a secondary AAR individually. To solve this problem, a centering module was created, which will feature the other modules as dependencies since the AAR itself does not have any type of configuration file that could indicate which dependencies it needs. In this post, I’m going to explain a way to manually create the centering AAR with more than one secondary AAR.

There is an approach that may help, in most cases, which is called FAT AAR. It adds more than one secondary AAR to your centering AAR, therefore, there is no need to import various libraries since you can create only one containing all that is necessary; However, unfortunately, it did not work out in several instances due to conflicts from other libraries within the project.

These problems led us to create a solution:

Manually add the files to the center AAR

We manually added another secondary AAR into the pom.xml file to create a centering AAR with all libraries. But, before that, we need to do a couple of things.

For modules with “apply plugin: ‘com.android.library’” that will be the secondary AAR, their libraries should be created and published individually, so, add them to your project root build.gradle to apply these new tasks to all of these modules (cleanLibsModule, buildLibsModule, and publishLibsModule).

As we can see, a publish.gradle was added. This local .gradle will be explained further below.

Note that only the app and core modules (which will be explained later) will not have these tasks applied, so that an exclusive command for all modules generated and published can be created, therefore making, like, the pipeline use easier.

Still inside the project root build.gradle file, we need a list containing the modules to be published. It will make more sense after the core module is generated:

Centering all in a single module

Now that we can create and publish our libraries individually, we need to create a new module with “apply plugin: ‘Com.android.library” to center all these secondary AARs generated. For all that, a new module was created, named core. This module does not contain anything and will only be used as our future centering AAR.

After creating the core module, now it’s time to gather all libraries together and publish a single centering AAR. I created a publish.gradle to make the use easier since it contains the task responsible for publishing all the AARs mentioned.

If this is not a core module, the publishLibsModule task will then be used, and each secondary AAR of each module will be individually published. When it meets the condition of a core module as shown in row 12, pom-default.xml will be manually built by grabbing GROUP_ID, the version of its modules and, through the module declaration shown above, in the allDependencies.gradle file, each ID artifact corresponding to each gradle module inside pom-default.xml will be added.

View of the pom-default.xml generation:

An overview of the example explained:

After that, you will be able to make searches directly to your .gradle that is going to use these AARs as a single reference.

"br.com.a.b.c:core:1.1.9-local234"

That’s all folks!I welcome comments and suggestions. You can make them below or contact me through LinkedIn ou https://flow.page/rviannaoliveira.

--

--