Skip to main content

Mobile development · Intermediate · 22 min

Sign an Android app and protect your upload key

By Padding · Updated 18 Nov 2026
Ubuntu (eclipse-temurin:25-jdk image) · Temurin 25.0.5 · Android Gradle Plugin 9.5.3 · Gradle 9.9.2 · Build Tools 36.0.0

You will create an upload key with keytool, keep it and its passwords out of Git, sign a small Android app from Gradle with a properties file or environment variables, prove the signature with apksigner, and have a backup and a reset plan for the day the key goes missing.

On this page

Android will not install an app that is not signed, and it will only install an update if the update is signed by the same key as the version already on the phone. That second rule is the one that hurts. Lose the key, and the next version of your app cannot reach your existing users as an update.

Google Play splits the problem in two with Play App Signing, which every app created since August 2021 uses. Google creates and holds the app signing key, the one that signs what users download. You hold an upload key, which only proves to Google that an upload came from you. If the upload key leaks or disappears, Google can register a new one, and your users notice nothing. The app signing key never leaves Google.

So your job is to look after one key well: create it with sensible settings, keep it and its passwords out of your repository, make the build use it without anyone typing secrets into a build file, check the result, and have a backup and a plan for losing it. You do all of that here on a tiny Android app in ~/signing-demo, built from the command line with Gradle so every step is visible. The same Gradle code works in an Android Studio project; only the build command differs (./gradlew instead of gradle).

Step 1 · Install the Android SDK tools and Gradle

If you use Android Studio, you already have an SDK: set ANDROID_HOME to its location (on macOS usually ~/Library/Android/sdk), install Build Tools 36.0.0 and Android 16 (API 36) from its SDK Manager, and skip to the Gradle part below.

Otherwise, install the Android command-line tools by hand. Download the Linux zip from Google, check its SHA-256 checksum against the value published on the Android Studio download page, and unpack it where sdkmanager expects it. On macOS, take the macOS zip and its checksum from the same page, and use shasum -a 256 -c - in place of sha256sum -c -.

Bash
sudo apt update
sudo apt install -y curl unzip
cd /tmp
curl -fLO https://dl.google.com/android/repository/commandlinetools-linux-15859902_latest.zip
echo "4e4c464f145a7512b57d088ac6c278c03c9eea610886b35a5e0804e74eedf583  commandlinetools-linux-15859902_latest.zip" | sha256sum -c -
mkdir -p ~/android-sdk/cmdline-tools
unzip -q -o commandlinetools-linux-15859902_latest.zip -d ~/android-sdk/cmdline-tools
rm -rf ~/android-sdk/cmdline-tools/latest
mv ~/android-sdk/cmdline-tools/cmdline-tools ~/android-sdk/cmdline-tools/latest
rm commandlinetools-linux-15859902_latest.zip

Tell your shell where the SDK lives, now and for future logins:

Bash
export ANDROID_HOME=$HOME/android-sdk
export PATH=$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/build-tools/36.0.0:$PATH
echo 'export ANDROID_HOME=$HOME/android-sdk' >> ~/.profile
echo 'export PATH=$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/build-tools/36.0.0:$PATH' >> ~/.profile

Accept the SDK licences, then install the two packages this tutorial needs: Build Tools 36.0.0, the version Android Gradle Plugin 9.5 uses by default and the home of apksigner, and the Android 16 (API 36) platform to compile against. Read the licences first with sdkmanager --licenses on its own if you want to see them; feeding it yes accepts every one.

Bash
sdkmanager --licenses < <(yes) > /dev/null
sdkmanager "build-tools;36.0.0" "platforms;android-36"

Android Gradle Plugin 9.5 needs Gradle 9.6 or newer. Install Gradle 9.9.2 the same way as in the Gradle tutorial, with its checksum:

