Sub topic 7
Android: productFlavors in Gradle
This chapter assumes that you know basic Flutter and Dart, can run flutter run and flutter build, and recognize the android/ project folder. You should also know that an Android application ID identifies an installed app.
Android product flavors let one Flutter project produce multiple Android apps. A common setup has dev, staging, and prod flavors. Each flavor can use a different application ID, app name, icon, backend configuration, or signing setup.
How Android flavors work
A product flavor is a named variation of an Android app. Gradle combines a flavor with a build type such as debug or release to create a build variant.
For example:
| Flavor | Build type | Build variant |
|---|---|---|
| `dev` | `debug` | `devDebug` |
| `prod` | `release` | `prodRelease` |
The --flavor option in a Flutter command selects the flavor. The build type still comes from the command. flutter run normally uses a debug build, while flutter build apk --release uses a release build.
A flavor dimension groups related flavors. Android requires every product flavor to belong to a dimension when flavors are defined.
flowchart TD
A[Flutter command] --> B[--flavor dev]
B --> C[Android product flavor]
C --> D[devDebug or devRelease]
D --> E[Application ID and resources]
E --> F[Installed Android app]Define `dev` and `prod`
In a project using the Kotlin Gradle DSL, edit android/app/build.gradle.kts. Add the flavors inside the existing 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")
}
}The env dimension means that dev and prod are alternatives within the environment group.
applicationIdSuffix = ".dev" changes the base application ID. If the base ID is com.example.my_app, the development app becomes com.example.my_app.dev. Android treats those as different applications, so both can be installed at the same time.
The resValue calls create Android string resources. Connect the generated resource to the application label in android/app/src/main/AndroidManifest.xml:
<application
android:label="@string/app_name"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
</application>The prod flavor has no suffix, so it keeps the normal application ID. Keeping production’s ID unchanged is useful when the app already exists in the Play Store.
> callout note > The flavor name is case-sensitive in commands and configuration. Use one spelling consistently, such as dev and prod, across Gradle files, Flutter commands, CI jobs, and service configuration. > ```
Run and build a flavor
Pass the flavor name to Flutter with --flavor:
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 selects Android’s native flavor. The -t flag selects the Dart entrypoint, and the configuration file supplies compile-time Dart values. These options solve different problems: Gradle controls the Android package and resources, while Flutter controls the Dart code that starts the app.
After changing Gradle flavor definitions, let Flutter or Android Studio resynchronize the project. If Gradle reports an unknown flavor, check the spelling and confirm that the flavor was added inside the app module’s android { } block.
Exercise
Why can dev and prod be installed side by side when only dev has applicationIdSuffix = ".dev"?
Show answer
The suffix gives dev a different application ID from prod, so Android treats them as separate applications.