Output and exit codes
ngr splits what it says between two channels and ends with one of four codes. This page explains which is which,
what each code means in each command, and what you can take as fixed when you write a script.
What goes to stdout and what to stderr
- stdout carries what you asked to see: the report of
info, the list oflist, the dump ofdump, the verdict ofidentify, the table ofverify -v. It is what you redirect to a file or to another program. - stderr carries what
ngrhas to tell you about the run: the warnings, the errors and the final line of the commands that write.
Inside stderr there are three kinds of line:
| Kind | Form | Example |
|---|---|---|
| Warning | WARN: ... |
WARN: 1 entry name(s) collided case-insensitively and overwrote each other on this filesystem |
| Error | ngr <command>: ... |
ngr decode: 'nope.apk' does not exist. |
| End | wrote ... |
wrote app_built.apk |
A warning does not change the exit code. An error does: the command ends with 1 or 2. The wrote line appears only
if the command wrote something, and it is the last one.
“Files each command writes” has the one for each command.
Two exceptions worth knowing:
verifyprints its warnings to stdout, in the formWARNING: ...(notWARN:). AWARNING:does not change the code: an apk with signature warnings exits with 0. If the apk does not verify, the verdict and theERROR:lines go to stderr.decompileprints a summary to stderr beforewrote, in the formngr decompile: 25 classes, 49 methods: 46 natural, 1 dispatcher, 2 smali; 25 files written. It is not an error even though it starts with the command name.
The four codes
| Code | Meaning |
|---|---|
| 0 | Everything went well |
| 1 | Failure: an input cannot be read or used, or an output cannot be written |
| 2 | Usage error: the options or the inputs on the command line are wrong |
| 3 | Negative verdict. Only identify, align, verify and decompile give it |
The 3 is not a failure of ngr: it is the answer to a question. The command did its job and the result is “no”.
In the other commands, a negative result does not exist: they either work (0) or fail (1 or 2).
What each code means in each command
| Command | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
identify |
It is one of the five structures | The file does not exist or cannot be read | Wrong options | The file was read and is none of the five structures |
info |
Report printed. Also with a protected apk, which gives WARN: PROTECTED instead of the report |
The apk does not exist or cannot be read | Wrong options | It does not give it |
resource |
Report printed. Also if the resource does not exist: it gives a WARN: and prints nothing |
The apk does not exist or cannot be read | Wrong options | It does not give it |
xml |
Report printed | The apk does not exist, or the entry is not in the apk | Wrong options | It does not give it |
decode |
The tree was written. Also with a protected apk, which is not decoded | The apk does not exist or cannot be read, or the folder already exists without -f |
Wrong options | It does not give it |
build |
The apk was written | The folder does not exist, is not a decode tree, the smali does not assemble, or the apk already exists without -f |
Wrong options | It does not give it |
merge |
The apk was written | The file does not exist or is not a bundle, or the apk already exists without -f |
Wrong options | It does not give it |
refactor |
The apk was written. Also with a protected apk: it prints its path and PROTECTED and writes nothing |
The apk does not exist or cannot be read, or the output already exists without -f |
Wrong options | It does not give it |
protect |
The apk was written. Also with a protected apk: it prints its path and PROTECTED and writes nothing |
The apk does not exist or cannot be read, or the output already exists without -f |
Wrong options | It does not give it |
align |
Written and aligned; with --check, everything is aligned |
The file does not exist or cannot be read, or the output already exists without -f |
Wrong options | An uncompressed entry is not aligned: with --check, in the input; without it, in the file just written, which is kept |
sign |
The apk was signed | The apk or the keystore cannot be read, the password is not valid, or the output (or its .idsig) already exists without -f |
Wrong options: no signer, --debug-key with --ks, a --ks-pass is missing |
It does not give it |
verify |
The apk verifies | The apk does not exist or cannot be read | Wrong options | The apk does not verify |
rotate |
The lineage was written | A keystore does not exist or cannot be read, the password is not valid, or the output already exists without -f |
Wrong options: -o or --old-ks is missing |
It does not give it |
lineage |
The lineage was read, and written if there were changes and -o |
The file is neither a lineage nor an apk, or there are changes without -o, or the output already exists without -f |
Wrong options: a --set and an --unset of the same capability |
It does not give it |
disassemble |
The smali was written | The file does not exist or cannot be read, or the folder already exists without -f |
Wrong options | It does not give it |
assemble |
The dex was written | The smali does not exist, does not assemble, or the dex already exists without -f |
Wrong options | It does not give it |
dump |
Dump printed | The file does not exist, cannot be read, or has a section the dump cannot write | Wrong options | It does not give it |
list |
List printed | The file does not exist or cannot be read | Wrong options, including a <kind> that does not exist |
It does not give it |
decompile |
The Java was written and all methods came out as Java | The input does not exist or cannot be read, or a file or the --report could not be written |
Wrong options | Some method, field or region came out as smali |
The 3 of the four commands that give it, as the help of each one explains it:
| Command | What the help says |
|---|---|
identify |
The 3 means the file was read and is none of the five structures in the list |
align |
The 3 means an uncompressed entry is not aligned, whether it was checked with --check or found in the file just written (which is kept) |
verify |
The 3 means the apk does not verify: the ERROR lines go to stderr and the verdict table is not printed |
decompile |
The 3 means some method, field or region was written as smali, because Java cannot express it, --unstructured smali asked for it, or its translation is not supported yet or failed |
The four cases, run:
ngr identify notes.txt; echo "exit $?"
file: notes.txt
structure: unknown (11 B)
verbs: none - this is none of the five structures nogar knows.
beware: some verbs accept it anyway: decode, align, align --check, rotate, lineage
exit 3
ngr align --check -v order-reversed.zip; echo "exit $?"
Verifying alignment of order-reversed.zip (4)...
310 f.bin (BAD - 2)
255 e.bin (BAD - 3)
200 d.bin (OK)
...
exit 3
ngr verify app.apk; echo "exit $?"
DOES NOT VERIFY
ERROR: Missing META-INF/MANIFEST.MF
exit 3
ngr decompile classes.dex; echo "exit $?"
...
WARN: 2 method(s) were written as smali; see the NOGAR ERROR comments
ngr decompile: 2 classes, 4 methods: 2 natural, 0 dispatcher, 2 smali; 2 files written
wrote 2 file(s) to classes_java
exit 3
In a script, a 3 from decompile usually means “there is work to look at”, not “it failed”: the files are
written. Treat it differently from a 1.
Error examples
A code 1 is a failure with the input or the output. The message says what failed:
ngr decode: 'nope.apk' does not exist.
ngr info: unable to read 'notes.txt': Central Directory corrupt.
ngr xml: unable to read 'app.apk': Entry 'nope.xml' not found in the APK.
ngr build: 'notes.txt' is a file, not the directory 'ngr decode' writes.
ngr build: 'app_built.apk' already exists (pass -f to overwrite).
ngr lineage: the lineage changed but no -o was given; nothing was written.
ngr sign: unable to load signer #1: cannot read the keystore 'old.p12': The certificate data cannot be read with the provided password, the password may be incorrect.
A code 2 is a usage error. It always ends with a reference to the help of the command:
ngr decode: missing <apk>. See 'ngr help decode'.
ngr decode: unexpected argument 'b.apk'. See 'ngr help decode'.
ngr decode: unrecognized option '--nope'. See 'ngr help decode'.
ngr list: 'nope' is not a valid kind; use one of: classes, strings, fields, methods, types, dex. See 'ngr help list'.
ngr info: 'yaml' is not a valid value for --format; use one of: text, json, xml. See 'ngr help info'.
ngr sign: no signer given; pass --ks <keystore>, or --debug-key to sign with nogar's debug key. See 'ngr help sign'.
No arguments, and commands that do not exist
| Command line | What it does | Code |
|---|---|---|
ngr |
Prints the general help to stdout | 0 |
ngr help |
The same | 0 |
ngr help decode |
Prints the help of decode |
0 |
ngr --version |
Prints ngr 0.1.0 |
0 |
ngr nope |
Error to stderr: ngr: unknown verb 'nope'. See 'ngr help'. |
2 |
ngr help nope |
The same error | 2 |
ngr --nope |
ngr: unrecognized option '--nope'. See 'ngr help'. |
2 |
Closing a pipe
If you read only the start of a long output, the program on the right closes the pipe before ngr finishes.
ngr tolerates it: it prints no error and ends with 0.
ngr list strings app.apk | head -3
"$SwitchMap$com$example$app$Mode"
"$VALUES"
"$default$greetAll"
In bash, echo "${PIPESTATUS[*]}" right after gives 0 0, and there is nothing on stderr. With set -o pipefail
the whole pipe also ends with 0.
Windows: UTF-8 in files and pipes
When its output goes to a file or a pipe, ngr.exe writes UTF-8. In PowerShell, run
[Console]::OutputEncoding = [Text.Encoding]::UTF8 first, or non-ASCII characters will look wrong in output that
goes through a pipe. “Installation” explains it in
more detail.
The 0.x versions are beta
We will try not to, but from one 0.x to the next the commands, the options, the exit codes and the output formats may change. “Release notes” says what changes in each version.
If you write a script, pin the version of ngr it uses. Rely on the exit code and the file names, and do not
parse the text of stderr (“Using ngr in scripts”).