Sub topic 12
Running flavors from the IDE and CI
This chapter assumes you have already created Android product flavors and iOS schemes with matching names, and that you have -t entrypoints and config/*.json files for each environment. Here you wire those pieces into the places where people and machines start builds: VS Code, Android Studio, and a CI service.
One set of flags, three places
A flavored build needs three inputs: the flavor name (--flavor), the entrypoint (-t), and the compile-time values (--dart-define-from-file). Every tool in this chapter passes the same three flags to the same flutter command. A bug that appears only in CI or only in your IDE is usually a flag that one of them is missing.
The commands from the earlier chapters look like this:
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.jsonKeep these two lines in mind. Everything below is a way to avoid retyping them.
Running flavors from VS Code
The Dart extension for VS Code reads launch configurations from .vscode/launch.json. Create one entry per flavor. Put the flutter tool flags in toolArgs, not in args. The args list is passed to your Dart program's main, not to flutter run.
{
"version": "0.2.0",
"configurations": [
{
"name": "MyApp (dev)",
"request": "launch",
"type": "dart",
"program": "lib/main_dev.dart",
"toolArgs": [
"--flavor", "dev",
"--dart-define-from-file=config/dev.json"
]
},
{
"name": "MyApp (prod, release)",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"program": "lib/main_prod.dart",
"toolArgs": [
"--flavor", "prod",
"--dart-define-from-file=config/prod.json"
]
}
]
}The program field is the equivalent of -t. The flutterMode field accepts debug, profile and release. After you save the file, the entries appear in the Run and Debug dropdown, and F5 starts the selected one.
Commit .vscode/launch.json to the repository. The whole team then gets the same entries, and a new developer does not have to learn the flag combination.
Running flavors from Android Studio and IntelliJ
Android Studio stores each run as a run configuration of type "Flutter". Open Run > Edit Configurations, add a Flutter configuration, and fill in these fields:
- Dart entrypoint: the file for the environment, for example
lib/main_dev.dart. - Build flavor: the flavor name, for example
dev. This field becomes--flavor dev. - Additional run args:
--dart-define-from-file=config/dev.json.
Create one configuration per flavor and name them clearly, such as dev and prod. To share them, tick Store as project file so the IDE writes them under .run/ as XML. Commit that folder.
Both IDEs show the same failure when something is off. If the Build flavor field is empty, Gradle sees no flavor and Flutter cannot find an APK to install. Flutter then reports that it could not find the built application. Check the flavor field first.
Scripts as a shared entry point
IDE settings do not help someone building from a terminal, and CI does not read your launch.json. A small script gives you one place to keep the flag combinations.
#!/usr/bin/env bash
# tool/build.sh <flavor> e.g. tool/build.sh prod
set -euo pipefail
flavor="${1:?usage: tool/build.sh <dev|staging|prod>}"
flutter build apk --release \
--flavor "$flavor" \
-t "lib/main_${flavor}.dart" \
--dart-define-from-file="config/${flavor}.json"The script depends on a naming rule: the entrypoint is main_<flavor>.dart and the config file is <flavor>.json. That rule is also useful in its own right, because a mismatch such as flavor prod with config/dev.json becomes hard to make by accident.
Building in CI
A CI job starts on a clean machine. It has to install Flutter, fetch packages, create any config files that are not in the repository, and then run the build. The example below uses GitHub Actions and builds each flavor with a matrix.
name: android-build
on:
push:
branches: [main]
jobs:
build-apk:
runs-on: ubuntu-latest
strategy:
matrix:
flavor: [dev, prod]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- uses: subosito/flutter-action@v2
with:
channel: stable
cache: true
- run: flutter pub get
- run: flutter analyze
- run: flutter test
- name: Build APK
run: |
flutter build apk --release \
--flavor ${{ matrix.flavor }} \
-t lib/main_${{ matrix.flavor }}.dart \
--dart-define-from-file=config/${{ matrix.flavor }}.json
- uses: actions/upload-artifact@v4
with:
name: apk-${{ matrix.flavor }}
path: build/app/outputs/flutter-apk/app-${{ matrix.flavor }}-release.apkTwo details matter here. The matrix gives each flavor its own job, so a failure in prod does not hide a result for dev. The artifact path includes the flavor, because Gradle names flavored outputs app-<flavor>-<buildType>.apk. Without --flavor, the file would be app-release.apk. A wrong path gives you a green build and an empty artifact upload, which is easy to miss.
Where the config files come from
If config/prod.json is committed, CI needs nothing extra. Some teams prefer to keep the file out of the repository. In that case, store its content in a CI variable and write it before the build step:
- name: Write prod config
env:
PROD_CONFIG_JSON: ${{ vars.PROD_CONFIG_JSON }}
run: printf '%s' "$PROD_CONFIG_JSON" > config/prod.jsonPassing the value through env instead of pasting ${{ ... }} directly into the shell command avoids quoting problems if the JSON contains quotes or special characters. Add config/prod.json to .gitignore so a local copy is not committed by mistake.
iOS builds in CI
iOS builds need a macOS runner and signing assets. The flavor flags are the same:
flutter build ipa --release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json \
--export-options-plist=ios/ExportOptions.plistThe --flavor prod flag requires a scheme named prod in Xcode, and that scheme must be marked as Shared so it is stored in ios/Runner.xcodeproj/xcshareddata/xcschemes/. An unshared scheme exists only in your local xcuserdata folder, so it works on your laptop and disappears on the CI machine. If a CI iOS build says that the scheme was not found while it works locally, check this first.
Signing is covered in its own chapter. For now, note that macOS runners cost more than Linux ones, and many teams build Android on every push and iOS only on release branches or tags.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| IDE run starts but installs the wrong app, or finds no APK | Build flavor field or `--flavor` in `toolArgs` is empty | Set the flavor in the run configuration |
| App starts with default URL, not the dev one | `--dart-define-from-file` missing from the IDE entry | Add it to `toolArgs` or Additional run args |
| `--dart-define` flags ignored in `launch.json` | Flags placed in `args` instead of `toolArgs` | Move them to `toolArgs` |
| CI uploads nothing or fails on the artifact path | Path uses `app-release.apk` for a flavored build | Use `app-<flavor>-release.apk` |
| iOS scheme not found only in CI | Scheme not shared | Tick **Shared** in Manage Schemes and commit the file |
| `prod` build shows a dev server | Flavor and config file mismatch | Derive both from one variable, as in `tool/build.sh` |
Checking what was built
A pipeline that builds without errors can still build the wrong thing. Add a cheap check in your app: print or display the flavor and the base URL on a debug screen or in a startup log, using appFlavor and your AppConfig. In CI you can also inspect the artifact. For Android, the aapt tool in the SDK build tools shows the package name and label of an APK:
aapt dump badging build/app/outputs/flutter-apk/app-dev-release.apk | grep -E "package:|application-label:"For the dev flavor from this tutorial, you should see the package name ending in .dev and the label MyApp Dev. If you do not, the flavor was not applied.
Exercise
A teammate says their VS Code launch entry for the staging flavor starts the app, but it always connects to the default API URL from AppConfig, and appFlavor is null. The entry has "program": "lib/main_staging.dart" and "args": ["--flavor", "staging", "--dart-define-from-file=config/staging.json"]. What is wrong, and how do you fix it?
Show answer
The flags are in args, which VS Code passes to the app's main function, so flutter run never receives them. Without --flavor the app is built with no flavor, so appFlavor is null, and without --dart-define-from-file the String.fromEnvironment calls fall back to their default values. Move the two flags into "toolArgs", keep program as it is, and run again.