Speed up Gradle build for Android to save your time

What would you do when you have some free time?? Some fun activity right? but what would you do when you are running an app to test the app in your device and it takes an eternity. You are tensed !!

In this blog, we will talk about how can we speed up your gradle build to run it quickly.

Let's discuss it step by step.

Avoid Legacy Multidex

Multidex slows down the build process of the app. If you are in the development phase, it's better to disable it.

To avoid using legacy multidex when building from the command line, set minSdkVersion to 21 or higher. When using Android Studio 2.3 or higher, the build automatically avoids legacy multidex when deploying to a device running API level 21 or higher—regardless of what you set as your minSdkVersion.

Turn Off Multiple APK Build

While deploying an APK on production, we generally upload multiple APK. But while in the development phase, just run the configuration to test the app.

To do it, use the following in the project

if (project.hasProperty('devBuild')) {
    splits.abi.enable = false
    splits.density.enable = false
}
You can also set this using Android Studio. For this, open Preferences -> Build, Execution, Deployment -> Compiler settings in Android Studio and add -PdevBuild to the Command-line options.

Gradle Version

First things first, your project should be updated with the latest version of gradle. Currently, we have Gradle 5.0 Version and it reduces the build time by a lot.

Required Resources to run your APK

While being in the development phase, we can optimize the apk to run on a selected resource configuration and specific screen size. So, what if we want to reduce the time, we need to add the following,

productFlavors {
  development {
    minSdkVersion 21
    resConfigs (“en”, “hdpi”)
  }
}

Here, in the above configuration, the app will run with HDPI Screensize and with the English resource.

This optimizes the APK to run just for only one configuration.

Managing your dependencies

We should not use dynamic dependency like,

implementation 'com.github.MindorksOpenSource:screenshot:v0.+'

But we should use it like,

implementation 'com.github.MindorksOpenSource:screenshot:v0.0.1'

Because as we put up a dynamic versioning , it takes Gradle to check for the new version in every 24 hours. So, we recommend not to use dynamic versioning.

Disable Crashlytics

By disabling Crashlytics in development build, we improve the build time. To do it,

android {
  ...
  buildTypes {
    debug {
      ext.enableCrashlytics = false
    }
}

and in the code we disable it for the debug mode,

Crashlytics.Builder()
.core(CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
        .build()
        .also { crashlyticsKit ->
            Fabric.with(this, crashlyticsKit)
        }

Crashlytics generates a unique build ID at every build. So, this is why when we build the app it takes time to build it and it is the reason we disabled it in the above code.

To still run crashlytics in Debug mode,

android {
    buildTypes {
        debug {
            ext.alwaysUpdateBuildId = false
        }
    }
}

This will not generate a new build id on every build and now it will also work on development builds.

Gradle Caching

Enabling Gradle caching helps to take and reuse the output from previous builds. Caching is not enabled by default.

It works above Android Studio 3.0

To enable caching, we use

# Set this in gradle.properties
org.gradle.caching=true

Disable PNG Crunching

While building the app, Android performs Image Compression by default to reduce the size of the app and to do it, it takes a bit of time. To reduce the time we can disable to crunching in debug build.

android {
    buildTypes {
        debug {
            // Disables PNG crunching on DEBUG build type
            crunchPngs false // Enabled by default for RELEASE build type
        }
    }
}

For older version of plugin use,

aaptOptions {
      cruncherEnabled false
 }

Muilt Module App

Always be in the lookout for the code sections which can be converted to different modules like Library. It will allow the build to compile only the modules you modify and cache those outputs for future builds.

Enable Offline Mode

Gradle requires an internet connection to build your app. But if we are in poor internet connectivity the build might suffer. We can tell Gradle to avoid using network resources by using only the artifacts that it has cached locally.

To use Gradle offline when building with Android Studio, proceed as follows:

  1. Open the Preferences window by clicking File > Settings (on Mac, Android Studio > Preferences).
  2. In the left pane, click Build, Execution, Deployment > Gradle.
  3. Check the Offline work checkbox.
  4. Click Apply or OK.

Use incremental annotation processors

Android Gradle plugin 3.3.0 and higher improve support for incremental Java compilation when using annotation processors. So, to improve incremental build speeds, you should update your Android Gradle plugin and use only incremental annotation processors whenever possible.

This feature is compatible with Gradle versions 4.10.1 and higher, except for Gradle 5.1

Heap Size of JVM

Gradle daemon starts with 512MB of heap instead of 1GB. It is good enough for small projects but might not work for better larger size projects.

To, upgrade the Heap size to 1Gb, we use

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

And, Profile your build

By Profiling our app, the build time reduces a lot and we can find the issues of the app while building the app. To run the profile we use,

./gradlew assembleDebug --profile.

in the command line Or, we add it on the command-line options under Preferences when running the project on Android Studio.

After the profiling is complete, use the Project window navigate to the

project-root/build/reports/profile/

and look for the HTML file which can be opened in your browser and it will show us the summary of the complete build.

And based on the profile report we can optimise the build.

This is how we can speed up the gradle build for android.

Happy learning

Team MindOrks :)

Also, Let’s connect on Twitter, Linkedin, Github, and Facebook.