Sub topic 6
Creating an Upload Keystore and Signing Android Releases
Assume you can run Flutter commands and edit a Flutter project's Android files. You should also have an Android release build that works before adding signing. This chapter focuses on the upload key used for Android release artifacts.
Upload keys and Google Play App Signing
Android requires release artifacts to be signed. A signature proves that an artifact came from the holder of a particular private key.
For Google Play releases, you normally use two keys:
- The app signing key signs the APKs that Google Play delivers to users.
- The upload key signs the App Bundle that you upload to Google Play.
Google Play can keep the app signing key while you keep the upload key. If the upload key is lost, you can request an upload-key reset. If an app signing key is lost, recovery is much more difficult, so do not confuse the two keys.
Create and protect the upload keystore
Run keytool on a machine with a Java Development Kit installed. Choose a secure location and a strong password.
keytool -genkey -v -keystore ~/upload-keystore.jks \
-keyalg RSA -keysize 2048 -validity 10000 -alias uploadThe command creates upload-keystore.jks and a key named upload. Record the keystore password, key password, alias, and file location in your team's password manager.
Back up the keystore securely. Anyone with the private key and its passwords may be able to sign artifacts as your app. Do not put the keystore in Git, a public file share, or a Docker image. Add local secret files to .gitignore.
Give Gradle the signing values
Create android/key.properties:
# android/key.properties (never commit this file)
storePassword=<store password>
keyPassword=<key password>
keyAlias=upload
storeFile=/path/to/upload-keystore.jksUse an absolute path for storeFile, or use a path that is valid on every build machine. In CI, generate this file from encrypted secrets instead of storing it in the repository.
In android/app/build.gradle.kts, load the properties and apply them to the release build type:
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")
}
}
}Flutter projects using build.gradle instead of build.gradle.kts need the equivalent Groovy configuration. Do not mix Kotlin DSL and Groovy syntax.
Build and verify a signed artifact
Build an Android App Bundle for Google Play:
flutter build appbundle --releaseFlutter writes the bundle to build/app/outputs/bundle/release/app-release.aab. For direct device installation, build APKs instead:
flutter build apk --releaseYou can inspect an APK's signing certificate with Android's apksigner tool. Compare its certificate fingerprint with the certificate registered for your upload key. Test the release build on a physical device before uploading it.
Exercise
Which key should you keep private when uploading an App Bundle to Google Play?
Show answer
Keep the upload keystore and its passwords private. Google Play uses its separately managed app signing key to sign the APKs delivered to users.