Android uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. One of the most important file in Android application development is build.gradle, which is used in the process of building an android app. Let’s see how we can configure android builds with build.gradle.

What is Gradle?

Gradle is an open source build automation system. It brings the convenience of a Groovy-based DSL along with the advantages of Ant and Maven. Android Gradle plugin was introduced as the build tool built into the first preview of Android Studio during Google I/O in May 2013. Since then lots of improvents have been added in Android Build Configuration.

Why configure the build?

With different build configurations, we can define different set of code and resources while reusing common parts in all build versions.

Android Build Configurations

With the help of Gradle and Android Plugin, we can configure the build in following ways:

Build Types

Using build types we can configure the different stages of development lifecycle. By default, there are two build types – debug and release. But we can add more such as uat depending on project requirement and available environments.

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField 'String', 'BASE_URL', '"https://<Production base url>"'
            signingConfig signingConfigs.release
            ...
        }

        debug {
            minifyEnabled false
            buildConfigField 'String', 'BASE_URL', '"<Staging base url>"'
            ...
        }
    }

With different build types, we can specify that release build type may shrink, obfuscate, and sign your APK with a release key for distribution and debug build will only print logs. We can even specify different resources for different build types.

Product Flavors

Product/Build Flavours are useful when we have different versions for application such as premium or free. Flavors are a way to differentiate the properties of an app such as App Name. WIth different flavours, we can change app identifies such as,

flavorDimensions "mode1", "mode2"
productFlavors {
        free {
            dimension "mode2"
            applicationIdSuffix ".free"
            versionNameSuffix "-free"
            manifestPlaceholders = [appName: "@string/app_name_free"]
            ...
        }
        premium {
            dimension "mode2"
            applicationIdSuffix ".premium"
            versionNameSuffix "-premium"
            manifestPlaceholders = [appName: "@string/app_name_premium"]
            ...
        }
    }

The manifestPlaceholders allows you to modify properties in your AndroidManifest.xml file at build time. In above example, we are specifying which app name to choose from strings.xml file,

<string name="app_name_free">My Awesome App Free</string>
<string name="app_name_premium">My Awesome App Premium</string>

Dimensions are used to combine configurations from multiple product flavors. For eg, we may want to create difernt configurations according to api level and add some feature for that api level.

minApi24 {
      dimension "mode1"
      minSdkVersion 24
      versionCode 100 + android.defaultConfig.versionCode
      versionNameSuffix "-minApi24"
      ...
    }

minApi21 {
      dimension "mode1"
      minSdkVersion 21
      ...
    }

To ensure the target device receives the version of the app with the highest compatible API level, assign version codes in increasing value with API level.

Build Variants

With use of build type and product/build flavour, what we have actually generated are Build Variants. The number of build variants will be equal to the product of the number of flavors in each flavor dimension and the number of build types configured. Gradle will name them as follows:

  • Build variant: [minApi24, minApi21][free, premium][release, debug]
  • Corresponding APK: app-[minApi24, minApi21]-[free, premium]-[release, debug].apk

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors.

Conclusion

Gradle build configurations are very powerful and are useful to simplify the build process for different versions of app. The things stated above are just a glimpse in gradle world and there are tons of features to improve build process.