Bash
cd /tmp
curl -fLO https://services.gradle.org/distributions/gradle-9.9.2-bin.zip
echo "971fcd7150ca4ba04852c0b2ca0d2d0ab9468e14b03473eab19528809914c76b  gradle-9.9.2-bin.zip" | sha256sum -c -
sudo mkdir -p /opt/gradle
sudo unzip -q -o -d /opt/gradle gradle-9.9.2-bin.zip
rm gradle-9.9.2-bin.zip
export PATH=/opt/gradle/gradle-9.9.2/bin:$PATH
echo 'export PATH=/opt/gradle/gradle-9.9.2/bin:$PATH' >> ~/.profile

Tools unpacked by hand get no apt security updates. Update the SDK packages with sdkmanager --update, and upgrade Gradle by repeating the download with the new version and its checksum. On prepaid data, do this step on Wi-Fi: the SDK packages and the first build download a few hundred megabytes, and later builds reuse the caches in ~/android-sdk and ~/.gradle.

Check it worked

Bash
sdkmanager --list_installed
apksigner --version
gradle --version

sdkmanager lists build-tools;36.0.0 and platforms;android-36. apksigner --version prints a version number (something like 0.9), and gradle --version starts with Gradle 9.9.2.

Step 2 · Create a small app and see an unsigned build

You need something to sign. Create a throwaway project with one app module. The app has no code, only a manifest with a label, which is enough for Gradle to package an APK and keeps the first download small.

Bash
mkdir -p ~/signing-demo/app/src/main
cd ~/signing-demo
cat > settings.gradle.kts <<'KTS'
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "signing-demo"
include(":app")
KTS
cat > build.gradle.kts <<'KTS'
plugins {
    id("com.android.application") version "9.5.3" apply false
}
KTS
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:label="Signing demo" android:hasCode="false" />
</manifest>
XML
cat > app/build.gradle.kts <<'KTS'
plugins {
    id("com.android.application")
}

android {
    namespace = "dev.example.signingdemo"
    compileSdk = 36

    defaultConfig {
        applicationId = "dev.example.signingdemo"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"
    }
}
KTS

Build the release variant. Gradle downloads the Android Gradle Plugin the first time, so this run is the slow one:

Bash
gradle :app:assembleRelease
ls app/build/outputs/apk/release/

Debug builds are signed automatically with a debug key that Android tools create in ~/.android/debug.keystore. Release builds are not, because no store accepts the debug key. With no signing set up, Gradle writes app-release-unsigned.apk. Ask apksigner about it:

Bash
apksigner verify app/build/outputs/apk/release/app-release-unsigned.apk

Check it worked

The build ends with BUILD SUCCESSFUL, the folder holds app-release-unsigned.apk, and apksigner verify fails with an error saying the APK is not signed (something like DOES NOT VERIFY and Missing META-INF/MANIFEST.MF or No signatures). That failure is the point: this file cannot be installed or uploaded.

Step 3 · Create the upload keystore outside the project

A keystore is a password-protected file that holds one or more private keys and their certificates. Keep it outside the project folder, so no git add . can ever pick it up. umask 077 in a subshell makes the folder and the keystore readable only by you from the moment they exist, rather than fixing the permissions afterwards.

Replace the -dname values with your own name or company and your two-letter country code. They end up in the certificate, which Google and API providers can see; they are not shown to users.

Bash
(
  umask 077
  mkdir -p ~/.android-keys
  keytool -genkeypair -v \
    -keystore ~/.android-keys/signing-demo-upload.jks \
    -storetype PKCS12 -alias upload \
    -keyalg RSA -keysize 4096 -validity 10000 \
    -dname "CN=Signing Demo Upload, O=Example Ltd, C=NG"
)

keytool asks for a keystore password twice and does not echo it, so the password never lands in your shell history. Use a long random password from a password manager and save it there straight away under a clear name such as "signing-demo upload keystore".

