The problem

Build time has always been a big issue for Android developers. In order to reduce build time, developers usually split the project into multiple modules to take advantage of the Gradle’s parallel execution feature. At Tiki, we modularize our project with the “per feature” approach, which means each feature will sit in its own module.

When developing new features or maintaining old ones, Tiki developers usually have to make changes in the :data module which causes the whole project to recompile because most of our feature modules are based upon it. The whole process would take about 5–8 minutes. The more modules/features you have, the more time the process will take to build. We call this O(n) build time.

We want to move fast, hence developing new features must be fast, which leads to the core issue that our build time has to be fast. 5–8 minutes is nowhere near fast because every day there are around 10–20 builds to be made. Wasting a total of 50–160 minutes per day is just not productive at all.

The solution

As it turns out, Gradle doesn’t need to recompile affected modules if they are not included in the project in the first place.

Say you are working on the Home Screen in :home module, you only care about the changes only on the Home Screen (because recompiling other feature modules makes no sense to you, it is just a waste of time!). For that reason, we decided to exclude other feature modules for the time being so that Gradle doesn’t have to recompile them.

The idea is simple, we will use the Gradle command line with a customized parameter to know which module we should include in that build.

./gradlew :app:installDebug -Ponly="home"

For this particular build command, only 3 modules get recompiled if something changes in the :data module.

Implementation detail

In this Github project shown below, I have made an example project to show you how this idea can be implemented. In the next section, I will discuss some of the challenges we face while trying to apply this idea into our codebase.

First and foremost, we have to make the app compiled. Since all of the feature modules are excluded from the project, :app module can’t resolve the class you referred to in the feature module. The number of references may vary depends on the codebase. For our codebase, there are only 2 references:

The activity class: we use a static method in activity class to retrieve the starter intent for navigation. To remove this reference, the most simple and straightforward way is using reflection.

Although this approach is simple to implement but is come with some reflection costs, in the example project we take another approach which leverages Dagger 2 ’s @BindOptionalOf to get rid of the cons with reflection. I won’t go any further since this relates to Dagger 2, but you can check out the source code to see how it’s done.

The activity’s dagger module: resulting from Dagger 2 best practice, each activity has the corresponding module and AppComponent in :app module references to all of these modules.

In the beginning, we try to make a stand-alone Component for each activity instead of Subcomponent from AppComponent , therefore AppComponent , no longer needs to reference the activity’s module. Refactor from Subcomponent to Component is time-consuming and we have a lot of modules to refactor. This approach would solve the problem but it will also require a lot of time (and we don’t want that!).

In the end, we come up with a simple technique called fake source, creating a fake source set to mirror the missing real source

and include these source sets corresponding to the excluded module.

Fake source compared to the real source:

This way, AppComponent now reference an empty module and compile successfully. Problem solved!

There is still one more issue. Since most all of the screens are excluded from the debug APK, how can we open the screen that we are developing?

This can easily be done with a deep link using ADB command:

adb shell am start -a android.intent.action.VIEW -d sample://sample

The result

After implementing this idea to the Tiki Android project, the build time has reduced by half. One of the bonus from this is the debug APK size become really small since all of the app’s feature is excluded, this will also save a lot of time when deploying the APK to test device.