How much time does it take you to rebuild your Android app every time you make changes?
How much time do you think the Zomato app used to take to build for our engineering team? Here are some reference points for you to be able to take an intelligent guess –
Well, here is the typical build time it used to take –
2 minutes and 18 seconds. Let’s estimate the huge amount of productivity loss that it used to lead to. A 10 member Android engineering team – running/building the app 20 times a day post even a minor change in code, results in 10*20*2.18 = 436min = ~7 hours per day of lost productivity; every day simply waiting for builds. That’s huge, and wasn’t cool until we decided to do something about it.
Slow build times make developers avoid testing on the fly, which resulted in a less stable app for our end users.
Keeping build times low is important for an engineering team’s fulfilment at work; it also indirectly leads to better, more stable products, leading to higher user satisfaction.
The first step is to run gradle assembleDebug --profile
to get a breakdown of how much time specific parts of your build take. If you want detailed results of why each step took place, and a thread-wise report of all executed tasks, use gradle assembleDebug --scan
.
1. Bottlenecks
If a project has a lot of modules, it is important to make sure the dependency graph is created well. It is also important to decide where to use implementation
and api
when creating module-to-module dependencies.
app -> api(modA) -> api(modB) -> api(modC)
then, any changes to modC
will lead to recompilation of modB
, modA
and finally, app
app -> implementation(modA) -> implementation(modB) -> implementation(modC)
then changes to modC
, will only read to modB
recompilation2. Unnecessary Work
The removal of unnecessary steps from the build is essential when making debug builds.
firebase-perf
or newrelic
plugins on debug builds. These transform your classes for performance tracking and increase Java/Kotlin compile time resConfig
to only en, xxhdpi
, which will reduce resource packaging for other dpis and languagesRemoving these steps from our debug build dropped 40s from our build. Isn’t that amazing?
3. Duplicated Work
We noticed that even without a single line of change, the tasks kotlinCompile
and javaCompile
were still running for the entire duration it usually takes in a clean build.
That made us suspect that the compiled jars are not saved for incremental builds.
The culprit? We had fields in BuildConfig.java
that were changing in every build. For example, saving the build timestamp.
Changes in BuildConfig invalidates all the incremental build jars from the last iteration, and also stops Android Instant Run from working.
Our builds are now heavily parallelised, leveraging Gradle parallel processing, since our dependency graph is properly setup now.
Making changes to deep-down library modules, that are used across the entire app in multiple modules still takes time. However, now we don’t have to wait for 2 minutes to view changes to reflect for as simple a thing as a padding of a button.
When there are only a few changes to our consumer app codebase (without any changes to inner modules), it takes only 7 seconds to build.
Not having ever-changing variables in BuildConfig.java
, in our debug builds, means that Instant Run now works and greatly improves iteration time of UI changes.
Part of our build time optimisation effort also got us up to date with the Android Gradle Build plugin version 3.3 (we were on 3.0 till now). We can now release our app as app bundles and reduce download sizes by 20% for our users.