TExcelTAD

Signing Configs & Keystores

Android refuses to install an unsigned APK, and Google Play identifies an app's updates by the key that signs them. A signing setup that works on a laptop but leaks the key, or that only works on one laptop, is one of the most expensive mistakes in mobile release engineering. This lesson sets it up properly.

Keystores, keys and aliases

A keystore (.jks or PKCS#12 .p12) is a password-protected file holding one or more private keys, each under an alias with its own password and a self-signed certificate. The JDK's keytool creates and inspects them.

Every machine with the Android SDK has a debug keystore, created automatically with well-known passwords (android). The image for this course also contains a demo release keystore, ~/release.jks:

bash · try it yourself
keytool -list -v -keystore ~/.android/debug.keystore -storepass android | grep -E '^(Keystore type|Alias name|Owner)'
echo "---"
keytool -list -v -keystore ~/release.jks -storepass learner-pass | grep -E '^(Keystore type|Alias name|Owner|Signature algorithm|Subject Public Key)'
Keystore type: PKCS12
Alias name: androiddebugkey
Owner: C=US, O=Android, CN=Android Debug
---
Keystore type: PKCS12
Alias name: release
Owner: CN=Ada Lovelace, O=Example, C=GB
Signature algorithm name: SHA384withRSA
Subject Public Key Algorithm: 2048-bit RSA key

Warning: that demo keystore and its password learner-pass are printed in a public course. That's fine for a container that is thrown away after each session, and exactly what you must never do with a real key.

Creating your own is one keytool command. -dname answers the interactive questions, so it works in scripts:

bash · try it yourself
keytool -genkeypair -keystore upload.jks -alias upload -keyalg RSA -keysize 2048 -validity 10000 \
  -storepass s3cret-store -keypass s3cret-store -dname "CN=Example Apps, O=Example, C=GB" 2>&1
keytool -list -keystore upload.jks -storepass s3cret-store | grep -E 'entr|PrivateKeyEntry' | sed 's/, [A-Z][a-z]* [0-9]*, [0-9]*,/, <date>,/'
Generating 2,048 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 10,000 days
	for: CN=Example Apps, O=Example, C=GB
Your keystore contains 1 entry
upload, <date>, PrivateKeyEntry, 

-validity 10000 (about 27 years) is not paranoia: Play requires app signing certificates to be valid until at least 22 October 2033, and an app can't change its signing key casually.

Upload key vs app signing key

With Play App Signing (mandatory for new apps since App Bundles became required in 2021), there are two keys:

Key Who holds it Used for
App signing key Google, in its key management service signing the APKs Play generates and delivers to devices
Upload key you (CI) signing the .aab you upload; Play checks it, then re-signs

If your upload key leaks or is lost, you ask Play support to register a new one. If you managed the app signing key yourself and lost it, you could never update the app again. That's the main reason to let Play hold it.

signingConfigs without secrets in the repository

A signingConfig needs four values: the keystore file, its password, the alias and the key password. None of them belong in build.gradle.kts. Read them from Gradle properties, which you can feed from ~/.gradle/gradle.properties on a laptop and from environment variables on CI. Gradle turns any environment variable named ORG_GRADLE_PROJECT_<name> into the project property <name>:

kotlin
android {
    signingConfigs {
        create("release") {
            val storePath = providers.gradleProperty("releaseStoreFile").orNull
            if (storePath != null) {
                storeFile = file(storePath)
                storePassword = providers.gradleProperty("releaseStorePassword").get()
                keyAlias = providers.gradleProperty("releaseKeyAlias").get()
                keyPassword = providers.gradleProperty("releaseKeyPassword").get()
            }
        }
    }
    buildTypes {
        release { signingConfig = signingConfigs.getByName("release") }
    }
}

The session runs the same release build twice: once like a fresh clone without credentials, once like CI with the four variables set:

bash · try it yourself
mkdir -p hello/app/src/main && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }

android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
    signingConfigs {
        create("release") {
            val storePath = providers.gradleProperty("releaseStoreFile").orNull
            if (storePath != null) {
                storeFile = file(storePath)
                storePassword = providers.gradleProperty("releaseStorePassword").get()
                keyAlias = providers.gradleProperty("releaseKeyAlias").get()
                keyPassword = providers.gradleProperty("releaseKeyPassword").get()
            }
        }
    }
    buildTypes {
        release { signingConfig = signingConfigs.getByName("release") }
    }
}
KTS
printf '*.jks\n*.keystore\nkeystore.properties\nlocal.properties\n' > .gitignore
gradle wrapper > /dev/null
echo "=== no credentials"
./gradlew assembleRelease 2>&1 | grep -A2 'What went wrong'
echo "=== CI: credentials from the environment"
export ORG_GRADLE_PROJECT_releaseStoreFile=$HOME/release.jks
export ORG_GRADLE_PROJECT_releaseStorePassword=learner-pass
export ORG_GRADLE_PROJECT_releaseKeyAlias=release
export ORG_GRADLE_PROJECT_releaseKeyPassword=learner-pass
./gradlew assembleRelease | grep -E 'Task :app:(validateSigningRelease|packageRelease)$|^BUILD'
ls app/build/outputs/apk/release/
./gradlew -q signingReport | grep -A4 'Variant: release
  

 | grep -E 'Variant|Config|Store|Alias'
=== no credentials
* What went wrong:
Execution failed for task ':app:validateSigningRelease' (registered by plugin 'com.android.internal.application').
> Keystore file not set for signing config release
=== CI: credentials from the environment
> Task :app:validateSigningRelease
> Task :app:packageRelease
BUILD SUCCESSFUL in 1s
app-release.apk
output-metadata.json
Variant: release
Config: release
Store: /home/learner/release.jks
Alias: release

The failure is the right behaviour: a release build without its key should stop loudly in validateSigningRelease, not produce an unsigned file someone uploads by accident. Debug builds and unit tests still work for everyone, because the if only skips configuring the key.

signingReport lists which key signs each variant; it also prints the certificate's MD5/SHA-1/SHA-256 fingerprints, which you paste into Firebase or Google Sign-In consoles (filtered out above because they differ for every keystore).

Getting the keystore onto CI

CI secrets are strings, and a keystore is a binary file. The usual pattern is base64:

bash
base64 -w0 upload.jks > upload.jks.b64        # once, on your machine; paste into the CI secret store
# in the CI job:
echo "$UPLOAD_KEYSTORE_B64" | base64 -d > "$RUNNER_TEMP/upload.jks"
export ORG_GRADLE_PROJECT_releaseStoreFile="$RUNNER_TEMP/upload.jks"

Write it outside the checkout ($RUNNER_TEMP), never echo it, and delete it at the end of the job. Better still, sign with a key that never leaves a hardware module: Google Cloud KMS, AWS KMS or an HSM, through a custom signing step that calls apksigner with a PKCS#11 provider. Field Notes: Keys, Secrets & Version Codes covers what to do when a key leaks anyway.

Test yourself

How does the session pass the keystore password to Gradle on CI without writing it into any file?

  1. -Dpassword=…
  2. Environment variables named ORG_GRADLE_PROJECT_<name>, read as Gradle properties
  3. local.properties
  4. It's hard-coded in build.gradle.kts

With Play App Signing, what happens if your upload key leaks?

  1. The app can never be updated
  2. You ask Google to register a new upload key; the app signing key held by Google is unaffected
  3. Users must reinstall
  4. Nothing; upload keys aren't checked

Why is failing in validateSigningRelease better than producing an unsigned APK?

  1. Unsigned APKs are larger
  2. An unsigned or wrongly signed artifact can't be installed or uploaded; failing early makes the missing secret obvious
  3. Gradle can't skip tasks
  4. It isn't; unsigned is fine