Sub topic 6
Creating an Upload Keystore and Signing Android Releases
Android only installs and updates apps that carry a digital signature. This chapter explains how Android signing works, how to create an upload keystore, and how to configure Gradle to sign release builds. It also covers the mistakes that most often break a release.
Prerequisites assumed: you can run flutter commands in a terminal, you have a Flutter project that builds in debug mode, and you have a JDK available (the one bundled with Android Studio is enough). You do not need to know Gradle in depth.
Why Android Requires Signing
A signature proves two things to the device. The package was produced by the holder of a specific private key, and its contents have not changed since signing. When you ship an update, Android checks that the new package is signed by the same identity as the installed one. If the identities differ, the update is rejected.
Debug builds are signed automatically with a generic debug key stored on your machine. Google Play does not accept packages signed that way. You need your own key for release builds.
Upload key and app signing key
Google Play uses two keys with different roles.
| Key | Who holds it | Purpose |
|---|---|---|
| Upload key | You | Proves to Play Console that an uploaded bundle comes from you |
| App signing key | Google (Play App Signing) | Signs the APKs that users actually install |
When you enroll in Play App Signing, which is the default for new apps, Google re-signs your app with the app signing key. If you lose your upload key, you can ask Google to register a new one. If you lose an app signing key that you managed yourself, you can never update that app. This is why the upload-key model is safer, and why this chapter creates an upload keystore.
Step 1: Create the Upload Keystore
A keystore is a file that holds one or more private keys, protected by passwords. Each key inside is identified by an alias. Create one with keytool, which ships with the JDK:
keytool -genkey -v -keystore ~/upload-keystore.jks \
-keyalg RSA -keysize 2048 -validity 10000 -alias uploadThe command prompts for a keystore password, a key password, and some identity details such as your name and organization. The identity details can be anything reasonable. Players never see them.
The options mean the following:
-keystoreis the output file. The.jksextension is a convention.-keyalg RSA -keysize 2048selects the algorithm and key length that Play accepts.-validity 10000makes the key valid for about 27 years. Play requires a validity that extends well beyond the app's expected life, so avoid short values.-alias uploadnames the key inside the keystore. You will refer to this name in the signing configuration.
If keytool is not on your path, use the full path to the JDK bundled with Android Studio, or run flutter doctor -v to see where Flutter found the Java installation.
Store the file safely
Treat the keystore like a password manager vault.
- Keep it outside the project directory, as in
~/upload-keystore.jks. This makes accidental commits much less likely. - Back it up in at least two places, for example an encrypted drive and your team's secret manager.
- Record the passwords and alias in the same secret manager.
Step 2: Reference the Keystore from key.properties
Gradle needs the passwords and the file location at build time. Rather than writing them into build files that are committed, put them in a separate properties file:
# android/key.properties (never commit this file)
storePassword=<store password>
keyPassword=<key password>
keyAlias=upload
storeFile=/path/to/upload-keystore.jksPlace this file at android/key.properties. Then add it to android/.gitignore, or to your root .gitignore:
key.properties
*.jks
*.keystoreFlutter's generated android/.gitignore already lists key.properties in recent templates. Check it anyway instead of assuming.
Use an absolute path for storeFile, or a path your build resolves reliably. A relative path is interpreted relative to android/app, which is a frequent source of "keystore file not found" errors.
Step 3: Configure Gradle Release Signing
Open android/app/build.gradle.kts. Newer Flutter templates use the Kotlin DSL. Older projects use build.gradle with Groovy syntax, and the structure is the same with different punctuation.
The Kotlin version has three parts. It loads key.properties if the file exists, declares a signing configuration named release, and attaches that configuration to the release build type:
// 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")
}
}
}Place the import lines at the top of the file and merge the android block with the one that already exists. Do not add a second android block. Inside the existing block, the generated buildTypes { release { ... } } section usually points to the debug signing config with a comment saying "Add your own signing config". Replace that line with the one above.
How the pieces connect
rootProject.file("key.properties")resolves relative to theandroiddirectory, which is the Gradle root project. That is why the file lives atandroid/key.properties.- The
as Stringcasts fail with a null error if a key is missing from the file. This is useful during setup because you find out immediately. storeFilegoes throughfile(it), which turns the path string into aFileobject.
Step 4: Build and Verify
Build a release bundle:
flutter build appbundle --releaseThe output is build/app/outputs/bundle/release/app-release.aab. Later chapters cover artifacts in more detail. For now, the goal is to confirm that the build succeeds and the bundle is signed with your key.
Check the signature with keytool:
keytool -printcert -jarfile build/app/outputs/bundle/release/app-release.aabThe output lists the signer's certificate, including SHA-256 and SHA-1 fingerprints. Compare these with the fingerprints of your keystore:
keytool -list -v -keystore ~/upload-keystore.jks -alias uploadIf the fingerprints match, the bundle is signed with your upload key. If the first command reports that the file is not signed, Gradle used no signing configuration, and you should recheck the buildTypes block.
Common Errors and Fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| `Keystore file ... not found` | Relative or wrong `storeFile` path | Use an absolute path in `key.properties` |
| `Keystore was tampered with, or password was incorrect` | Wrong store password, or a typo in `key.properties` | Retype the password and check for trailing spaces |
| `Cannot recover key` | Key password does not match the alias | Check `keyPassword` and `keyAlias` |
| `null cannot be cast to non-null type kotlin.String` | A property is missing in `key.properties` | Add all four properties |
| Play Console: "signed with the wrong key" | A different keystore than the registered upload key | Use the original keystore, or request an upload key reset |
| Release build is signed with the debug key | `signingConfig` still points to `debug` | Update the `release` build type |
Edge cases worth knowing
- Missing key.properties on CI or a teammate's machine. The
exists()check means the build proceeds to the cast and fails with an error. If you want local debug builds to work for people without the keystore, wrap thesigningConfigscreation in a check for the file, and only then assign the config to the release build type. - Different passwords for store and key.
keytoollets them differ, and PKCS12 keystores, the default in newer JDKs, effectively require the same password. If you see a key password error on a PKCS12 file, try using the store password for both. - Aliases are case-sensitive on some platforms. Copy the alias exactly as shown by
keytool -list. - Release builds from
flutter run --release. This uses the same signing configuration. If it fails with a keystore error, the problem is in the Gradle setup and not in Flutter.
Environment Variables for CI
On a CI server you usually do not have a file at a fixed path. A common pattern stores the keystore as a base64-encoded secret, decodes it into the workspace, and writes key.properties from secrets before building:
echo "$KEYSTORE_BASE64" | base64 --decode > android/upload-keystore.jks
cat > android/key.properties <<EOF
storePassword=$STORE_PASSWORD
keyPassword=$KEY_PASSWORD
keyAlias=upload
storeFile=$PWD/android/upload-keystore.jks
EOFThe workspace is discarded after the job, so the secrets do not persist. Chapter 14 returns to this in the context of a full pipeline.
Summary of the Workflow
- Generate the upload keystore once, with
keytool. - Back it up and store its passwords in a secret manager.
- Write
android/key.propertiesand keep it out of version control. - Load it in
android/app/build.gradle.ktsand attach areleasesigning config to the release build type. - Build with
flutter build appbundle --releaseand verify the fingerprints.
Exercise
You run flutter build appbundle --release and the build succeeds, but keytool -printcert -jarfile on the resulting .aab shows no signer. What is the most likely cause, and where do you fix it?
Show answer
The release build type is not using your release signing configuration, so Gradle left the bundle unsigned or fell back to debug signing. Open android/app/build.gradle.kts, find buildTypes { release { ... } }, and make sure it contains signingConfig = signingConfigs.getByName("release"). Also confirm that the signingConfigs { create("release") { ... } } block exists inside android.
Exercise
A teammate reports Keystore file '/android/app/../upload-keystore.jks' not found, although the file exists in the android folder next to key.properties. What change prevents this class of error?
Show answer
The storeFile value in key.properties is a relative path, and Gradle resolves it differently from what the teammate expected. Replace it with an absolute path, or build the absolute path in CI, for example storeFile=/home/user/project/android/upload-keystore.jks.
Exercise
Why does the tutorial recommend keeping the keystore outside the project directory and out of version control, and what is the recovery path if the upload key is lost but you use Play App Signing?
Show answer
Keeping it outside the repository reduces the chance of committing a secret that would let someone upload builds as you. With Play App Signing, Google holds the app signing key, so a lost upload key can be replaced: you generate a new upload key and ask Google to register it through Play Console support.