Gradle Improvement
How to speed up Android app build time
3 simple rules to greatly improve your productivity
Whatever the size of the Android project you work on, the build time can seem awfully long at each build.
Google teams did a great job of reducing this time in the past few years, but a build will always take too much time for us Android developers.
As Android developers, we can significantly impact our project build time, in a good or bad way.
If you’re fed up of getting too muscular by doing push-ups during each build, waiting for the end, the following advice can help you:
TL;DR
- Don’t have dependency’s dynamic version (ex:
androidx.activity:activity-ktx:1.+)
. Always use a specific version. - Correctly order repositories. They must be ordered from the repository with the more dependencies to the one with the less.
- If you use repositories that contain only one or a few dependencies you need, indicate it to avoid contacting the repository for a dependency you know it doesn’t have.
Don’t use dynamic version
In 2022, all Android developers should know that: we should not define dependencies like this androidx.activity:activity-ktx:1.+
.
Firstly, the used library version can change if a new version is released without a voluntary action on your part.
Secondly, it causes Gradle to make additional network calls to fetch the latest dependency versions, slowing down the build.
Correctly order your repositories
When looking for a dependency, Gradle will contact repositories in the order they are defined.
If you put first a repository that contains only a small number of your dependencies, you will have a lot of fail requests, slowing down the build.
You have to order your repositories from the one with the more dependencies to the one with the less.
Define which dependency is in a specific repository
If you use repositories that contain only one or a few dependencies you need, explicitly list these dependencies to avoid contacting the repository for a dependency you know it doesn’t have.
To do this, add a bloc content
and define the dependency group with the function includeGroup
next to the repository URL definition.
With this code, the repository will only be contacted to get dependency for the groupes com.verizon.ad
or com.yahoo.ads
or com.yahoo.mobile.ads
.
Conclusion
This article focused on dependency version declaration and how to manage repositories to speed up Android app build time.
Several other things can impact the build time that are not covered in this article:
- Don’t use
buildSrc
butcomposite build
instead. You can find an explanation in this medium article. - Enable
configuration cache
by puttingorg.gradle.unsafe.configuration-cache=true
in your projectgradle.properties
. You can find Gradle documentation here.