What the options mean:

  • -storetype PKCS12 is the standard format and the JDK default. keytool gives the key the same password as the store, which is what most tools expect from a PKCS12 file, so it asks only once. Older guides show JKS files with two separate passwords; Gradle reads both formats.
  • -keyalg RSA -keysize 4096 meets Google's minimum of RSA 2048 for upload keys with room to spare.
  • -validity 10000 is in days, about 27 years. Google recommends at least 25 years, because once a key expires, updates signed with it stop installing smoothly.
  • -alias upload is the name of the key inside the file. Gradle and Play Console both ask for it.

Check it worked

Bash
ls -ld ~/.android-keys
ls -l ~/.android-keys/signing-demo-upload.jks

The folder shows drwx------ and the keystore shows -rw-------: only your user can read either. keytool printed a line like Generating 4,096 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 10,000 days.

Step 4 · Read the keystore and export the upload certificate

Look inside the file to confirm it holds what you think it holds. keytool asks for the password again:

Bash
keytool -list -v -keystore ~/.android-keys/signing-demo-upload.jks -alias upload

Note three things in the output: Entry type: PrivateKeyEntry (the file holds a private key, not just a certificate), the Valid from ... until ... line, and the SHA256: fingerprint, a string of hex pairs separated by colons. The fingerprint identifies your upload certificate. Services such as Google Sign-In and Firebase ask for fingerprints, and the Play Console shows the same value for the upload key once you have uploaded a release.

Now export the certificate, the public half of the key, as a PEM file. This is the file Google asks for if you ever need to reset the upload key, and it is safe to share and keep alongside your project notes:

Bash
keytool -exportcert -rfc \
  -keystore ~/.android-keys/signing-demo-upload.jks \
  -alias upload -file ~/signing-demo/upload_certificate.pem

Check it worked

Bash
head -1 ~/signing-demo/upload_certificate.pem
keytool -printcert -file ~/signing-demo/upload_certificate.pem | grep -E "Owner|SHA256"

The first line is -----BEGIN CERTIFICATE-----. keytool -printcert needs no password, because a certificate holds no secret, and shows the same owner and SHA256 fingerprint as the listing above.

Step 5 · Point Gradle at the key without committing secrets

A signing config needs four values: the keystore path, its password, the key alias and the key password. Android Studio's wizard writes them straight into build.gradle.kts, which puts a password in your Git history. Keep them in a keystore.properties file that Git ignores instead, and fall back to environment variables when the file is missing, which is how a build server will provide them.

First make Git ignore the secrets file and any keystore, so the rules are in place before the file exists:

Bash
cd ~/signing-demo
cat > .gitignore <<'EOF'
.gradle/
build/
local.properties
keystore.properties
*.jks
*.keystore
EOF

Create keystore.properties empty and private, then open it in your editor and add the values. Writing it in an editor rather than with echo keeps the password out of your shell history.

Bash
install -m 600 /dev/null ~/signing-demo/keystore.properties
Text
storeFile=/home/you/.android-keys/signing-demo-upload.jks
storePassword=the-password-from-your-password-manager
keyAlias=upload
keyPassword=the-same-password

Replace /home/you with your home folder (echo $HOME prints it; on macOS it is /Users/you) and both passwords with your keystore password. Gradle does not expand ~, so the path must be absolute. With a PKCS12 keystore the key password is the same as the store password.

Now replace the app's build file. It reads keystore.properties if it exists, and otherwise four UPLOAD_* environment variables, and uses the result for release builds:

Bash
cat > ~/signing-demo/app/build.gradle.kts <<'KTS'
import java.util.Properties

plugins {
    id("com.android.application")
}

// Signing values come from keystore.properties (ignored by Git) or,
// when that file is missing, from UPLOAD_* environment variables.
val keystorePropertiesFile = rootProject.file("keystore.properties")
val keystoreProperties = Properties().apply {
    if (keystorePropertiesFile.exists()) {
        keystorePropertiesFile.inputStream().use { load(it) }
    }
}

fun signingValue(property: String, envVar: String): String? =
    keystoreProperties.getProperty(property)
        ?: providers.environmentVariable(envVar).orNull

