Month ago, my colleagues complained that our project was slow to build: it normally took 5 minutes, especially in branch switching it took about 10 minutes. Then I found that it doesn’t belong to only our project, it’s the common issue. And here are somethings I have done in our Android project to get its build faster.

Profiling

How long it takes to get your build done? Let’s add the –profile param to have the exact number. In Android Studio, go to Preferences -> Build, Execution, Deployment -> Compiler and set:

Let build. You could file a HTML file in [project_dir]/build/reports/profile/ generated with the detail time spent for each build task, like that:

I recommend you see the report for any optimisation we do in this article to see how good of each param.

Reasons

There are 2 common reasons of the slow build:

Repositories and dependencies

What does Android Studio do when you click on “Sync Gradle” (if build.gradle file is changed) or build a project? Gradle collect all the dependencies. The good news is Gradle uses cache, a folder to store all dependencies to stop going online. The bad news is if Gradle cannot find any specific dependency, it always goes online to scan the repositories (normally is mavenCenter or jcenter); so it’s really slow if your network isn’t good enough.

There are 2 reasons for Gradle cache cannot be used:

  • Dependency version isn’t fixed.

You could use + signal to let Gradle check for the latest version of a dependency, it always get Gradle online

 

  • Gradle version isn’t fixed.

Each Gradle organises the cache itself, no shared the dependency cache. If your team has some Gradle versions, it takes time to switch and download all the dependencies when you switch Gradle version.

Gradle isn’t optimised

Gradle provides some parameters to allow us optimise but they aren’t normally set in Android Studio.

Gradle is slow itself

Gradle is official used and supported by Android Team but it’s not a fastest build tool.

Solutions

The solutions to solves these issues are:

Setup the fixed Gradle and dependency versions

  • Use same Gradle version as your team settings. The good way is using Gradle wrapper instead of local Gradle by creating the gradlew, checkin the source code repository, go to Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle

  • Specific all the dependency version

Use correct Gradle params

There are some Gradle params you could miss:

  • –offline: Force Gradle using cache
  • –configure-on-demand: Let Gradle only builds the relevant projects instead of all projects
  • –daemon: Let Gradle use in-process build and cut down the initial and warm-up state
  • –parallel: Let Gradle builds your projects in parallel if it’s possible
  • –max-workers: Going with –parallel param to let Gradle know how may threads it could use

So normally command line call could be:

Mapping to Android Studio config, they could be:

Go to Preferences -> Build, Execution, Deployment -> Compiler

Go to Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle

Other settings

Other settings could help is increasing the heap size for

  • JVM

  • multiDex

Use another build tool

OK, it’s time to build your project, open the HTML report file to see how much time you saved.

During the study, I found out that there are some faster build tool than Gradle, especially Buck that is using by Facebook. On my demo, it’s super fast. But of course, it isn’t supported by Android Team and its community is very small. The idea of keeping Gradle and Buck builds in parallel is really nice to get it stable by Gradle but also fast by Buck. I will try to get back to it later.

1,600 total views, 5 views today