Sub topic 5
What the --flavor flag does
This chapter assumes you can run flutter run and flutter build, you know what the android/ and ios/ folders contain, and you have read the earlier chapters on --dart-define and -t entrypoints. Here you will see what --flavor actually changes, what it leaves alone, and the errors you get when the native side is not set up.
The short version
--flavor is a hint to the native build systems. It does not change your Dart code, and it does not load any configuration by itself. The Flutter tool takes the name you pass and hands it to Gradle on Android or Xcode on iOS. Those tools use the name to select a variant of the native project.
flutter run --flavor devAfter this command:
- On Android, Gradle builds the
devproduct flavor. - On iOS, Xcode builds the scheme named
devwith the matching build configurations. - The tool records
devso Dart code can read it later throughappFlavor.
Everything else comes from flags you pass alongside it. -t picks the Dart entrypoint, and --dart-define or --dart-define-from-file supplies compile-time values. The three flags are independent, so you can combine them freely.
| Flag | What it selects | Where it takes effect |
|---|---|---|
| `--flavor dev` | Native variant: app ID, name, icon, native config files | Gradle, Xcode |
| `-t lib/main_dev.dart` | Dart entrypoint | Dart compiler |
| `--dart-define-from-file=config/dev.json` | Compile-time constants | Dart compiler |
What happens when you pass --flavor
The tool does different work on each platform.
flowchart LR
A["flutter run --flavor dev"] --> B{Target platform}
B -->|Android| C["Gradle task: assembleDevDebug"]
B -->|iOS| D["xcodebuild -scheme dev -configuration Debug-dev"]
C --> E["Product flavor dev: app ID suffix, app_name, sources"]
D --> F["Scheme dev: Info.plist values, bundle ID, icon set"]
A --> G["appFlavor == dev in Dart"]The diagram block below carries the same content for screen readers.
Android
Gradle names its build tasks by joining the flavor and the build type. For flavor dev and build mode debug, the tool runs the task assembleDevDebug. For prod in release mode, it runs assembleProdRelease. The flavor name must therefore match a name in productFlavors. This is the block from the spine:
// 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")
}
}Two rules matter here:
- Every flavor needs a
dimensiononce you declareflavorDimensions. Gradle fails the build if one is missing. - Keep to a single dimension while you learn this. With several dimensions, Gradle's variant names combine flavors from each one, and the single
--flavorvalue no longer maps cleanly to one of them.
The output file also carries the flavor name. A release build for prod produces build/app/outputs/flutter-apk/app-prod-release.apk. Scripts that upload or archive artifacts must use that name, not app-release.apk.
iOS
Xcode has no product flavors, so Flutter maps the flavor onto two things you create yourself:
- A scheme whose name equals the flavor name, such as
dev. - Build configurations named by build mode and flavor:
Debug-dev,Profile-devandRelease-dev.
The tool finds the scheme by name and expects the scheme's Run, Profile and Archive actions to use the matching configurations. Chapter 8 covers creating them in Xcode. For now, remember that the scheme name is the link between the command line and the project. If the name differs from the flavor you pass, the build fails.
What --flavor does not do
Flavors are a native concept, and some common assumptions about them are wrong.
- It does not select a Dart entrypoint. Without
-t, the tool runslib/main.dartfor every flavor. - It does not set
API_BASE_URLor any other--dart-definevalue. - It does not rename the app by itself. Gradle or Xcode only does that if you configured the flavor to.
- It does nothing on platforms without a flavor concept. Web, for example, does not support it, and the tool reports an error if you pass it there.
Because the pieces are independent, a mismatch is easy to produce. This is a typical wrong command:
flutter run --flavor dev -t lib/main_prod.dart --dart-define-from-file=config/prod.jsonIt installs an app named "MyApp Dev" with the .dev suffix that sends traffic to production. Nothing in the tool warns you. You can add a runtime check that compares appFlavor with the config your app loaded, and fail fast on a mismatch. The next chapter shows how.
Reading the flavor in Dart
The appFlavor constant from package:flutter/services.dart exposes the value you passed. It is null when no flavor was given, so always handle that case.
import 'package:flutter/services.dart';
void printFlavor() {
// appFlavor is null when the app was built without --flavor.
print('Running flavor: ${appFlavor ?? 'none'}');
}Use this value for diagnostics and sanity checks. For anything that changes behavior, such as the API URL, prefer an explicit config object built from --dart-define values, as in the earlier chapters.
Running and building
The same flag works with every build command, and the flavor must match the entrypoint and config file you choose.
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 first command starts a debug session on a connected device. The second builds the release APK that ends up at app-prod-release.apk. For flutter build ios, the tool uses the prod scheme and the Release-prod configuration.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| The build fails and Gradle reports that a task such as `assembleDevDebug` does not exist | The flavor name is not in `productFlavors`, or the name differs in case or spelling | Match the name exactly in `build.gradle.kts` |
| Gradle reports that a flavor has no dimension | `flavorDimensions` is declared but a flavor omits `dimension` | Add `dimension = "env"` to each flavor |
| The tool says it cannot find the APK after a successful Gradle build | You ran a plain `flutter run` or `flutter build apk` on a project that has flavors, so Gradle built every variant and the expected `app-debug.apk` does not exist | Always pass `--flavor` once flavors exist |
| On iOS, the tool reports that the scheme or configuration does not exist | No scheme named after the flavor, or the scheme's configurations are not named `Debug-dev` and so on | Create the scheme and configurations, then mark the scheme as shared so it is committed |
| Both flavors replace each other on the device | The flavors have the same application ID or bundle ID | Add `applicationIdSuffix` on Android and a distinct bundle identifier on iOS |
| `appFlavor` is `null` in a build that used `--flavor` | The code runs in a test or an unsupported platform, or the build did not pass the flag | Check the command; in unit tests, treat `null` as a valid value |
Edge cases to keep in mind
- Name restrictions. Gradle rejects flavor names that start with
test,androidTestormain. Names such asdev,stagingandprodare safe. - Hot reload. Switching flavors needs a full restart because the native app is different. Hot reload and hot restart only replace Dart code.
- Single-flavor builds still work for unflavored platforms. If you run
flutter runon an iOS simulator for a project whose Android side has flavors, the iOS project has to define the scheme too. Flavors only work if both platforms you ship agree on the names. - Shared schemes. On iOS, an Xcode scheme that is not marked "Shared" lives in user data and is not committed. A teammate or CI machine then cannot find it.
Exercise
You run flutter run --flavor staging -t lib/main_dev.dart and the app starts with the dev API URL, but the installed app is named "MyApp Staging". Which flag controls which behavior, and why did this combination happen?
Show answer
--flavor staging selected the native staging variant, which supplies the app name (and ID suffix and icon). -t lib/main_dev.dart selected the Dart entrypoint, which hard-codes the dev API URL. The flags are independent, so the tool let you combine a staging native build with a dev entrypoint. The fix is to use -t lib/main_staging.dart (and the matching --dart-define-from-file) together with --flavor staging.
Exercise
A project defines the Android flavors dev and prod. You run flutter build apk --release --flavor prod. What is the Gradle task name, and where is the APK written?
Show answer
The task is assembleProdRelease, and the APK is written to build/app/outputs/flutter-apk/app-prod-release.apk.
Exercise
On iOS, which two things must exist in the Xcode project for flutter run --flavor dev to work, and what are the build configurations conventionally called?
Show answer
A scheme named dev (marked as shared) and build configurations tied to it. The configurations are named Debug-dev, Profile-dev and Release-dev.