android {
    namespace = "dev.example.signingdemo"
    compileSdk = 36

    defaultConfig {
        applicationId = "dev.example.signingdemo"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"
    }

    signingConfigs {
        create("upload") {
            signingValue("storeFile", "UPLOAD_STORE_FILE")?.let { storeFile = file(it) }
            storePassword = signingValue("storePassword", "UPLOAD_STORE_PASSWORD")
            keyAlias = signingValue("keyAlias", "UPLOAD_KEY_ALIAS")
            keyPassword = signingValue("keyPassword", "UPLOAD_KEY_PASSWORD")
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("upload")
        }
    }
}
KTS

The build file names the variables but holds no secret, so it is safe to commit. Before you build, ask Gradle which key each variant will use. signingReport reads the keystore without building anything:

Bash
cd ~/signing-demo
gradle :app:signingReport

Check it worked

The report has a block for Variant: release with Config: upload, a Store: line pointing at your keystore, Alias: upload and a SHA-256 fingerprint that matches the one keytool -list showed. The debug variant still uses ~/.android/debug.keystore. If the release block says the store file is missing or the password is wrong, fix keystore.properties before going on.

Step 6 · Build a signed release and check it with apksigner

Build the release APK again, and the app bundle (.aab) that Google Play wants for uploads:

Bash
cd ~/signing-demo
gradle :app:assembleRelease :app:bundleRelease
ls app/build/outputs/apk/release/ app/build/outputs/bundle/release/

The APK is now app-release.apk, without -unsigned, and the bundle is app-release.aab. Check the APK's signature and print the certificate that signed it:

Bash
apksigner verify -v --print-certs app/build/outputs/apk/release/app-release.apk

To compare that fingerprint with the keystore, turn keytool's colon-separated form into apksigner's lowercase form. keytool asks for the password:

Bash
keytool -list -v -keystore ~/.android-keys/signing-demo-upload.jks -alias upload \
  | grep 'SHA256:' | awk '{print $2}' | tr -d ':' | tr 'A-F' 'a-f'

apksigner checks APKs only. A bundle is signed like a JAR file, so check the .aab with keytool, which needs no password for this:

Bash
keytool -printcert -jarfile app/build/outputs/bundle/release/app-release.aab | grep -E "Owner|SHA256"

Check it worked

apksigner prints Verifies, then at least one Verified using v2 scheme (APK Signature Scheme v2): true or v3 scheme line set to true, and Signer #1 certificate DN: CN=Signing Demo Upload, O=Example Ltd, C=NG. Its Signer #1 certificate SHA-256 digest: value equals the string the keytool pipeline printed. The bundle shows the same owner and SHA256 fingerprint as keytool -list. If the fingerprints differ, the build used a different key: run signingReport again and check the path in keystore.properties.

Step 7 · Sign on a build server with environment variables

A build server has no keystore.properties. It gets the keystore file and the password from its secret store, and the build file you wrote reads them from the UPLOAD_* variables. Try that path on your own machine by moving the properties file aside for a moment:

Bash
cd ~/signing-demo
mv keystore.properties keystore.properties.off

Set the variables. read -rs asks for the password without echoing it and without putting it in your history; never put it on the command line itself.

Bash
export UPLOAD_STORE_FILE="$HOME/.android-keys/signing-demo-upload.jks"
export UPLOAD_KEY_ALIAS=upload
printf 'Keystore password: '; read -rs UPLOAD_STORE_PASSWORD; echo
export UPLOAD_STORE_PASSWORD
export UPLOAD_KEY_PASSWORD="$UPLOAD_STORE_PASSWORD"

Rebuild from clean and verify:

Bash
gradle clean :app:assembleRelease
apksigner verify --print-certs app/build/outputs/apk/release/app-release.apk | grep "certificate DN"

Then put the file back and clear the variables, so your shell does not keep the password around:

Bash
mv keystore.properties.off keystore.properties
unset UPLOAD_STORE_FILE UPLOAD_STORE_PASSWORD UPLOAD_KEY_ALIAS UPLOAD_KEY_PASSWORD

