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:
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-passare 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:
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>:
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:
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