What are Build Types, Product Flavors and Build Variants Concepts?

Build Types, Product Flavors and Build Variants. Don’t confuse these three concepts :)

Feyza Ürkut
Huawei Developers
6 min readMar 12, 2024

--

Photo by Jacob Mejicanos on Unsplash

Introduction

Hello everyone! 🌺 In this article, I will explain the differences and advantages of Build Types and Product Flavors, as well as the concept of Build Variants through various usage scenarios.

These tools allow you to customize and manage your application according to your target audience. This means, for example, that we can use many different versions on the same device by utilizing different variants without removing other versions.

I fully understood these three concepts when I decided to write this article and experimented with them a bit more on my project. I’m sure it will be useful for you too 😃

Enjoy the reading, let’s get started!

The Wind Rises ✨

🌻 Build Type

  • Each Build Type is defined and managed in the Gradle file.
  • The resources, manifest file, ProGuard rules, and other configurations to be used during the compilation process are determined.
  • Each Build Type has configurations that vary to meet different requirements.
  • It provides compilation configurations optimized for different stages of the development process.

What You Can Control with Build Types:

Compilation and Distribution Stages:

  • You can define different compilation and distribution scenarios such as Debug, Release, and Test.
  • It provides compilation configurations optimized for debugging, production, and testing environments.

Optimization at Compilation Level:

  • While the Debug version may include extra information for debugging purposes, the Release version is optimized and removes unnecessary information for debugging.

Settings Related to the Build Process:

  • You can specify configurations such as ProGuard rules, signing, and other compilation process settings.

Debug and Release Application Behaviors:

  • The Debug version can enable specific debugging features or use certain configurations for development purposes.
  • The Release version may include extra security measures or performance enhancements to provide an optimized experience for users.

🌻 Product Flavor

  • Product flavors represent different features or configurations of your application.
  • Different product flavors allow you to create customized versions based on the same underlying codebase. For example, you can use them to manage free and paid versions or customized versions for different markets or target audiences.
  • Typically, features such as language, color theme, API keys, etc., can be set specific to product flavors.
  • Each flavor may include different resource files (images, texts), different code snippets, or different dependencies.

What You Can Control with Product Flavors:

Customization for Different Market Segments:

  • You can create free and premium versions and add different features or services to each.
  • You can create application versions specific to different market segments or user groups.

Localization and Regional Settings:

  • You can create different variations for different language options or regional settings.
  • Each product flavor can include localized texts, images, and other localization requirements.

Brand Differentiation and Features:

  • You can create custom application versions for different brands or partners.
  • You can provide customized themes, logos, and other brand features for each brand or partner.

Hardware and Screen Differences:

  • You can create customized versions for different device types or screen sizes.
  • Each product flavor can include adjustments tailored to different screen resolutions or hardware features.

A/B Testing:

  • You can create different versions to test different features or design variations. Each product flavor allows you to collect data by directing different test groups, enabling you to improve the user experience.

Here you can find the similarities and differences between the concepts of Build Types and Product Flavors.

Differences:

  • Build Types define the purpose of compilation and distribution, while Product Flavors specify different variations of the application.
  • Build Types are predefined for specific purposes like Debug, Release, and Test, whereas Product Flavors allow you to define various variations of the application.
  • Build Types are typically used for different stages of the development process, whereas Product Flavors are used to create different versions tailored to different market segments, device types, or business requirements.

Similarities:

  • Both Build Types and Product Flavors provide diversity to meet different application requirements.
  • They are both defined and managed in the Gradle file.
  • Both Build Types and Product Flavors are used to manage compilation and distribution processes.

🌼 Build Variants

  • A combination of Build Type and Product Flavor is known as a Build Variant. Build variants represent different compilation and distribution options for your application.
  • A build variant includes a specific build type (Debug or Release) and one or more product flavors.
  • You can use build variants to create different combinations. For example, you can create both “free” and “premium” product flavors for the “Debug” build type.
  • At compilation time, different APKs can be generated based on a specific configuration. For instance, debug and release builds, as well as separate builds for testing and production environments.

🛠 Parameters Used

Parameters such as applicationId, applicationIdSuffix, versionNameSuffix, dimension, buildConfigField etc. are used to manage, version, and distinguish different feature sets of your application. This allows your application to be flexibly configured according to different scenarios.

  • applicationId: This is the unique identifier of the application and is typically expressed as a reverse domain name. For example, “com.example.myapp”. This identifier is used to distinguish the application from other applications.
  • applicationIdSuffix: This is an additional value appended to the end of the applicationId. It can be used to support multiple application configurations, for example, for different versions in different countries.
  • versionNameSuffix: This is an additional value appended to the end of the application version number (versionName). It can be used to distinguish different distribution types (e.g., beta or alpha versions).
  • dimension: Dimension is a label used to group product variants. For example, “flavor”, “abi” (application binary interface), “size”, etc.
  • buildConfigField: This is used to add custom fields from the application code to the BuildConfig class. It can be used to add information such as specific configuration or version numbers to the BuildConfig class.
  • flavorDimensions: This allows the grouping of different “flavors”. For example, “market” and “environment”. It is used in the productFlavors block.

BuildConfig.java

By default, BuildConfig.java is always generated at build time for each Android module of your application and comes with variables such as DEBUG, APPLICATION_ID, BUILD_TYPE, etc.

You can see the impact of the above code snippet on the BuildConfig file generated after the application is built. By accessing many parameters, we define from within the application, such as API_KEY, BASE_URL, you can control your code as you wish. ✌

Combine Multiple Product Flavors with Flavor Dimensions

Specifies the flavor dimensions you want to use. The order in which you list the dimensions determines their priority from highest to lowest when Gradle combines variable sources and configurations. Gradle determines the priority between flavor dimensions based on their appearance order alongside the flavorDimensions property; the first dimension has higher priority than the second, and so on.

As you can see in the code snippet below, when we change the order of store and mode dimensions with flavorDimensions, our active build variants also change.

Order of applicationIdSuffix: original appId + productFlavors + buildTypes

Original order of dimensions
  • applicationId : com.feyzaurkut.musicplayer.hms.free.debug
flavorDimensions += [“mode”, “store”]
  • applicationId : com.feyzaurkut.musicplayer.free.hms.debug

Conclusion

Build Types and Product Flavors are powerful tools for managing and distributing Android applications. While Build Types define compilation and distribution processes, Product Flavors allow you to create different versions for different market segments or user groups. Using both concepts correctly will make your application more flexible and manageable, improving the user experience.

See you in the new articles! 😃🌺

Spirited Away ✨

--

--