ngr sign
Signs an apk with one or more keys. It writes a new apk with the v1, v2 and v3 signatures, and next to it the
.idsig file with the v4 signature. The input apk is not touched.
Usage
ngr sign [options] <apk>
Inputs
| Input | What it is |
|---|---|
<apk> |
The apk to sign |
Options
Who signs:
| Option | What it does | Default |
|---|---|---|
--debug-key |
Signs with the nogar debug key. It is created the first time. Cannot be combined with --ks |
|
--ks <file> |
PKCS#12 keystore of a signer. Each --ks opens a new signer. Can be repeated |
|
--ks-pass <spec> |
Keystore password: pass:<password>, env:<variable>, file:<path> or stdin. Applies to the preceding --ks |
|
--ks-key-alias <alias> |
Key to sign with. Applies to the preceding --ks |
The only key in the keystore |
--v1-signer-name <name> |
Base name of the v1 signature files. Applies to the preceding --ks |
The key alias |
Which schemes:
| Option | What it does | Default |
|---|---|---|
--no-v1 |
Does not sign with v1 (JAR signature) | |
--no-v2 |
Does not sign with APK Signature Scheme v2 | |
--no-v3 |
Does not sign with APK Signature Scheme v3 | |
--no-v4 |
Does not write the v4 signature (.idsig) |
|
--v4 |
Asks for v4 explicitly: if it cannot be written, it is an error | |
--verity |
Adds the verity digest and signature to the v2 and v3 blocks | |
--min-sdk-version <n> |
Lowest API level to sign for. Below 18, v1 uses SHA-1 | The manifest's |
Alignment:
| Option | What it does | Default |
|---|---|---|
--alignment-preserved |
Leaves uncompressed entries where they are, instead of realigning them | |
--page-size <4|16|64> |
Aligns uncompressed .so entries to pages of that many KB |
16 |
Key rotation:
| Option | What it does | Default |
|---|---|---|
--lineage <file> |
Certificate lineage for the rotation: a lineage file or a rotated apk | |
--rotation-min-sdk-version <n> |
Lowest API level of the rotated key | 33 |
--rotation-targets-dev-release |
Lets the rotated key also cover the development release of Android |
Output:
| Option | What it does | Default |
|---|---|---|
-o, --output <file> |
The apk to write | <name>_signed<ext> next to the input |
-f, --force |
Overwrites the output and its .idsig if they already exist |
|
-v, --verbose |
Also prints what was signed, with which schemes, and the v4 file | |
-h, --help |
Shows the help |
A signer is required: --debug-key or at least one --ks.
What it writes
- The signed apk,
<name>_signed.apkunless you say otherwise. - The v4 signature in
<output>.idsig, next to the signed apk.
The final line goes to stderr:
wrote app_signed.apk and app_signed.apk.idsig
With -v, to stdout:
Signed app_signed.apk
schemes: v1 v2 v3 v4
wrote app_signed.apk.idsig
Examples
Sign for testing, with the debug key:
ngr sign --debug-key app_built.apk
Sign with your keystore, with the password in an environment variable:
ngr sign --ks release.p12 --ks-pass env:KS_PASS -o app-release.apk app_built.apk
Sign without v1, for an apk that is only installed on Android 7.0 or later:
ngr sign --ks release.p12 --ks-pass env:KS_PASS --no-v1 app_built.apk
Several signers
Each --ks opens a signer, and the --ks-pass, --ks-key-alias and --v1-signer-name options belong to the
--ks before them. One of those options before its --ks is a usage error.
Two signers that rotate a key:
ngr sign --ks old.p12 --ks-pass env:OLD_PASS --ks new.p12 --ks-pass env:NEW_PASS \
--lineage lineage.bin app.apk
The lineage decides which signer is the old one, not the order of the --ks options. Several --ks options need
--lineage. “Rotating the signing key” covers the whole process.
Exit codes
| Code | When |
|---|---|
| 0 | The apk was signed |
| 1 | The apk or the keystore cannot be read, the password is wrong, or the output already exists and you did not pass -f |
| 2 | The options are wrong: there is no signer, --debug-key is used with --ks, or the --ks-pass of a --ks is missing |
Things to know
- Keystores are PKCS#12 (
.p12,.pfx). A JKS keystore has to be converted first, for example with the JDK'skeytool -importkeystore. signaligns. Unless you pass--alignment-preserved, uncompressed entries are realigned when signing, and.sofiles to 16 KB pages. You do not need to runngr alignfirst, though doing so does no harm.- Signing comes last. Any later change to the apk, including aligning it, invalidates the signature.
- The debug key is for testing. It is stored in your user folder and nogar keeps no copy of it (“The debug key”).
- v4 is not inside the apk. It is the
.idsigfile, andngr verifyonly checks it if you give it to it with--v4-signature-file.
See also
ngr verify, to check what you have signed.ngr rotateandngr lineage, for key rotation.- “Signing and verifying”.