Sub topic 2
Passing Environment Values and Building Signed Releases
This chapter assumes you can run flutter build from a terminal, that you have a Flutter project with its android and ios folders, and that you know basic Gradle and Xcode settings. On macOS you need Xcode. Android work needs the Android SDK and a JDK that includes keytool.
Passing environment values with --dart-define
A --dart-define flag is a compile-time constant. The build tool stores the value inside the app, so you can switch backends without editing code.
const apiBaseUrl = String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'https://staging.example.com',
);Pass a value at build time:
flutter build appbundle --release --dart-define=API_BASE_URL=https://api.example.comCreating an upload keystore and signing Android releases
Google Play requires a signed build. Create an upload keystore once, and keep it and its passwords backed up:
keytool -genkey -v -keystore ~/upload-keystore.jks \
-keyalg RSA -keysize 2048 -validity 10000 -alias uploadStore the secrets in android/key.properties, and add that file to .gitignore:
storePassword=<store password>
keyPassword=<key password>
keyAlias=upload
storeFile=/path/to/upload-keystore.jksThen load it in 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("release") {
keyAlias = keystoreProperties["keyAlias"] as String
keyPassword = keystoreProperties["keyPassword"] as String
storeFile = keystoreProperties["storeFile"]?.let { file(it) }
storePassword = keystoreProperties["storePassword"] as String
}
}
buildTypes {
release {
signingConfig = signingConfigs.getByName("release")
}
}
}Building an App Bundle and APKs
Google Play accepts an Android App Bundle (AAB). Build one for upload, or build APKs split by CPU architecture for direct installs:
flutter build appbundle --release
flutter build apk --release --split-per-abiShrinking, obfuscating, and keeping symbols
Add --obfuscate to rename Dart symbols, and --split-debug-info to write the symbol files that you need to decode crash reports:
flutter build appbundle --release \
--obfuscate --split-debug-info=build/symbolsStore the build/symbols folder with each release. Without it, obfuscated stack traces cannot be read.
For Android code shrinking, confirm that the release build type sets isMinifyEnabled = true (in Gradle, isMinifyEnabled under buildTypes) if your project template does not already do so.
Signing iOS apps: certificates, profiles, and Xcode
Apple requires a signing certificate and a provisioning profile that match the app's bundle identifier. Open ios/Runner.xcworkspace in Xcode, select the Runner target, and under Signing & Capabilities choose your team and enable "Automatically manage signing." Then build an archive:
flutter build ipa --releaseThe IPA is written to build/ios/ipa. Upload it with Xcode or Transporter.
:::
Exercise
Why must you keep the build/symbols folder after a release with --split-debug-info?
Show answer
The symbol files map obfuscated names back to readable ones. Without them, a crash stack trace from a user's device cannot be decoded.