Android – Speed up Building with Android Studio and Gradle

Posted by in Software

I’ve noticed that building a small application with gradle is slow (in my case because I’m only starting with Android development and I compile stuff way too often). One of the things I found online was a way to speed up building by using the gradle daemon instead. From daemon’s documentation:

[…] We came up with several use cases where the daemon is very useful. For some workflows, the user invokes Gradle many times to execute a small number of relatively quick tasks. For example:

  • When using test driven development, where the unit tests are executed many times.
  • When developing a web application, where the application is assembled many times.
  • When discovering what a build can do, where gradle tasks is executed a number of times.

Here are the steps to do this:

Install gradle

This took me a while to figure out. Android Studio (AS) comes with its own instance of gradle (more precisely, projects have their own wrappers built-in). If you want the daemon, this is no good and you need to install it :).

You could install gradle by downloading it from the site. However, on my system (OSX) I chose the homebrew install:

You may need to take account of which version of gradle AS uses…

Configure to use the daemon

First some preparatory steps (in case the gradle configuration doesn’t exist for your user):

  1. If you don’t have it already, create a .gradle directory in your home user. Depending on OS, the location is:
    • Linux: /home/<username>/.gradle/
    • Windows: C:\Users\<username>\.gradle
    • OS X: /Users/<username>/.gradle/

    All credit for this goes to Tim Roes.

  2. Create a gradle.properties for your user (in your .gradle directory) if it doesn’t exist.

Once the properties file exists, add the following line to it:

Configure AS

I’m not sure this step is required, but I have AS configured to use my local install, not the default. My gradle settings look like this now (AS 0.8.4):

AS-gradle

Conclusion

This allowed me to compile the source quicker, shaving a few seconds off (not starting gradle every time). Everything else is up to you (e.g. a SSD always helps!).

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)