Understanding Build Variants in Android

Mayur Waghmare
Mobile Innovation Network
3 min readJun 10, 2024

Build variants in Android development refer to different versions of an app that can be generated from a single codebase.

  • These variants can target various configurations, environments, or custom features.
  • Allows to deliver different versions of the app without duplicating code or maintaining separate projects.
  • Manage different versions of the app for various distribution environments.

Why Build Variants Matter

1. Streamlining Testing : Test multiple app versions without codebase switching, making bug fixing easier.

2. Tailoring Features : Customize app functionality for user groups or regions, ensuring a personalized experience.

3. Optimizing Performance : Fine-tune resources for better app performance, catering to diverse user needs.

4. Simplifying Deployment : Manage different app versions effortlessly for various distribution channels or environments.

Creating Build Variants

Build variants are the result of different combinations of product flavors and build types. Let’s take an examples :

  • Build Types — debug and release
  • Product Flavour — Staging, PreProduction, Production.

We will have 6 build variants like:

- StagingDebug

- StagingRelease

- PreProductionDebug

- PreProductionRelease

- ProductionDebug

- ProductionRelease

Step 1: Define Build Types

Android Studio automatically configures two build types for you:

  • Debug build type — For debugging purposes.
  • Release build type — For distribution purposes.
buildTypes {
debug {
// Configuration for debug build type
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
// Configuration for release build type
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

Step 2: Create Product Flavour

flavor Dimensions.add("environment")
productFlavors {
create(name: "Staging") {
dimension = "environment"
applicationIdSuffix = ".staging"
resValue(type: "string", name: "app_name", value: "MyApp (Staging)")
buildConfigField(type: "String", name: "API_BASE_URL", value: "\"https://api.staging.example.com\"")
}

create(name: "PreProduction") {
dimension = "environment"
applicationIdSuffix = ".preprod"
resValue( type: "string", name: "app_name", value: "MyApp (Pre-Production)")
buildConfigField( type: "String", name: "API_BASE_URL", value: "\"https://api.preprod.example.com\"")
}

create(name: "Production") {
dimension = "environment"
// No suffix for production
resValue(type: "string", name: "app_name", value: "MyApp")
buildConfigField(type: "String", name: "API_BASE_URL", value: "\"https://api.example.com\"")
}
}

This code does these things:

  • Creates a flavor dimension called environment.
  • Creates 3 product flavors:

Staging { } , PreProduction { } and Production { }.

In this configuration:

  • We’ve added application ID suffix, app name for each flavor.
  • Different API base URL for each flavor.

Staging Flavor

PreProduction Flavor

Production Flavor

How to view your Build Variants ?

Click Build > Select Build Variant (or View > Tool Windows > Build Variants) to display the Build Variants window.

Conclusion

  • Build variants streamline app versioning from a single codebase.
  • They offer versatility by targeting various configurations, environments, or custom features.
  • Developers can manage and deliver diverse app versions without code duplication.
  • Testing is simplified with the ability to test multiple variants without switching codebases.

Thank you for taking the time to read this article. If you found the information valuable, please consider giving it a clap or sharing it with others who might benefit from it.

Any Suggestions are welcome. If you need any help or have questions for Code Contact US. You can follow us on LinkedIn for more updates 🔔

--

--

Mayur Waghmare
Mobile Innovation Network

"Mobile App Developer specializing in Android, iOS, and Flutter. Passionate about crafting user-focused, efficient mobile solutions."