ngr assemble
Assembles smali into a dex. It accepts a .smali file or a folder that is searched for .smali files. With
--dialect smali, the same input and the same options always give the same bytes. It is the reverse step of
ngr disassemble.
Usage
ngr assemble [options] <smali>
Inputs
| Input | What it is |
|---|---|
<smali> |
A .smali file, or a folder in which .smali files are searched for in all its subfolders |
The input can be the smali that ngr disassemble and ngr decode write.
Options
| Option | What it does | Default |
|---|---|---|
-o, --output <file> |
The dex to write | <input>.dex next to the input |
-f, --force |
Overwrites the output if it already exists | |
--android-api <level> |
API level: decides the dex version and the opcodes the smali can use | 15 |
--dialect <smali|jf> |
Which smali dialect it accepts and how it lays out the dex: smali, or jf, which reads a folder in file system order, shares the debug information and orders the annotations differently |
smali |
-h, --help |
Shows the help |
The dex version depends on the API level:
| API level | Dex version |
|---|---|
| Up to 23 | 035 |
| 24 and 25 | 037 |
| 26 and 27 | 038 |
| 28 and 29 | 039 |
| 30 to 34 | 040 (only with smali) |
| 35 | 041 (only with smali) |
With --dialect smali there is nothing above level 35. With jf, every level above 27 gives 039.
invoke-polymorphic and invoke-custom need level 26, and const-method-handle and const-method-type need 28.
What it writes
A new dex. With a folder, <folder>.dex next to it; with a file, the same name with the .dex extension. The final
line goes to stderr:
wrote classes_smali.dex
Examples
Assemble the folder that disassemble wrote:
ngr assemble classes_smali
Assemble a single .smali file, with another output name:
ngr assemble -o counter.dex Counter.smali
Assemble for a newer dex. Level 26 gives a dex 038:
ngr assemble --android-api 26 -o classes.dex classes_smali
ngr identify classes.dex
file: classes.dex
structure: dex (v038, 21,552 B)
verbs: disassemble, dump, list, decompile
Smali with invoke-polymorphic only assembles from level 26:
ngr assemble poly.smali
ngr assemble: unable to assemble 'poly.smali': 'invoke-polymorphic' does not exist at api 15: it needs --android-api 26 or higher (in Lx/Y;->foo).
Assemble with the jf dialect:
ngr assemble --dialect jf classes_smali
Exit codes
| Code | When |
|---|---|
| 0 | The dex was written |
| 1 | The input does not exist, has no .smali, the smali cannot be assembled, or the output already exists and you did not pass -f |
| 2 | The options are wrong: <smali> is missing, --android-api is less than 1 or no dex matches that level with the chosen dialect, or --dialect is neither smali nor jf |
Things to know
- The dex is not identical to the original. If you assemble what you disassembled, the dex that comes out has another version and another layout of the debug information, and its bytes do not match those of the original dex. With the dex of a test apk, the original was a 21,628-byte dex 035 and the assembled one was a 21,556-byte dex 035.
- The default API level is 15, which gives a dex 035. For the newer opcodes you have to raise it.
- An error in the smali ends with 1 and gives the file, the line and the reason:
ngr assemble: unable to assemble 'bad2.smali': .../bad2.smali: line 6: 'bogus-op' is not a known opcode.
The line number points to the line after the one that fails: in this example, the wrong opcode is on line 5.
Some errors, such as jumping to a label that does not exist, come out with a message that names neither the file nor the line, also with code 1:
ngr assemble: The given key ':nolabel' was not present in the dictionary.
jfreads the folder in the order the file system lists it, so its output can depend on the volume. Withsmali, it does not.- A level above 35 with
smalidoes not exist:no dex version maps to api level 36; smali stops at 35 (--dialect jf does not), with code 2. invoke-customandconst-method-handledo not assemble. Smali that uses them fails with code 1 and the messageCallSite operands (invoke-custom / const-method-handle) are not implemented.
See also
ngr disassemble, which writes the smali assembled here.ngr build, which builds an apk from the tree thatdecodewrote.ngr dump, to look inside the dex that comes out.- “Modifying and rebuilding an apk”.