Sub topic 13
Signing and releasing each flavor
This chapter assumes that you know basic Flutter development, can run flutter build, and have already created Android flavors and matching iOS schemes. It also assumes you understand that a release build must be signed before users can install or update it.
A flavor identifies an environment such as dev, staging, or prod. Signing proves who published the app. These concerns are related, but they are different:
- Configuration selects the API and services.
- A flavor selects the native app variant.
- Signing selects the certificate and distribution identity.
A common release setup uses debug signing for local development and separate release signing for production.
Android release signing
Android signs an app with a keystore. A keystore contains a private key and certificate. The private key must remain protected because users can update an installed app only when future builds use the same signing identity.
Store the keystore outside the repository. Supply its path and passwords through environment variables or a CI secret store.
In android/app/build.gradle.kts, create a release signing configuration and attach it to the release build type:
import java.util.Properties
val keystoreProperties = Properties()
val keystorePropertiesFile = rootProject.file("key.properties")
if (keystorePropertiesFile.exists()) {
keystorePropertiesFile.inputStream().use {
keystoreProperties.load(it)
}
}
android {
signingConfigs {
create("release") {
val storeFilePath = System.getenv("ANDROID_KEYSTORE_PATH")
?: keystoreProperties["storeFile"]?.toString()
if (storeFilePath != null) {
storeFile = file(storeFilePath)
}
storePassword = System.getenv("ANDROID_KEYSTORE_PASSWORD")
?: keystoreProperties["storePassword"]?.toString()
keyAlias = System.getenv("ANDROID_KEY_ALIAS")
?: keystoreProperties["keyAlias"]?.toString()
keyPassword = System.getenv("ANDROID_KEY_PASSWORD")
?: keystoreProperties["keyPassword"]?.toString()
}
}
buildTypes {
getByName("release") {
signingConfig = signingConfigs.getByName("release")
}
}
}The dev and prod flavors can use the same release key when they belong to the same published application, but they normally have different application IDs. For example, com.example.myapp.dev and com.example.myapp are different Android applications and may use different keys.
The resulting variant combines the flavor and build type:
| Command target | Meaning |
|---|---|
| `devDebug` | Development flavor with debug settings |
| `prodRelease` | Production flavor with release optimization and signing |
| `stagingRelease` | Staging flavor prepared for distribution |
iOS schemes and signing
On iOS, a scheme selects a build configuration. Each flavor should map to a configuration with the correct bundle identifier, display name, entitlements, and provisioning profile.
For example:
Devusescom.example.myapp.dev.Stagingusescom.example.myapp.staging.Produsescom.example.myapp.
In Xcode, configure signing for each scheme under the target's Signing & Capabilities settings. Automatic signing can select profiles for a development team. CI commonly uses an App Store Connect API key or an installed distribution certificate and provisioning profile.
Building and checking releases
Build production artifacts explicitly. Do not rely on whichever flavor an IDE last selected.
flutter build appbundle \
--release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json
flutter build ipa \
--release \
--flavor prod \
-t lib/main_prod.dart \
--dart-define-from-file=config/prod.json \
--export-options-plist=ios/ExportOptions-prod.plistBefore uploading, check the artifact's identity:
- Confirm the Android application ID and version.
- Confirm the iOS bundle identifier and version.
- Launch the release build and verify its API endpoint.
- Confirm that production builds do not display a dev label.
- Test an update over the previous production version.
CI release rules
A continuous integration system should receive the flavor, entrypoint, configuration file, and signing credentials as separate inputs. A protected production branch can permit prod builds, while ordinary branches build dev or staging.
Exercise
Why can a production build fail to update an already-published Android app even when its Dart code is correct?
Show answer
It may have been signed with a different key, or it may use a different application ID. Android treats either change as a different application.