Build Variants and Product Flavors
Build Variants and Product Flavors
Android's build system uses build variants and product flavors to produce multiple versions of an application from a single codebase. Build types control how the application is compiled and packaged, while product flavors define distinct application versions with different features, resources, or identifiers. The Gradle plugin combines these dimensions into a matrix of build variants, each producing a separate APK or bundle. By the end of this lesson, you will be able to:
- Explain how build types and product flavors combine to form build variants.
- Configure product flavors with distinct application IDs and resources.
- Describe how flavor dimensions create a multi-axis variant matrix.
- Use source set directories to provide flavor specific code and resources.
- Identify how variant aware dependency resolution works in Gradle.
Build Types
Every Android project includes at least two build types: debug and release. A build type defines compilation and packaging settings that apply to the build process itself rather than to the application's identity or features:
android {
buildTypes {
debug {
isDebuggable = true
applicationIdSuffix = ".debug"
}
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile(
"proguard-android-optimize.txt"
),
"proguard-rules.pro"
)
}
}
}
The debug build type enables debugging tools, adds a suffix to the application ID so both debug and release versions can coexist on a device, and skips code shrinking for faster builds. The release build type enables R8 minification and resource shrinking to reduce APK size, and signs the output with a release key for distribution. Custom build types such as staging can be added for intermediate environments that need production like optimization with debug logging.
Product Flavors and Flavor Dimensions
Product flavors define variations of the application that differ in features, branding, or configuration. Each flavor belongs to a flavor dimension, and the build system creates one variant for every combination of flavors across all dimensions and build types:
android {
flavorDimensions += listOf("tier", "region")
productFlavors {
create("free") {
dimension = "tier"
applicationId = "com.example.app.free"
}
create("paid") {
dimension = "tier"
applicationId = "com.example.app.paid"
}
create("us") {
dimension = "region"
resValue("string", "api_host", "us.api.example.com")
}
create("eu") {
dimension = "region"
resValue("string", "api_host", "eu.api.example.com")
}
}
}
This interview continues for subscribers
Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.
Become a Sponsor