Introduction
When developing an Android app, you will encounter situations where you need to create different flavors to cater to specific requirements, such as setting up environments (like staging, production, etc.). However, there is always something more than just setting up environments; for example, display apps with different icons and titles. Configuring icons and titles for each flavor is another thing that most people would do. If you find yourself struggling with the challenge of “building different flavors” or “setting icons and titles for different flavors”, you’ve come to the right place!
Key Considerations Before Getting Started
- This article will not cover “App Signing”.
2. This article will not cover “how to use Image Asset Tool to import icon”.
Essential Terms
- Build Variants: the results that
Gradle
builds based on the product flavors andbuild types
. - Build Type: different build and packaging settings, such as:
release
anddebug
. - Flavor Dimensions: a concept that’s used in
Gradle
to define different dimensions or aspects ofproduct flavors
. The combination of different dimensions allows you to generate differentbuild variants
.
android {
flavorDimensions "environment", "language"
productFlavors {
development {
dimension "environment"
}
production {
dimension "environment"
}
english {
dimension "language"
}
spanish {
dimension "language"
}
}
}
- Product Flavor: a configuration block in
Gradle
that allows you to define different flavors or variants for your app.
android {
productFlavors {
flavor1 {
...
}
flavor2 {
...
}
}
}
- Application Id: a property in
Gradle
to specify the unique identifier or package name for an Android app. - Application Suffix: a property in
Gradle
that appends a suffix to theapplicationId
property. - Manifest Placeholders:
manifestPlaceholders
is a property to provide placeholder values that can be used inAndroidManifest.xml
. This property stores data in the format of key-value, which defines data dynamically (for example, setting properties based on their product flavors).
# app/build.gradle
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_red",
appIconRound: "@mipmap/ic_launcher_red_round"
]
# AndroidManifest.xml
<application
...
android:icon="${appIcon}"
android:roundIcon="${appIconRound}"
...
/>
...
</application>
Defining Flavors & Customization
- Configure
buildTypes
:
android {
...
buildTypes {
release {
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.debug
}
}
}
2. Configure signingConfigs
:
android {
...
signingConfigs {
release {
// Add your configurations here
}
debug {
// Add your configurations here
}
}
}
3. Setup flavors by copying the configurations down below under the buildTypes
block:
android {
...
flavorDimensions "environment"
productFlavors {
dev {
dimension "environment"
applicationIdSuffix ".dev"
}
prod {
dimension "environment"
applicationIdSuffix ".prod"
}
}
}
4. Adding manifestPlaceholders
to flavor blocks in productFlavors
block if you want to customize app icons or titles:
android {
...
productFlavors {
dev {
...
manifestPlaceholders = [
appLabel: "MyApp (Development)",
appIcon: "@mipmap/dev_icon",
] }
prod {
...
manifestPlaceholders = [
appLabel: "MyApp",
appIcon: "@mipmap/prod_icon",
] }
}
}
5. Applying the placeholder values defined in manifestPlaceholders
to your AndroidManifest.xml
:
<application
android:label="${appLabel}"
android:icon="${appIcon}"
...
/>
...
</application>
6. Adding arguments --flavor <flavor>
to your build command. Take flutter run command for instance: flutter run --flavor dev
. If you are using Android Studio to build different flavors, don’t forget to add the arguments -- flavor <flavor>
to Build Variant
panel (You can find it by going to Build > select Build Variant
).
Reference
Basic use of Android Product Flavors/build variants
How to create build variants in Flutter Android so that each build variants have different app id?
Android — app icon for debug and release mode