Last Updated:

Accelerating Gradle Build in Android Studio: 9 Ways to Increase Build Speed

Accelerating Gradle Build in Android Studio

In this article, we will look at the build system of Android Gradle projects. We will also find out why it is so slow and consider 9 ways to speed up the build of the Gradle project in Android Studio.

What is Gradle?

Gradle is a system for building projects. It is the next step in the evolution of build tools based on the Java Virtual Machine (JVM). Gradle incorporates the best ideas of existing tools (Ant and Maven), and also added more flexibility and freedom in the build configuration.

In addition to the advantages, the new approach brings new problems: even the simplest Gradle call is quite slow. On a mid-range laptop, running a simple task with a list of available project tasks takes at least 10 seconds. The official IDE for Android application development is Android Studio and uses Gradle.

 

Android projects are becoming more complex, the modular development approach is gaining popularity, so performance in the new realities is crucial. Increasing the speed of building a project by just a few seconds gives a good increase in productivity.

How to increase the speed of building a project

Below I have collected the methods that I use when building a Gradle project:

1. Make sure you always use the latest version of Gradle.

Typically, with each new update, there is a significant performance improvement. If you put in time the new versions and updates that Android Studio offers immediately after downloading, then there should be no problems with the outdated version of Gradle.

2. Try to minimize the use of modules

Sometimes you have to fork a library to tweak something or change it to suit your needs. Avoid such situations, as building a module takes ~4 times longer than any jar or dependency. This is because the module must be rebuilt from scratch each time.aar

3. Enable Gradle offline operation

You can put a tick near the point of autonomous operation by the way: . This will prevent Gradle from accessing the network at build time and will force it to work with project dependencies in its cache.Preferences => Build, Execution, Deployment => Build Tools => Gradle

Note that this feature only works correctly when all dependencies have already been successfully loaded and cached. If you need to change or add a new dependency in the file, you will have to disable the offline option - otherwise the "build will collapse".build.gradle

4. Use Gradle Daemon

Open the file in the root of your project and add the following line of code:gradle.properties

org.gradle.daemon=true

This saves you the time it takes to start Gradle, as it becomes a background process. It should be noted that after adding this option, Gradle will consume even more RAM when building the project.

5. Use parallelization of the build process

org.gradle.parallel=true

This line allows you to compile multiple modules at the same time. Note that this will only give a performance boost if you have a complex project with many modules.

6. Optimize project configuration before building

Add an option and Gradle will be able to validate changes at the project level and reuse the configuration.org.gradle.configureondemand=true

Gradle configures each project before performing tasks, regardless of whether it is really needed for a particular build. The On-Demand Configuration mode changes the default behavior. As in parallel mode, this option will have the greatest effect in projects with many modules.

7. Configure the project compilation options

Replace the default memory usage settings with the following:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

 

This will allow the Java compiler to use up to 2GB (-Xmx2048m option) of available RAM. Of course, this can only be done if you have more than 2 GB of available memory.

8. Avoid dynamic dependencies

Example: compile 'com.android.support:design:+'

This plus (+) at the end tells us that a dynamic dependence is being used.

Dynamic dependencies slow down the build, as the search for the latest version occurs with each build. To increase efficiency, we need to clearly fix the version.

9. Use only the dependencies you need

Always add the most precisely defined dependency to your project. For example, we only need maps from Google in the project. Instead of writing like this:

compile 'com.google.android.gms:play-services:10.0.1'

should always be written like this:

compile 'com.google.android.gms:play-services-maps:10.0.1'.

Hopefully, these ways to streamline the build process will save you a couple of hours of development. 🙂

 

And what ways do you use to speed up Gradle in your projects?

 

Subscribe to new articles on Android development and Java programming!