ngr disassemble
Disassembles a dex into a tree of .smali files, one per class. From an apk or an xapk it reads all the dex files
and writes one folder for each. You use it when you only want an app's code as smali, without decoding the
resources. ngr assemble does the reverse.
Usage
ngr disassemble [options] <file>
Inputs
| Input | What it is |
|---|---|
<file> |
A dex, an apk or an xapk, or a dex inside an apk (app.apk/classes2.dex) |
Options
| Option | What it does | Default |
|---|---|---|
-o, --output <dir> |
The folder to write | <name>_smali/ next to the input |
-f, --force |
Writes into the folder even if it exists. Unlike decode, it does not empty it first |
|
-j, --jobs <n> |
Number of threads. The output does not depend on it | One per core |
--use-locals |
Writes .locals (the registers that are not parameters) instead of .registers |
|
--no-debug-info |
Leaves out the debug information (.line, .local, .param...) |
|
--no-accessor-comments |
Leaves out the comments on calls to synthetic accessors | |
--no-parameter-registers |
Names every register vNN, without the pNN form for parameters |
|
--code-offsets |
Puts a comment with its position in the code before each instruction | |
--implicit-references |
Names the members of the class itself without the class name | |
--sequential-labels |
Numbers the labels of each kind in order, instead of by address | |
--classes <classes> |
Disassembles only these classes: type descriptors separated by commas (La/b;,Lc/d;). Can be repeated |
All |
--resolve-resources <prefix=public.xml> |
Comments each resource number with its name, read from a public.xml. prefix is the name of the R class |
|
-h, --help |
Shows the help |
What it writes
A folder with one .smali per class, in subfolders that follow the package name. With a bare dex:
classes_smali/
com/example/app/Counter.smali
com/example/app/MainActivity.smali
...
With an apk or an xapk there is one folder per dex, classes/, classes2/..., each with its packages:
app_smali/
classes/com/example/app/Counter.smali
classes2/com/example/extra/Helper.smali
With a dex inside an apk (app.apk/classes2.dex), the packages hang directly from the output folder.
The final line goes to stderr:
wrote 43 file(s) to app_smali
Examples
Disassemble a dex:
ngr disassemble classes.dex
Disassemble all the dex files of an apk, into another folder:
ngr disassemble -o code app.apk
Disassemble only the second dex of an apk:
ngr disassemble app.apk/classes2.dex
Disassemble only two classes, and do it again over the same folder:
ngr disassemble -f --classes 'Lcom/example/app/Counter;,Lcom/example/app/Mode;' classes.dex
Smali that is shorter to read, without the debug information and with the labels numbered in order:
ngr disassemble --no-debug-info --sequential-labels classes.dex
Comment each resource number with its name. ngr decode writes the public.xml in
resources/package_1/res/values/:
ngr disassemble --resolve-resources com.example.app=app/resources/package_1/res/values/public.xml classes.dex
How each option changes the smali
All the snippets come from the same class. With no options, a method looks like this:
.method public static fib(I)J
.registers 4
.line 14
sget-object v0, Lcom/example/app/Counter;->TABLE:[J
array-length v1, v0
if-ge p0, v1, :cond_8
--use-localschanges the registers directive:.registers 4becomes.locals 3, becausefibhas one parameter.--no-debug-inforemoves the.line 14lines and the other debug directives.--no-parameter-registersnames the parameter as a normal register:if-ge p0, v1, :cond_8becomesif-ge v3, v1, :cond_8.--code-offsetsputs a comment with its position, in code units, before each instruction:
#@0
.line 3
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
#@3
return-void
--implicit-referencesremoves the class name from the members of the class itself:sget-object v0, Lcom/example/app/Counter;->TABLE:[Jbecomessget-object v0, TABLE:[J.--sequential-labelsnumbers the labels of each kind from 0 instead of by address::cond_8becomes:cond_0, and:try_start_3,:try_end_9and:catchall_bbecome:try_start_0,:try_end_0and:catchall_0.--no-accessor-commentsremoves the comment thatngrputs before the call to a synthetic accessor, for example:
# getter for: Lcom/example/app/Registry;->entries:Ljava/util/Map;
invoke-static {v0}, Lcom/example/app/Registry;->access$000(Lcom/example/app/Registry;)Ljava/util/Map;
--resolve-resourcesadds its name at the end of each resource number. With the prefixcom.example.app:
const p1, 0x7f120009 # com.example.app.string.greeting
const/high16 p1, 0x7f0d0000 # com.example.app.layout.activity_main
The prefix is copied as is into the comment: with R you get # R.string.greeting.
Exit codes
| Code | When |
|---|---|
| 0 | The smali was written. Also if --classes names no class that exists |
| 1 | The input or the public.xml does not exist or cannot be read, or the folder already exists and you did not pass -f |
| 2 | The options are wrong: <file> is missing, --jobs is less than 1, or --resolve-resources is not of the form <prefix>=<file> |
Things to know
-fdoes not empty the folder. It writes over the files that match and leaves the others, including any you put there.ngr decode -fdoes empty it.- A class name that does not exist is not an error. With
--classes 'Lnope/X;'the command ends with 0 without writing anything and without saying anything: no folder and nowroteline. - Dex files with hidden api restrictions lose those marks.
ngrwarns on stderr, and the smali does not include them:
WARN: 'hiddenapi.dex' carries hidden api restrictions (whitelist, greylist, blacklist...) that nogar does not read; the smali leaves them out
--jobsdoes not change the result, only how long it takes.- The smali it writes is what
ngr assemblereads. The dex that comes out of assembling it is not identical to the original.
See also
ngr assemble, the reverse step.ngr decode, which also decodes the resources and the manifest.ngr list, to see which classes a dex has before disassembling it.- “Modifying and rebuilding an apk”.