Gradle is an easily customizable build system that supports building by a convention model. Gradle is written in Java, but the build language is Groovy DSL (domain spec language). Gradle not only supports multi-project builds, but it also supports dependencies like Ivy and Maven. Gradle also can support building non-Java projects.

The entire process of improving your build speed is as follows:

  1. Optimize your build configuration by taking a few steps that immediately benefit most Android Studio projects.

When developing your mobile app, you should deploy to a device running Android 7.0 (API level 24) or higher whenever possible. Newer versions of the Android platform implement better mechanics for pushing updates to your app, such as the Android Runtime (ART) and native support for multiple DEX files.

A. Optimize your build configuration

  • Keep your tools up to date: The Android tools receive build optimizations and new features with almost every update, and some tips on this article assume you’re using the latest version. To take advantage of the latest optimizations, keep your Android Studio and SDK tools and the Android plugin for Gradle.
android {
...
defaultConfig {...}
buildTypes {...}
productFlavors {
// When building a variant that uses this flavor, the following configurations
// override those in the defaultConfig block.
dev {
// 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.
minSdkVersion 21
versionNameSuffix "-dev"
applicationIdSuffix '.dev'
}
prod {
// If you've configured the defaultConfig block for the release version of
// your app, you can leave this block empty and Gradle uses configurations in
// the defaultConfig block instead. You still need to create this flavor.
// Otherwise, all variants use the "dev" flavor configurations.
}
}
}

If your build configuration already uses product flavors to create different versions of your app, you can combine the “dev” and “prod” configurations with those flavors by using flavor dimensions. For example, if you already configure a “demo” and “full” flavor, you can use the following sample configuration to create combined flavors, such as “devDemo” and “prodFull”:

android {
...
defaultConfig {...}
buildTypes {...}
// Specifies the flavor dimensions you want to use. The order in which you
// list each dimension determines its priority, from highest to lowest,
// when Gradle merges variant sources and configurations. You must assign
// each product flavor you configure to one of the flavor dimensions.
flavorDimensions "stage", "mode" productFlavors {
dev {
dimension "stage"
minSdkVersion 21
versionNameSuffix "-dev"
applicationIdSuffix '.dev'
...
}
prod {
dimension "stage"
...
}
demo {
dimension "mode"
...
}
full {
dimension "mode"
...
}
}
}
  • Avoid compiling unnecessary resources: Avoid compiling and packaging resources that you aren’t testing (such as additional language localizations and screen-density resources). You can do that by only specifying one language resource and screen desnity for your “dev” flavor, as shown in the following sample:
android {
...
productFlavors {
dev {
...
// The following configuration limits the "dev" flavor to using
// English stringresources and xxhdpi screen-density resources.
resConfigs "en", "xxhdpi"
}
...
}
}
  • Disable Crashlytics for your debug builds: If you don’t need to run a Crashlytics report, speed up your debug builds by disabling the plugin as follows:
android {
...
buildTypes {
debug {
ext.enableCrashlytics = false
}
}

Android/Hybrid Developer at Indigo Consulting Mumbai, Maharashtra, India