paint-brush
Speed Up Gradle Build In Android Studioby@101
53,655 reads
53,655 reads

Speed Up Gradle Build In Android Studio

by Nick BabichJanuary 31st, 2016
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Ever feel like you are waiting for the builds to complete in <a href="https://hackernoon.com/tagged/agndroid" target="_blank">Android</a> Studio for minutes? Me too. And it’s a pretty annoying.

Company Mentioned

Mention Thumbnail
featured image - Speed Up Gradle Build In Android Studio
Nick Babich HackerNoon profile picture

Ever feel like you are waiting for the builds to complete in Android Studio for minutes? Me too. And it’s a pretty annoying.

Fortunately, there are a few ways that you can use to improve this. Android uses Gradle for building. The latest version is 2.10 has a huge performance boost over previous versions (see Release notes for details).

Step 1: Update Gradle version

An easier way to accomplish this is to go to: Open Module Settings (your project) > Project Structure

In Gradle version and enter 2.10

Changing the Gradle version in Project Structure dialog

Download Gradle Release distributive from https://services.gradle.org/distributions/gradle-2.10-all.zip

And copy it to the Gradle folder:

Last step is to add your discribution in Settings > Gradle

Don’t forget to click Apply to save changes**.**

Step 2: Enable Offline mode, Gradle daemon and parallel build for the project

Offline mode tells Gradle to ignore update-to-date checks. Gradle asks for dependencies everytime and having this option makes it just uses what is already on the machine for dependencies.

  1. Go to Gradle from android studio Setting and click in Offline work box.

2. Go to Compiler from android studio Setting and add “— offline” in command-line box and click Compile independent modules in parallel.

Gradle options: Compiling in parallel and Offline mode

The next step is to enable the Gradle daemon and parallel build for your project. Parallel builds will cause your projects with multiple modules (multi-project builds in Gradle) to be built in parallel, which should make large or modular projects build faster.

These settings could enabled by modifiing a file named gradle.properties in Gradle scripts directory(i.e., ~/.gradle/gradle.properties).Some of these options (e.g. Complie modules in parallel) are available from Android Studio and also enabled there by default, but putting them in the gradle.properties file will enabled them when building from the terminal and also making sure that your colleagues will use the same settings. But if you’re working on a team, sometimes you can’t commit this stuff.



# When configured, Gradle will run in incubating parallel mode.# This option should only be used with decoupled projects. More details, visitorg.gradle.parallel=true



# When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.# The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.org.gradle.daemon=true

Using the daemon will make your builds startup faster as it won’t have to start up the entire Gradle application every time. The Gradle Daemon is not enabled by default, but it’s recommend always enabling it for developers’ machines (but leaving it disabled for continuous integration servers). FAQ about this mode could be found here https://docs.gradle.org/current/userguide/gradle_daemon.html.

The parallel builds setting could be unsafe for some projects. The requirement is that all your modules must be decoupled or your build could fail (see http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects for details).

Step 3: Enable incremental dexign and tweak memory settings

You can speed up your builds by turning on incremental dexing. In your module’s build file:

Add this option to your android block:



dexOptions {incremental true}

In that dexOptions block you can also specify the heap size for the dex process, for example:




dexOptions {incremental truejavaMaxHeapSize "12g"}

Where “12g” is 12GB of memory. Additional information about this could be found here google.github.io/android-gradle-dsl/current/

You can also configure Gradle parameters in the settings file, e.g. increase the max heap size in case you have a large project:




_# Specifies the JVM arguments used for the daemon process.# The setting is particularly useful for tweaking memory settings.# Default value: -Xmx10248m -XX:MaxPermSize=256m_org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

See all list of parameters here: https://docs.gradle.org/current/userguide/userguide_single.html#sec:gradle_configuration_properties for details.

Step 4: Disable Antivirus

Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security. But if you switch between branches a lot, then antivirus will rescan files before allowing gradle process to use it, which slows build time (in particular Android Studio sync project with gradle files and indexing tasks). Measure build time and process CPU with and without antivirus enabled to see if it is related.

I hope this helps. Leave a comment if you have any question or some other tips for improving the build performance.

Thank you!