Android code signing

Every Android app is signed with a key before it reaches Google Play. AppRelease keeps the keystore for you and hands it to the build.

How it works

You upload the keystore once, with its passwords, under a reference name. A workflow lists that name under android_signing. Before the scripts run, the runner writes the keystore to a temporary path and exports four environment variables; your Gradle file reads them. When the build ends, the file is deleted.

Upload key vs app signing key

With Play App Signing (the default for every app created since 2021), Google holds the key that signs what users install, and you sign uploads with a separate upload key. That upload key is what you give AppRelease. If you lose it, Google can reset it; if you lose the app signing key of an app that opted out of Play App Signing, nobody can.

Generating a keystore

keytool -genkey -v -keystore upload.keystore -storetype JKS \
        -keyalg RSA -keysize 2048 -validity 10000 -alias upload

Keytool asks for a keystore password, then for a key password, then for your name and organisation. The values do not matter to the stores; the passwords do.

Uploading the keystore

  1. In the console open Credentials → Add credential → Android keystore.
  2. Pick the .keystore or .jks file.
  3. Enter the keystore password, the key alias and the key password. AppRelease checks them against the file before saving.
  4. Choose the application it signs, or leave it shared.
  5. The reference name is filled in for you, for example shop_upload_keystore. It is the name apprelease.yaml would use; the file's own name is not.
  6. Click Save.
You need the file only if you write apprelease.yaml yourself. Without the file, the runner signs with the keystore linked to the application, or the shared one.
Keep your own copy. The uploaded keystore cannot be downloaded. Store the file and the passwords in a password manager; every future release has to be signed with the same key.

Referencing it in apprelease.yaml

workflows:
  android-workflow:
    environment:
      android_signing:
        - upload_keystore
VariableValue
AR_KEYSTORE_PATHAbsolute path of the keystore on the runner
AR_KEYSTORE_PASSWORDKeystore password
AR_KEY_ALIASKey alias
AR_KEY_PASSWORDKey alias password

Wiring it into Gradle

Flutter's template reads key.properties; keep that for local builds and add the CI branch:

android {
    signingConfigs {
        create("release") {
            if (System.getenv("CI") != null) {
                storeFile = file(System.getenv("AR_KEYSTORE_PATH"))
                storePassword = System.getenv("AR_KEYSTORE_PASSWORD")
                keyAlias = System.getenv("AR_KEY_ALIAS")
                keyPassword = System.getenv("AR_KEY_PASSWORD")
            } else {
                keyAlias = keystoreProperties["keyAlias"] as String?
                keyPassword = keystoreProperties["keyPassword"] as String?
                storeFile = (keystoreProperties["storeFile"] as String?)?.let { file(it) }
                storePassword = keystoreProperties["storePassword"] as String?
            }
        }
    }
    buildTypes {
        getByName("release") {
            signingConfig = signingConfigs.getByName("release")
        }
    }
}
android {
    signingConfigs {
        release {
            if (System.getenv()["CI"]) {
                storeFile file(System.getenv()["AR_KEYSTORE_PATH"])
                storePassword System.getenv()["AR_KEYSTORE_PASSWORD"]
                keyAlias System.getenv()["AR_KEY_ALIAS"]
                keyPassword System.getenv()["AR_KEY_PASSWORD"]
            } else {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                storePassword keystoreProperties['storePassword']
            }
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

Checking the signature

If Google Play rejects an upload with "your Android App Bundle is signed with the wrong key", compare fingerprints. The one Google expects is shown under Setup → App signing → Upload key certificate; the one you used:

keytool -list -v -keystore upload.keystore -alias upload | grep SHA1