Sub topic 13
Signing and releasing each flavor
This chapter assumes you have already set up Android productFlavors and iOS schemes with matching build configurations, as covered earlier in this tutorial. You should also know how to run flutter build with --flavor and -t. Here you will make each flavor produce a signed, uploadable artifact that cannot be mistaken for another flavor.
Why signing differs per flavor
Signing proves that an app came from you and that nobody has changed it since. Both stores use it to decide whether a build may replace an installed app or an existing store listing.
Flavors are different apps from the stores' point of view, because each has its own application ID (Android) or bundle ID (iOS). That has three consequences:
- Each flavor you ship needs its own store listing, or its own internal track or TestFlight app.
- Each flavor can use a different signing identity. In practice,
devandstagingoften use a debug key or an internal certificate, and onlyproduses the production upload key. - A build signed with the wrong key is rejected by the store. Worse, it can be installed on a device and fail to update over the previous build.
The overall flow looks like this:
Android: a signing config per flavor
Keep the keystore out of the repository
Create the keystore once and store it outside version control. Put its location and passwords in android/key.properties, and add that file to .gitignore.
# android/key.properties (never commit this file)
storeFile=/secure/path/myapp-upload.jks
storePassword=change-me
keyAlias=upload
keyPassword=change-meWire it into Gradle
In android/app/build.gradle.kts, load the properties file, define one signing config per flavor that needs a real key, and attach each config to its flavor.
// android/app/build.gradle.kts
import java.io.FileInputStream
import java.util.Properties
val keystoreProperties = Properties()
val keystorePropertiesFile = rootProject.file("key.properties")
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}
android {
signingConfigs {
create("prod") {
if (keystorePropertiesFile.exists()) {
storeFile = file(keystoreProperties.getProperty("storeFile"))
storePassword = keystoreProperties.getProperty("storePassword")
keyAlias = keystoreProperties.getProperty("keyAlias")
keyPassword = keystoreProperties.getProperty("keyPassword")
}
}
}
flavorDimensions += "env"
productFlavors {
create("dev") {
dimension = "env"
applicationIdSuffix = ".dev"
signingConfig = signingConfigs.getByName("debug")
}
create("prod") {
dimension = "env"
signingConfig = signingConfigs.getByName("prod")
}
}
}The guard around the prod config lets teammates without the keystore still build and run dev. Gradle configures every signing config, so an unguarded file(null) would fail the whole build.
The Flutter project template sets signingConfig = signingConfigs.getByName("debug") inside buildTypes { release { ... } } so that new projects build out of the box. Delete that line. Android Gradle Plugin gives a build type's signing config priority over a flavor's. If the release build type keeps its own config, every flavor is signed with the debug key and your prod config is silently ignored.
The debug build type sets its own debug key, so flutter run --flavor prod still uses the debug key. This is what you want: only release builds get the production key.
Build the artifacts
Play Console requires an Android App Bundle (AAB) for new apps. APKs are still useful for direct installs and for testers.
# Store upload: Android App Bundle for prod
flutter build appbundle --release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json \
--build-name=1.4.0 --build-number=42
# Tester build: signed APK for dev
flutter build apk --release \
--flavor dev \
-t lib/main_dev.dart \
--dart-define-from-file=config/dev.jsonFlutter writes the artifacts to a folder named after the flavor and build type, for example build/app/outputs/bundle/prodRelease/app-prod-release.aab. Scripts that upload artifacts must use this path, not the old release/ folder.
Play App Signing and the upload key
With Play App Signing, which is the default for new apps, the key in your keystore is only an upload key. Google re-signs the final APKs with an app signing key that it holds. If you lose the upload key, you can ask Google to reset it. Losing a key you used without Play App Signing means you can never update that app.
Because the dev flavor has a different application ID, it is a separate app in Play Console. Create a separate app entry for it, or distribute it through another channel such as Firebase App Distribution.
iOS: configurations, certificates and profiles
On iOS, --flavor prod selects the Xcode scheme named prod. Flutter then builds with the configuration names Debug-prod, Profile-prod and Release-prod. If a scheme or configuration name is missing or misspelled, the build fails with a message that the scheme was not found.
Each flavor's release configuration carries its own bundle ID, team and provisioning profile. Putting these values in an .xcconfig file keeps them in version control and out of the Xcode UI. Create one file per flavor and configuration:
// ios/Flutter/Release-prod.xcconfig
#include "Release.xcconfig"
PRODUCT_BUNDLE_IDENTIFIER = com.example.myapp
DEVELOPMENT_TEAM = ABCDE12345
CODE_SIGN_STYLE = Manual
CODE_SIGN_IDENTITY = Apple Distribution
PROVISIONING_PROFILE_SPECIFIER = MyApp Prod AppStoreThen assign this file to the Release-prod configuration of the Runner project in Xcode (Project, Info, Configurations). If your project uses CocoaPods, check that the Pods configuration is still applied to the target after you change the base configuration.
The dev flavor repeats the pattern with a different bundle ID such as com.example.myapp.dev, a development or ad hoc profile, and CODE_SIGN_IDENTITY = Apple Development.
Export options for command-line builds
flutter build ipa archives and exports in one step. For manual signing, it needs an export options file that names the profile for each bundle ID:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>app-store-connect</string>
<key>teamID</key>
<string>ABCDE12345</string>
<key>signingStyle</key>
<string>manual</string>
<key>provisioningProfiles</key>
<dict>
<key>com.example.myapp</key>
<string>MyApp Prod AppStore</string>
</dict>
</dict>
</plist>Save it as ios/ExportOptions-prod.plist, then run the build:
flutter build ipa --release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json \
--export-options-plist=ios/ExportOptions-prod.plistThe IPA appears in build/ios/ipa/. Older Xcode versions use app-store instead of app-store-connect for the method value.
Releasing from CI
A CI job must recreate the secrets that you keep out of the repository. The usual pattern is to store the keystore as a base64 string in the CI secret store, decode it to a temporary file, and write key.properties from other secrets:
#!/usr/bin/env bash
set -euo pipefail
echo "$ANDROID_KEYSTORE_BASE64" | base64 --decode > "$RUNNER_TEMP/upload.jks"
cat > android/key.properties <<EOF
storeFile=$RUNNER_TEMP/upload.jks
storePassword=$ANDROID_STORE_PASSWORD
keyAlias=$ANDROID_KEY_ALIAS
keyPassword=$ANDROID_KEY_PASSWORD
EOF
flutter build appbundle --release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json \
--build-number="$CI_BUILD_NUMBER"RUNNER_TEMP is the temporary directory that GitHub Actions provides. Substitute your CI system's equivalent. Delete the keystore and key.properties at the end of the job if the runner is not discarded.
Versioning and release checks
- Build numbers: Both stores reject an upload whose build number was already used for that app. Because each flavor is a separate app,
devandprodhave independent counters. Pass--build-numberfrom CI so numbers never repeat. - Release mode only: Store builds must be
--release. Profile and debug builds are not accepted, and debug builds ship with assertions and slower code. - Matching configuration: Check that the entrypoint and the
--dart-define-from-filefile belong to the same flavor. Aprodbundle built withconfig/dev.jsonsigns and uploads without error, and users then hit the dev backend. - Verify the artifact: Before you upload, inspect the result. For Android,
keytool -printcert -jarfile app-prod-release.apkprints the signing certificate of an APK, so you can compare it with your keystore. For iOS,codesign -dvv Runner.appon an unpacked app shows the identity and team.
Common failures
| Symptom | Likely cause |
|---|---|
| Release APK is signed with the debug key | The `release` build type still sets `signingConfig` to `debug`, and it overrides the flavor's config |
| `Keystore file not found` during `dev` build | The `prod` signing config reads `key.properties` without checking that the file exists |
| `No profile for team matching ... found` on iOS | `PROVISIONING_PROFILE_SPECIFIER` names a profile that is not installed on the build machine |
| `The Xcode project does not define custom schemes` or scheme not found | The scheme name does not match the `--flavor` value, or the scheme is not marked Shared |
| Store rejects upload with "version code already used" | The same build number was submitted earlier for that application ID |
| Update fails on a tester's phone with a signature mismatch | The earlier build was signed with a different key. Uninstall it first, or sign both with the same key |
Exercise
Your CI produces a prod release APK, but apksigner verify --print-certs shows the debug certificate. Gradle has a prod signing config and productFlavors.prod.signingConfig points to it. What do you check first, and why?
Show answer
Check buildTypes { release { ... } } in android/app/build.gradle.kts. The Flutter template sets signingConfig = signingConfigs.getByName("debug") there. A build type's signing config takes priority over a flavor's, so the flavor's prod config is ignored. Remove that line so the flavor's config applies to release builds.
Exercise
A teammate without access to the production keystore cannot run flutter run --flavor dev and gets a "file not found" error from Gradle. What is wrong with the signing setup, and how do you fix it?
Show answer
The prod signing config reads storeFile from key.properties unconditionally, and Gradle evaluates every signing config even when you build dev. Wrap the property reads in a check that key.properties exists, as in the chapter's example, so the prod config stays empty on machines that lack the key.