Sub topic 7
Android: productFlavors in Gradle
This chapter shows how to turn the --flavor flag into real Android build variants. It assumes you know the layout of the android/ folder, you can run flutter run and flutter build, and you have seen --dart-define and -t from earlier chapters. The examples use the Kotlin DSL (build.gradle.kts). Older projects use Groovy (build.gradle), where the same ideas apply with slightly different syntax.
What a product flavor is
Android's build system, the Android Gradle Plugin (AGP), produces a build variant for every combination of a build type and a product flavor.
- A build type describes how the app is built. The defaults are
debugandrelease. - A product flavor describes which version of the app is built, such as
devorprod. - A flavor dimension is a group of flavors that exclude each other. Every flavor must belong to one dimension.
With flavors dev and prod in one dimension, Gradle creates four variants: devDebug, devRelease, prodDebug and prodRelease.
When you run flutter run --flavor dev, Flutter passes the name to Gradle. It then runs the matching task, such as assembleDevDebug. If no flavor with that name exists, the build fails with an error saying the flavor is not found.
Declaring flavors
Open android/app/build.gradle.kts. Add the flavor configuration inside the android { } block:
// android/app/build.gradle.kts, inside the android { } block
flavorDimensions += "env"
productFlavors {
create("dev") {
dimension = "env"
applicationIdSuffix = ".dev"
resValue("string", "app_name", "MyApp Dev")
}
create("prod") {
dimension = "env"
resValue("string", "app_name", "MyApp")
}
}Each line does one job:
flavorDimensions += "env"declares the dimension. The nameenvis arbitrary, but each flavor'sdimensionmust match it.applicationIdSuffix = ".dev"appends text to the baseapplicationIdfromdefaultConfig. If the base iscom.example.myapp, the dev build installs ascom.example.myapp.dev.resValue(...)generates an Android string resource namedapp_nameat build time, with a different value per flavor.
Why the application ID matters
Android identifies an installed app by its application ID. Two builds with the same ID replace each other on a device. Giving dev a suffix lets you keep the dev build and the store version on one phone.
The suffix changes only the application ID. It does not change namespace, which controls where the generated R class and your Kotlin or Java package live. Your source files do not need to move.
Some services tie to the application ID, so each flavor ID must be registered there. Examples are Firebase, Google Sign-In and deep-link verification.
Connecting the generated app name
A generated resource does nothing until the manifest uses it. Flutter's default template hard-codes the label. Edit android/app/src/main/AndroidManifest.xml:
<application
android:label="@string/app_name"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">If android/app/src/main/res/values/strings.xml already defines app_name, remove that entry. Otherwise the build fails with a duplicate resource error.
On newer AGP versions, generated resource values may be switched off by default. If Gradle reports that resValue is not allowed, enable it:
// android/app/build.gradle.kts, inside the android { } block
buildFeatures {
resValues = true
}Running and building
Pass the same name to Flutter that you used in create(...). The flavor name is case-sensitive.
flutter run --flavor dev -t lib/main_dev.dart --dart-define-from-file=config/dev.json
flutter build apk --release --flavor prod -t lib/main_prod.dart --dart-define-from-file=config/prod.jsonThe --flavor flag only selects the native variant. The -t flag selects the Dart entrypoint, and --dart-define-from-file supplies compile-time values. All three can differ per environment, but none of them sets the others. You can run the dev flavor with lib/main_prod.dart, and Flutter will not warn you.
After a flavored build, Flutter writes the APK with the flavor in the file name, for example build/app/outputs/flutter-apk/app-prod-release.apk.
For the Play Store, build an app bundle the same way:
flutter build appbundle --release --flavor prod -t lib/main_prod.dart --dart-define-from-file=config/prod.jsonPer-flavor source sets
Gradle merges folders under android/app/src/ into the build. main is always included. A folder named after a flavor is included only for that flavor, and its files win over main.
| Path | Included in |
|---|---|
| `android/app/src/main/` | Every variant |
| `android/app/src/dev/` | Only `devDebug` and `devRelease` |
| `android/app/src/prod/` | Only `prodDebug` and `prodRelease` |
| `android/app/src/debug/` | Only debug variants of any flavor |
| `android/app/src/devDebug/` | Only the `devDebug` variant |
Use these folders for per-flavor files:
- Icons: put
ic_launcher.pngfiles underandroid/app/src/dev/res/mipmap-hdpi/and the other density folders. The dev build then shows a different icon, so testers can tell the builds apart on the home screen. - Firebase config: put a separate
google-services.jsoninandroid/app/src/dev/andandroid/app/src/prod/. Remove or avoid a copy inandroid/app/. A later chapter covers this in detail. - Manifest overrides: a
src/dev/AndroidManifest.xmlis merged into the main manifest. It is a good place for debug-only permissions or a different intent filter.
A file in src/dev/res/ replaces the same-named file in src/main/res/. It does not add a second copy. If a resource exists in several folders of the same priority, Gradle reports a conflict.
Manifest placeholders
For values that belong in the manifest but are not strings, use manifestPlaceholders. A common case is a maps key or an OAuth redirect scheme:
productFlavors {
create("dev") {
dimension = "env"
applicationIdSuffix = ".dev"
manifestPlaceholders["authScheme"] = "myapp-dev"
}
create("prod") {
dimension = "env"
manifestPlaceholders["authScheme"] = "myapp"
}
}In the manifest, write android:scheme="${authScheme}". Gradle substitutes the value when it merges the manifest. Gradle fails the build if a placeholder has no value in some variant.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Gradle build failed to produce an .apk file" after a flavored build | Flutter looked for the wrong file name, often because the flavor name differs in case or has an unusual first letter | Use a lowercase first letter. Keep the name identical in Gradle and on the command line |
| "The Gradle project does not define a task suitable for the requested build" | The flavor name passed to `--flavor` does not exist in Gradle | Check spelling in `productFlavors` |
| "Missing dimension strategy" or "All flavors must now belong to a named flavor dimension" | A flavor has no `dimension` set | Add `dimension = "env"` to every flavor |
| Duplicate resources `app_name` | `strings.xml` and `resValue` both define it | Delete the entry in `strings.xml` |
| Installing the dev build removes the prod app | Both use the same application ID | Add `applicationIdSuffix` to dev |
| Firebase says no matching client found | `google-services.json` does not contain the flavor's application ID | Register the suffixed ID in Firebase and download a new file |
Edge cases
- Naming. Gradle capitalizes flavor names in task names (
assembleDevDebug), so start flavor names with a lowercase letter. Names starting withtestorandroidTestare reserved and fail. - One dimension. Multiple dimensions are legal in Gradle, but Flutter's
--flavortakes a single name. Keep one dimension unless you have a strong reason. With more, the flavor name you pass must match the combined variant name, which is awkward. - Running without a flavor. Once flavors are declared,
flutter runwith no--flavormay fail because Gradle cannot pick a variant. Add a default in your IDE run configuration or script. - Debug and release signing. Flavors share the
debugandreleasebuild types. You can attach a differentsigningConfigto each flavor if the keys differ. The signing chapter covers this. - Hot reload. Changing
build.gradle.ktsneeds a full rebuild, not a hot restart.
Checking the result
After a run, confirm the ID on a connected device:
adb shell pm list packages | grep myappYou should see package:com.example.myapp.dev for the dev build. If you install both flavors, you see both lines. The launcher shows "MyApp Dev" and "MyApp" side by side.
Exercise
Your base applicationId is com.acme.shop. You add a staging flavor with applicationIdSuffix = ".staging" and resValue("string", "app_name", "Shop Staging"). The launcher still shows the old label after you install it. Name two things to check, and give the final application ID.
Show answer
Check that AndroidManifest.xml uses android:label="@string/app_name" instead of a hard-coded string, and that no app_name entry in strings.xml overrides or conflicts with the generated one (also confirm you ran with --flavor staging). The final application ID is com.acme.shop.staging.
Exercise
You want only the dev build to use a different launcher icon. Where do the icon files go, and why do you not need to touch the manifest?
Show answer
Put them in android/app/src/dev/res/mipmap-*/ic_launcher.png. The manifest already references @mipmap/ic_launcher, and Gradle merges the dev source set over main, so the dev file replaces the main one for dev variants only.