Chapter 3. Build Types and Flavors

3.1 Working with Build Types

Problem

You want to customize the debug and release build types, or create additional types of your own.

Solution

The buildTypes block inside android is used to configure build types.

Discussion

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release. Both can be configured inside the buildTypes block inside of the module build file. The buildTypes block from the module build file in a new project is shown in Example 3-1.

Example 3-1. Default buildTypes block from module build file
android {
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
        }
    }
}

The only build type shown in the example is the release build, but it is just as easy to add a debug block as well if you want to configure the default settings. Either block supports a range of properties. The complete set of properties and methods can be found in the DSL reference for the com.android.build.gradle.internal.dsl.BuildType class.

In the release block on the example, minifyEnabled refers to the automatic removal of unused resources in the packaged app. If true, Gradle also removes resources from dependent libraries if they are not needed. This only works if the shrinkResources property is also set to true.

In Example 3-2, both are set to true.

Example 3-2. Removing resources and shrinking ...

Get Gradle Recipes for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.