On a hosted CI service, store the keystore as a base64-encoded secret, decode it to a file with mode 600 at the start of the job, map the password secret to UPLOAD_STORE_PASSWORD, and delete the file at the end. Most services hide secret values in logs, but do not print them yourself.

Check it worked

With no keystore.properties present, the build still produced app-release.apk, and apksigner printed Signer #1 certificate DN: CN=Signing Demo Upload, O=Example Ltd, C=NG. After the last command, ls ~/signing-demo/keystore.properties finds the file again and echo "${UPLOAD_STORE_PASSWORD:-unset}" prints unset.

Step 8 · Back up the key and plan for losing it

Play App Signing protects your users from a lost upload key, but a reset takes time, and until it is done you cannot ship a fix. A laptop stolen, a disk that fails, a shared office machine wiped: all of these happen, and a backup made today turns a lost week into a lost hour.

Keep at least two copies of the keystore and the password, in different places, and never in the same unencrypted folder:

  • The password, the alias and the certificate fingerprint in your password manager. Most password managers also accept file attachments, which is a good home for the keystore itself.
  • A second copy of the keystore on an encrypted USB drive or in an encrypted archive kept somewhere other than your laptop.
  • Never the keystore in the Git repository, a chat message or an email.

To copy it to a backup drive, keep permissions intact and check that the copy is identical. Replace /media/you/backup with your drive's path:

Bash
cp -p ~/.android-keys/signing-demo-upload.jks /media/you/backup/
sha256sum ~/.android-keys/signing-demo-upload.jks /media/you/backup/signing-demo-upload.jks

On macOS, use shasum -a 256 in place of sha256sum. Test the backup once by listing it with keytool -list -keystore /media/you/backup/signing-demo-upload.jks and the password from your password manager. A backup you have never opened is a guess.

If the upload key is lost or leaked

Act the same day if you think someone else has the key or the password. Google's documented route is:

  1. Create a new upload keystore with the same keytool -genkeypair command and a new password.
  2. Export its certificate with keytool -exportcert -rfc, as in the earlier step.
  3. In Play Console, open your app and go to Protected with Play, then Play Store protection, then Manage Play app signing. In the Upload key certificate section, choose Request upload key reset, give the reason and upload the PEM file.
  4. Point keystore.properties and your CI secrets at the new keystore, and delete the old one everywhere.

The new upload certificate does not work straight away; Expo's guide says it becomes valid about 72 hours after the reset. Your users keep receiving updates as normal afterwards, because the app signing key that Google holds has not changed.

If you build with EAS

With Expo's EAS Build, you can let EAS generate the Android keystore and keep it on Expo's servers, so you never handle the file. The same rules still apply: that keystore is your upload key once Play App Signing is on, and eas credentials lets you download it to a credentials.json file for a backup or a reset. If you keep credentials locally instead, treat credentials.json and the keystore exactly like keystore.properties here: ignored by Git, mode 600, backed up. See the EAS tutorial for the build itself.

Apps shared outside Google Play

In many markets, apps also travel as APK files shared from phone to phone or downloaded from your own site, which saves users data compared with the store. An APK you sign with your upload key and a copy installed from Play carry different signatures, because Play re-signs with the app signing key. A user cannot install one over the other as an update. If you distribute both ways, pick one channel per user group, or give Play your own app signing key at the start so both channels share it.

Check it worked

You can name where each copy of the keystore and its password lives, sha256sum printed the same hash for both copies, and keytool -list opened the backup with the password from your password manager. upload_certificate.pem is saved with your project notes, ready for a reset request.

When you are done with the demo, remove what this tutorial created. The SDK and Gradle in ~/android-sdk and /opt/gradle can stay if you will build Android apps; delete the demo key, because nothing should be signed with it:

Bash
rm -rf ~/signing-demo
rm -f ~/.android-keys/signing-demo-upload.jks
rmdir ~/.android-keys 2>/dev/null || true

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.