Signing and verifying
An Android device only installs a signed apk, and only updates it with another one that carries the same
signature. ngr sign signs, and ngr verify checks.
Signing with your key
ngr reads PKCS#12 keystores (.p12 or .pfx):
ngr sign --ks release.p12 --ks-pass env:KS_PASS app.apk
It writes app_signed.apk and app_signed.apk.idsig. The input apk is not touched.
If the keystore has more than one key, say which with --ks-key-alias. The password is not simply typed in: you
give it in one of the four forms described in “Passwords”.
If your keystore is JKS, convert it to PKCS#12 first. With the JDK:
keytool -importkeystore -srckeystore release.jks -destkeystore release.p12 -deststoretype PKCS12
Signing for testing
ngr sign --debug-key app.apk
--debug-key signs with a key that nogar creates the first time you ask for it. You do not have to create a
keystore or remember a password.
The debug key
It is a PKCS#12 keystore that nogar stores in your user's configuration folder:
| System | File |
|---|---|
| macOS | ~/Library/Application Support/NOGAR/debug.p12 |
| Linux | $XDG_CONFIG_HOME/NOGAR/debug.p12, or ~/.config/NOGAR/debug.p12 if XDG_CONFIG_HOME is not defined |
| Windows | %APPDATA%\NOGAR\debug.p12 |
The first time, ngr warns that it has created it:
WARN: created a new debug key in /Users/you/Library/Application Support/NOGAR/debug.p12; keep it to sign updates of the same apps
Keep that file. If you lose it, the apps you signed with it can no longer be updated without uninstalling them. nogar keeps no copy of it.
The debug key is for testing. Do not publish an app signed with it: use your own keystore.
Which schemes are signed
By default, all four: v1 (JAR signature), v2 and v3 inside the apk, and v4 in the .idsig file. Each one is
removed with its own option:
| Option | When it makes sense |
|---|---|
--no-v1 |
The apk is only installed on Android 7.0 or later, which already understands v2 |
--no-v4 |
You do not need the .idsig. Only incremental installation uses it |
--no-v2, --no-v3 |
Test cases. Without a reason, leave them |
--min-sdk-version says the lowest API level to sign for. By default it is the manifest's.
Aligning
ngr sign aligns the apk when it signs it, so on the normal path there is nothing to do.
ngr align exists to check an apk (--check) and to align a zip without signing it.
If you use both, the order is align and then sign: aligning removes the signing block.
Verifying an apk
ngr verify app_signed.apk
With no options, verify only prints the warnings. The answer is the exit code: 0 if it verifies, 3 if it does not.
With -v it gives the verdict for each scheme:
ngr verify -v app_signed.apk
Verifies
Verified using v1 scheme (JAR signing): true
Verified using v2 scheme (APK Signature Scheme v2): true
Verified using v3 scheme (APK Signature Scheme v3): true
Verified using v3.1 scheme (APK Signature Scheme v3.1): false
Verified using v3.2 scheme (APK Signature Scheme v3.2): false
Verified using v4 scheme (APK Signature Scheme v4): false
Verified for SourceStamp: false
Number of signers: 1
A false on a line says the apk does not carry that signature, not that it is wrong. If a signature it does carry
does not verify, the apk does not verify:
DOES NOT VERIFY
ERROR: APK Signature Scheme v3 signer #1: APK integrity check failed. CHUNKED_SHA256 digest mismatch. ...
v4 comes out false unless you give it the .idsig:
ngr verify -v --v4-signature-file app_signed.apk.idsig app_signed.apk
Finding out who signed an apk
ngr verify --print-certs app.apk
V3.0 Signer: certificate DN: CN=Example, O=Example, C=ES
V3.0 Signer: certificate SHA-256 digest: 5647da8c1450d839...
V3.0 Signer: certificate SHA-1 digest: 664c1fd744da684e...
...
Two apks are signed by the same key if their certificate SHA-256 digest matches. That is what Android compares
when updating.
Common warnings
verify can verify an apk and warn at the same time. The most common:
WARNING: META-INF/services/... not protected by signature. Unauthorized modifications to this JAR entry will not be detected. Delete or move the entry outside of META-INF/.
It says that an entry in META-INF/ is not covered by the v1 signature. It does not prevent installing the apk.
See also
ngr signandngr verify.- “Rotating the signing key”.
- “Using ngr in scripts”, to sign without typing the password in the command.