Skip to content

Instantly share code, notes, and snippets.

@ErikHellman
Last active August 29, 2015 14:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikHellman/0678bfde870a82f182e4 to your computer and use it in GitHub Desktop.
Save ErikHellman/0678bfde870a82f182e4 to your computer and use it in GitHub Desktop.
Boosting the performance for Gradle in your Android projects

Boosting the performance for Gradle in your Android projects

Ever feel like all you do is waiting for the builds to complete in Android Studio all day? Me too. Fortunately, there are a number of improvements you can do to speed things up. Some of these are still experimental and could be unsafe, but it is probably worth a try in case you’re suffering from long build times. I’ve seen project go down to 2.5 seconds when building after small code changes using the stuff I describe below. Hope it works for you as well.

Android uses Gradle for building. The default version of Gradle at the time of writing is 2.2. The latest version is 2.4 and has a huge performance boost over previous versions. In order to make your Android project use this version, add the following at the end of your root build.grade script.

task wrapper(type: Wrapper) {
    gradleVersion = '2.4'
}

Now open a terminal and run ./gradlew wrapper and it will download and setup Gradle 2.4 for your local Gradle wrapper.

Note that this works only when using the Gradle wrapper for your project (which is the default when generating projects with Android Studio). If this is not the case, refer to the Gradle documentation at http://gradle.org.

The next step is to enable the Gradle daemon and parallel build for your project. Using the daemon will make your builds startup faster as it won't have to start up the entire Gradle application every time. 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 are enable by adding a file named gradle.properties in the root of your project with the following content:

org.gradle.daemon=true
org.gradle.parallel=true

Note that the daemon is already be enabled from within Android Studio, but this setting will make sure that it is also enabled when building from the terminal.

Important note about parallel builds: The parallel builds setting could prove unsafe for some projects. The requirement is that all your modules must be decoupled (see http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects) or your build could fail. Test all your build variants carefully so that you know that everything works as it should.

You can add additional Gradle parameters here, like increasing the max heap size in case you have a large project or specifying which JVM to use:

org.gradle.jvmargs=-Xmx768m
org.gradle.java.home=/path/to/jvm

For other configuration options you can do in gradle.properties, see http://gradle.org/docs/current/userguide/userguide_single.html#sec:gradle_configuration_properties. Some of these options are available from Android Studio, 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.

The final change is incremental dexing, which is an experimental feature that is currently disabled by default. Enabling this could cause your builds to fail (especially on consecutive runs), but I do recommend you to try it out and see if it works for you.

Add the following to the android section of your main app modules build.gradle to enable this:

dexOptions {
        incremental true
}

Hope this help. Leave a comment if you have any question or some other tips for improving the build performance. :)

@tasomaniac
Copy link

    buildTypes {
        debug {
            ext.enableCrashlytics = false
        }
    }

also helps a lot 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment