Android Nomad #56 - Build Flavors

Android Nomad #56 - Build Flavors

What are Build Variants?

Build variants are essential for creating different versions of an Android application during development and release phases.

They are useful for scenarios like:

  • Creating debug builds with or without Proguard.
  • Developing separate builds for free and paid users.
  • Generating builds tailored for different Android versions.

Build variants eliminate the need for maintaining separate projects for each version, as the core codebase remains largely consistent, with variations mainly in configurations and APIs.

Understanding Build Types and Product Flavors:

Build variants are created using a combination of Build Types and Product Flavors:

  • Build Types: Represent the build configuration, such as debug and release. Android Studio provides these by default.
  • Product Flavors: Define different versions of the app with varying content or features but sharing the same code base. Examples include free and paid versions.

Adding Build Types:

To add a new build type, modify the buildTypes block in the module-level build.gradle file. The article provides an example with three build types:

buildTypes {
    debug {
        versionNameSuffix ".dev"
        debuggable true
        ...

    }
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        ...
    }
    minifiedDebug {
        versionNameSuffix ".dev"
        debuggable true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        ...
    }
}

Here, minifiedDebug combines the properties of debug with Proguard enabled. You can also create a noMinifiedRelease build type that disables Proguard in the release configuration.

For creating a new build type based on an existing one, use the initWith property:

newBuildType {
    initWith debug
    versionNameSuffix ".newbuild"
    ...
}

Adding Product Flavors:

Product flavors are defined within the productFlavors block inside the android block of the build.gradle file.

Each flavor must belong to a flavor dimension:

flavorDimensions "version"
productFlavors {
    development {
        dimension "version"
        versionNameSuffix ".dev"
    }

    production {
        dimension "version"
        versionNameSuffix ".prod"
    }
}

This code creates two flavors, development and production, both belonging to the “version" dimension.

Combining these flavors with existing build types (debug and release) results in four build variants:

  • developmentDebug
  • developmentRelease
  • productionDebug
  • productionRelease

You can define multiple flavor dimensions by separating them with commas:

flavorDimension "dimensionOne", "dimensionTwo", "dimensionThree"

Conclusion:

Utilizing build variants effectively through a combination of build types and product flavors allows developers to manage different application versions efficiently within a single project. This approach significantly streamlines the development and release process, particularly for apps requiring multiple tailored versions.

Subscribe to Sid Pillai

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe