ngr align
Aligns the uncompressed entries of a zip or an apk by padding the extra field of their local header, and checks
what it has written. Compressed entries and folders stay where they are. With --check it writes nothing: it only
checks. You use it before signing, or to find out whether an apk is already aligned.
Usage
ngr align [options] <zip>
Inputs
| Input | What it is |
|---|---|
<zip> |
A zip or apk file |
Options
| Option | What it does | Default |
|---|---|---|
-o, --output <file> |
The file to write | <name>_aligned<ext> next to the input |
-f, --force |
Overwrites the output if it already exists | |
--alignment <bytes> |
Alignment of the uncompressed entries, in bytes | 4 |
--page-size <4|16|64> |
Aligns uncompressed .so entries to pages of that many KB |
|
--check |
Only checks the alignment of the input. Writes nothing | |
-v, --verbose |
Lists every entry of the checked file: the output, or the input with --check |
|
-h, --help |
Shows the help |
What it writes
Without --check, a new file, <name>_aligned<ext> unless you say otherwise. The final line goes to stderr:
wrote app_aligned.apk
With --check no file is written and, if everything is aligned, nothing is printed.
With -v, stdout gets one line per entry of the checked file, with its position in the zip and the verdict, and a
last line with the result. The header says which alignment was checked:
Verifying alignment of app.apk (4)...
49 AndroidManifest.xml (OK - compressed)
5960 res/drawable-hdpi-v4/photo.png (OK)
11136 resources.arsc (OK)
65536 lib/arm64-v8a/libnogar.so (OK)
Verification successful
The verdicts are OK, OK - compressed (a compressed entry, which is not checked), OK - directory and
BAD - <n>, where <n> is the remainder of dividing the position by the alignment. If any entry is BAD, the
last line is Verification FAILED.
Examples
Align an apk:
ngr align app.apk
Check whether an apk is aligned, without writing anything. The answer is the exit code:
ngr align --check app.apk
echo $?
See which entries of a zip are not aligned:
ngr align --check -v data.zip
Verifying alignment of data.zip (4)...
63 a.txt (BAD - 3)
132 b.bin (OK)
3195 c.txt (BAD - 3)
Verification FAILED
Align the native libraries to 16 KB pages, for Android 15 and later:
ngr align --page-size 16 -o app-16k.apk app.apk
Align before signing, with the two commands one after the other:
ngr align -o app_aligned.apk app_built.apk
ngr sign --ks release.p12 --ks-pass env:KS_PASS app_aligned.apk
Exit codes
| Code | When |
|---|---|
| 0 | The file was written and is aligned, or with --check it is |
| 1 | The input does not exist or is not a readable zip, or the output already exists and you did not pass -f |
| 2 | The options are wrong: <zip> is missing, --alignment is less than 1, --page-size is not 4, 16 or 64, or --check is used with -o |
| 3 | An uncompressed entry is not aligned: with --check, in the input; without it, in the file just written, which is kept |
Things to know
- Aligning removes the signing block. An apk that is signed and then aligned no longer verifies. Align first and
sign after. With an apk signed with v1, v2 and v3, the result of aligning it and running
ngr verifyis:
DOES NOT VERIFY
ERROR: JAR signer OLD.RSA: JAR signature META-INF/OLD.SF indicates the APK is signed using APK Signature Scheme v2 but no such signature was found. Signature stripped?
ngr sign already aligns when it signs, so align is only needed if you want the apk aligned and
unsigned.
- It accepts any zip, not just apks. The alignment is that of the uncompressed entries, whatever the content.
--alignmentaccepts any number from 1 up, not just powers of two.--page-sizeapplies only to uncompressed.soentries, and--checkhonors it too: an apk aligned to 16 KB fails the check with--page-size 64.--checkcannot be combined with-o:ngr align: --check writes nothing; drop -o., with code 2.- Without
-v,--checkprints nothing, not even when it fails. Look at the exit code.
See also
ngr sign, which aligns and signs in a single step.ngr verify, to check the signature.- “Signing and verifying”.