Verifying APKs: apksigner & zipalign
"The build is green" doesn't mean the artifact is right. A release pipeline should check what it produced before uploading: is it signed, with the expected key and schemes, and correctly aligned? Two build-tools binaries do it: apksigner and zipalign.
Signature schemes in one table
| Scheme | Since | Covers | Notes |
|---|---|---|---|
| v1 (JAR signing) | Android 1 | each zip entry separately | slow to verify; doesn't protect zip metadata |
| v2 | Android 7.0 (API 24) | the whole file as one blob | fast; any byte change breaks it |
| v3 / v3.1 | Android 9 / 13 | v2 + key rotation lineage | lets an app move to a new signing key |
| v4 | Android 11 | a separate .idsig file |
for incremental installs (adb install --incremental) |
AGP picks schemes from minSdk: v1 is only needed when the app must install below API 24. With minSdk = 24 you get v2, as the session shows.
Verify a release APK
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") {
storeFile = file(providers.gradleProperty("releaseStoreFile").get())
storePassword = providers.gradleProperty("releaseStorePassword").get()
keyAlias = providers.gradleProperty("releaseKeyAlias").get()
keyPassword = providers.gradleProperty("releaseKeyPassword").get()
}
}
buildTypes { release { signingConfig = signingConfigs.getByName("release") } }
}
KTS
export ORG_GRADLE_PROJECT_releaseStoreFile=$HOME/release.jks ORG_GRADLE_PROJECT_releaseKeyAlias=release
export ORG_GRADLE_PROJECT_releaseStorePassword=learner-pass ORG_GRADLE_PROJECT_releaseKeyPassword=learner-pass
gradle wrapper > /dev/null
./gradlew -q assembleRelease
APK=app/build/outputs/apk/release/app-release.apk
zipalign -c -v 4 $APK | tail -1
apksigner verify --print-certs -v $APK | grep -vE 'digest:'Verification successful
Verifies
Verified using v1 scheme (JAR signing): false
Verified using v2 scheme (APK Signature Scheme v2): true
Verified using v3 scheme (APK Signature Scheme v3): false
Verified using v3.1 scheme (APK Signature Scheme v3.1): false
Verified using v4 scheme (APK Signature Scheme v4): false
Verified for SourceStamp: false
Number of signers: 1
Signer #1 certificate DN: CN=Ada Lovelace, O=Example, C=GB
Signer #1 key algorithm: RSA
Signer #1 key size (bits): 2048
The grep -v drops the certificate and public-key digests (SHA-256, SHA-1, MD5): they differ for every keystore. In a real pipeline those digests are exactly what you check, against the SHA-256 of your known upload certificate:
EXPECTED=0fe0f22945ee3862dfaa3c1e4cde75f478c34718dae10b2e16fd2acf7c6c66c9 # from a trusted place, not from the build
apksigner verify --print-certs "$APK" | grep -q "certificate SHA-256 digest: $EXPECTED" \
|| { echo "APK signed with an unexpected key"; exit 1; }zipalign -c 4 checks that every uncompressed entry starts at a 4-byte boundary (and with -p, that .so files sit on page boundaries), so the system can mmap them directly instead of copying them into RAM.
Breaking it on purpose
v2 signatures cover the whole file. Adding one innocent file to the zip breaks verification, and so does running zipalign after signing (it moves bytes). The right order is always align, then sign:
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" }
}
KTS
gradle wrapper > /dev/null
./gradlew -q assembleRelease
cp app/build/outputs/apk/release/app-release-unsigned.apk unsigned.apk
echo "=== unsigned"
apksigner verify unsigned.apk 2>&1 | head -1
echo "=== align, then sign by hand"
zipalign -P 16 -f 4 unsigned.apk aligned.apk
apksigner sign --ks ~/release.jks --ks-key-alias release --ks-pass pass:learner-pass --key-pass pass:learner-pass \
--out signed.apk aligned.apk
apksigner verify signed.apk && echo "signed.apk verifies"
apksigner verify -v signed.apk | grep -E 'v[12] scheme'
zipalign -c 4 signed.apk && echo "signed.apk is aligned"
echo "=== tamper: add a file after signing"
cp signed.apk tampered.apk
echo "free coins" > extra.txt && zip -q tampered.apk extra.txt
apksigner verify tampered.apk 2>&1 | head -2=== unsigned
DOES NOT VERIFY
=== align, then sign by hand
signed.apk verifies
Verified using v1 scheme (JAR signing): false
Verified using v2 scheme (APK Signature Scheme v2): true
signed.apk is aligned
=== tamper: add a file after signing
DOES NOT VERIFY
ERROR: JAR signer RELEASE.RSA: JAR signature META-INF/RELEASE.SF indicates the APK is signed using APK Signature Scheme v2 but no such signature was found. Signature stripped?
Read the tamper error closely. zip rewrote the archive and silently dropped the APK Signing Block (it isn't a zip entry, so zip doesn't know it exists). Run by hand, apksigner sign still wrote v1 JAR signature files into META-INF/ (unlike AGP's signing step; pass --v1-signing-enabled false to skip them). Verification reports v1 scheme: false because no device that can install this APK needs them, but once zip has dropped the v2 block, apksigner falls back to those files, and they record that a v2 signature should exist: hence "Signature stripped?". Either way, the file no longer verifies.
apksigner sign with --ks/--ks-key-alias is what AGP does internally (through the same apksig library). You'll do it by hand when signing happens outside Gradle: an HSM-backed signing service, or re-signing an artifact that was built once and promoted through environments.
Note:
apksigner signalso writes a.idsigfile next to the output when v4 signing applies. Don't ship it in the APK; keep it for incremental installs or delete it.
A verification step for CI
Put the checks into the release job, after the build and before upload, so a wrong artifact never leaves CI:
set -euo pipefail
APK=app/build/outputs/apk/free/release/app-free-release.apk
zipalign -c -P 16 4 "$APK"
apksigner verify --min-sdk-version 24 --print-certs "$APK" > signing.txt
grep -q 'Verified using v2 scheme (APK Signature Scheme v2): true' signing.txt
grep -q "certificate SHA-256 digest: $EXPECTED_UPLOAD_CERT_SHA256" signing.txt
aapt2 dump badging "$APK" | grep -q "versionCode='$EXPECTED_VERSION_CODE'"
! aapt2 dump badging "$APK" | grep -q application-debuggableThe Mini Project runs a version of this script for real.
Test yourself
Why is v1 (JAR) signing absent from the APK built with minSdk 24?
- apksigner doesn't support v1 any more
- Every device from API 24 verifies v2, so AGP skips v1 when minSdk ≥ 24
- R8 removed it
- v1 is only for App Bundles
In which order must zipalign and apksigner run?
- Sign, then align
- Align, then sign: the v2 signature covers every byte, so aligning afterwards breaks it
- Order doesn't matter
- Only one of them is needed
What should a release pipeline compare the APK's certificate SHA-256 digest against?
- The digest printed by the same build
- A known value of the upload certificate stored outside the build
- The versionCode
- The keystore password