Scalable Android Apps using Libraries Approach

Kapil Vij
3 min readOct 13, 2021

--

There are lot of ways to structure code base of scalable products.
One is conventional which i think 70% of projects follows that is everything under same ‘app’ module and segregating in different packages.

Others follow Multi Module Structure.
A multi-project build in Gradle consists of one root project, and one or more subprojects.

A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app:

You can have any number of library(‘lib’ as shown in above image) modules

There are some disadvantages of above approach:
- Multiple apps can’t consume same module unless copied.
- Other way could be managing in same Repo using Product Flavors.

Better way could keeping these libraries in different repositories and then creating aar files to consume in all the required projects.

File extension of end product of Android app is .apk(Android Package), and of library project is .aar(Android Archive).

There are two ways to use these aar files now in order to consume.

  • Manually copying generated aar file and copy it in libs folder of consuming app.
    - But issue with this approach is that everytime you make changes to library, you have to ‘Invalidate cache and restart’ Android Studio as there would be No versioning.
  • Second approach is by publishing aar file to Gradle repository and then consuming it using dependencies. It’s a focus for this blog as well

By now we understood what is the need to library project and how its helpful in creating scalable product codebase.

We have two options in terms of gradle Repositories:

  • Using Public Gradle Repos, like MavenCentral or google or jCenter
  • Using Private Repositories like jitpack etc. For more information on it go through this link.

Now lets see who to publish those in private repositories.

Step 1. Add credentials for gradle repo in gradle.properties, we will refer the same everywhere in project.

. gradleArtifactURL= https://<service_provider>/<org_name>/_packaging/<feed_name>/maven/. gradleArtifactUserName=SDK_ARTIFACT. gradleArtifactPassToken= ghdgkjsj3oolo2o6je2gd6

You will get above details from private repo providers such as jitpack. username and PassToken provides authenticated access to your libraries.

Step 2. Update root level build.gradle using above credentials and URL

Step 3. In module level build.gradle

Add apply plugin on top of file

apply plugin: 'maven-publish'

Step 4. In Same module level build.gradle

Step 5. We are all set with the code. Lets Publish it now.

gradlew clean build publish

Just make sure you update versionName everytime before running this command.

Woah.. MyFirst_SDK is Published

Let’s see how to Consume our SDK now.

This is way simple now.

Step 1 and Step 2 are exactly same even for consuming apps. Please refer above.

Step 3. Consume it as we do for any gradle dependency.

api "com.example.myfirst_sdk:FirstSdk:$rootProject.versionName"

Step 4. Sync Project and you are done. :)

--

--