Using ngr in scripts
ngr is designed so that a script can use it without surprises: it asks nothing, it does not write where you do
not tell it to, and it answers with an exit code.
What to rely on
The 0.x.y versions are beta versions. We will try not to, but from one to the next the commands, the options, the
exit codes and the output formats may change. Pin the version of ngr your script uses and read the
“Release notes” before changing it.
Also, in a script:
- decide with the exit code, not with the stderr text;
- give the output names yourself with
-o, instead of trusting the default name; - give the options you care about yourself, even if they match today's default.
Exit codes
| Code | Meaning | What a script usually does |
|---|---|---|
| 0 | Everything went well | Carry on |
| 1 | Failure: an input cannot be read or an output cannot be written | Stop with an error |
| 2 | Usage error: the command is badly written | Stop: it is a bug in the script |
| 3 | Negative verdict from identify, align, verify or decompile |
Depends on the question |
The 3 separates “ngr could not do it” from “it did it and the answer is no”. This is how you check a signature:
ngr verify app.apk
case $? in
0) echo "signature ok" ;;
3) echo "the signature does not verify"; exit 1 ;;
*) echo "could not check"; exit 2 ;;
esac
And this is how you require that everything is aligned and signed before publishing:
set -e
ngr align --check app.apk
ngr verify app.apk
With set -e, any code other than 0 stops the script, including 3.
stdout and stderr
What you asked to see goes to stdout. The WARN: warnings, the errors and the wrote line go to stderr.
Redirecting stdout gives you only the data:
ngr list classes app.apk > classes.txt
ngr info --format json app.apk > info.json
A pipe that closes early is not an error:
ngr list strings app.apk | head -3
To keep the warnings of a run:
ngr decompile -o out app.apk 2> decompile.log
Output for programs
| Command | Structured output |
|---|---|
info, resource, xml |
--format json or --format xml, to stdout |
decompile |
--report <file>, a JSON with the category of each method |
list |
Text, one item per line |
For example, the package name of an apk with jq:
ngr info --format json --package app.apk | jq -r '.[0].package'
And the methods that did not come out as natural Java, from the decompiler report:
ngr decompile --report report.json -o out app.apk
jq -r '.classes[] | .class as $c | .methods[] | select(.category != "natural") | "\($c) \(.method) \(.category)"' report.json
The schema of these JSON files can change between 0.x versions. Pin the version of ngr your script uses.
Passwords
Never write a password with pass: in a script that is saved or in a command that ends up in a log. Use an
environment variable or a file:
export KS_PASS="..." # or your continuous integration system defines it as a secret
ngr sign --ks release.p12 --ks-pass env:KS_PASS -o app-release.apk app.apk
ngr sign --ks release.p12 --ks-pass file:/run/secrets/ks_pass -o app-release.apk app.apk
On a continuous integration server do not use --debug-key: it would create a new key on each machine, and apks
signed on one would not update those of another.
Overwriting
A script that runs twice finds the outputs of the previous run, and ngr refuses to overwrite them. Pass -f
where you want to overwrite, knowing what it does in each command: in decode it deletes the whole folder, and in
disassemble and decompile it writes over without emptying. For a clean output from these two, delete the folder
yourself first.
Commands that write a file do not create the destination folder: if you give -o build/app.apk, the build folder
has to exist.
Pinning the version
ngr --version
ngr 0.1.0
Each package unpacks into a folder with its version, so a script can call nogar-0.1.0-linux-x64/ngr by its path
and not depend on the PATH.
On Windows
When its output goes to a file or a pipe, ngr.exe writes UTF-8. In PowerShell, before redirecting it:
[Console]::OutputEncoding = [Text.Encoding]::UTF8
A complete example
Rebuild, sign and check an apk, stopping at the first failure:
#!/usr/bin/env bash
set -euo pipefail
mkdir -p build
ngr build -f -o build/app.apk app
ngr sign -f --ks release.p12 --ks-pass env:KS_PASS -o build/app-release.apk build/app.apk
ngr verify build/app-release.apk
ngr align --check build/app-release.apk
echo "done: build/app-release.apk"