The decode tree
ngr decode turns an apk into a folder. ngr build turns it back
into an apk. What you change between the two commands is your edit. This page says what is in the folder, which
parts you can touch and what build does with each one.
The examples use this command and its variants:
ngr decode app.apk
Overview
app/
AndroidManifest.xml
resources/
package_1/
package.json
res/
values/strings.xml, public.xml, ...
layout/, drawable/, raw/, ...
smali/
classes/
classes2/
root/
assets/, lib/, META-INF/, ...
signatures/
.cache/
archive-info.json
path-map.json
uncompressed-files.json
| Part | What it contains | Edit it? | What build does with it |
|---|---|---|---|
AndroidManifest.xml |
The manifest, as text xml | Yes | Compiles it to binary xml |
resources/package_N/res/values*/*.xml |
The values of the resource table | Yes | Rebuilds the resource table |
resources/package_N/res/values/public.xml |
The resource identifiers | Yes, to add resources | Rebuilds the resource table |
resources/package_N/res/ (the rest) |
The decoded xml files and the binary files of the resources | Yes | Compiles them and puts them in the apk |
resources/package_N/package.json |
Data of the resource package | Not needed | Rebuilds the resource table |
smali/classes*/ |
One .smali per class |
Yes | Reassembles the dex files that changed |
dex/ |
The dex files as they are (only with --dex) |
No: they are binary | Copies them to the apk |
root/ |
The other entries of the apk | Yes: change, add and remove | Copies them to the apk |
signatures/ |
The signing blocks of the original apk | No | Puts them back. They are no longer valid |
.cache/ |
The dex files that came out of smali/ |
No | Reuses or rewrites them |
archive-info.json |
The date and header data of the zip entries | Rarely | Uses them when writing the zip |
path-map.json |
The entry names that are not valid as file names | Only if you rename one | Uses it to go back to the original name |
uncompressed-files.json |
Which entries are stored uncompressed | Rarely | Stores them uncompressed |
AndroidManifest.xml
It is the manifest of the apk as text xml. You edit it like any xml: the strings, the permissions, the attributes.
Values that are references to resources are written as @string/app_name.
When you run build, the manifest is compiled to binary xml again. You can check it with
ngr xml:
ngr xml app_built.apk AndroidManifest.xml | grep versionName
A: android:versionName(0x0101021c)="9.9.9" (Raw: "9.9.9")
That came from changing the value of android:versionName to 9.9.9 in AndroidManifest.xml.
resources/package_N/
The resource table of the apk, one folder per package (package_1, and so on). It has two things.
package.json stores what does not fit in the xml files: the identifier and the name of the package
(package_id, package_name) and the flags of each resource (specs). None of the edits on this page touches it.
res/ contains the resources with the paths they have inside the table:
-
values*/*.xml: the values (strings.xml,colors.xml,dimens.xml,styles.xml...), one folder per configuration (values-es/,values-night/,values-b+sr+Latn/...). Each file holds only the values of its type. You edit them as text andbuildrebuilds the table from them. -
values/public.xml: the list of resources with their identifier, their type and their name. It decides which resources exist:<public id="0x7f120017" type="string" name="title_main" /> -
The decoded xml files (
layout/,menu/,anim/,xml/, the xmldrawablefiles...): the binary xml of the apk, as text. You edit them andbuildcompiles them again. -
The files that are not xml (png,
raw/...): they are copied untouched. The png that comes out of the tree is byte-for-byte identical to the one that was in the apk, andbuildputs it back as it is.
The folder names follow the table, not the zip. A resource that is in res/drawable-hdpi-v4/ in the zip appears in
the tree as res/drawable-hdpi/, and path-map.json stores the correspondence
(“below”).
You check it with ngr info, ngr resource and
ngr xml. Changing the app_name string in res/values/strings.xml:
<string name="app_name">Example App</string>
ngr resource app_built.apk @string/app_name
resource="Example App"
And ngr info app_built.apk shows AppName="Example App". A decoded xml is edited the same way: changing an
android:layout_height="wrap_content" in res/layout/row.xml to 48dip makes ngr xml app_built.apk res/layout/row.xml show a dimension there instead of wrap_content.
Adding a resource
An entry that is in a values*/*.xml but not in public.xml does not exist for build. A new value needs both
things: its line in the values xml and its line in public.xml, with an identifier that is not in use:
<string name="extra_new">Brand new</string>
<public id="0x7f12001a" type="string" name="extra_new" />
With both, build ends with 0 and the resource exists:
ngr resource app_built.apk @string/extra_new
resource="Brand new"
Without the public.xml line, build ends with 1 and does not write the apk:
ngr build: unable to build 'app': Undefined entry name: <string name="extra_new"> in app/resources/package_1/res/values/strings.xml.
The same happens with a new file in res/. With res/raw/extra.txt and without its line in public.xml:
ngr build: unable to build 'app': Local resource not defined: @raw/extra, for path:res/raw/extra.txt.
With <public id="0x7f110002" type="raw" name="extra" /> added, the apk carries res/raw/extra.txt and
ngr resource app_built.apk -v @raw/extra finds it.
smali/
The code, disassembled: one folder for each dex of the apk (classes/ for classes.dex, classes2/ for
classes2.dex, and so on), with a package tree and one .smali per class. Nested classes go in their own file
(Outer$Inner.smali). You edit them as text. To check a change, ngr list strings reads the built apk:
const-string v2, "minus: "
That line replaced const-string v2, "negative: " in Counter.smali. Afterwards:
ngr list strings app_built.apk | grep minus
"minus: "
Deleting a .smali removes that class from the apk: ngr list classes no longer names it.
ngr disassemble shows the same smali for a single dex, and ngr list is useful for
checking the result.
dex/
It exists only with --dex: the dex files of the apk as they are, not disassembled. That tree has no smali/ or
.cache/. build takes the code from dex/ and copies it to the apk. It is the fastest option if you are not
going to change the code.
root/
All the other entries of the apk, with their path: assets/, lib/, META-INF/, kotlin/, org/... There is
nothing special about them: build copies them to the apk as they are in the folder.
- Change a file: edit
root/assets/data.json, runbuild, andunzip -p app_built.apk assets/data.jsonreturns the new content. - Add a file: creating
root/assets/new.txtis enough.buildputs it in the apk andunzip -llists it. - Remove a file: deleting it from
root/removes it from the apk.
The files in root/META-INF/ are the v1 signature of the original apk. They are still there, but the signature is
no longer valid once you change anything, so you have to sign again (“below”).
A new file is compressed. To leave it uncompressed, see uncompressed-files.json.
signatures/
The signing blocks of the original apk (0_V2.signature.info.bin, 1_V3.signature.info.bin, and a padding
block). build puts them back in the apk, but they cover the previous content. The apk that comes out of build
does not verify, even if you changed nothing:
ngr verify -v app_built.apk
DOES NOT VERIFY
ERROR: APK Signature Scheme v3 signer #1: APK integrity check failed. CHUNKED_SHA256 digest mismatch. Expected: <858178d2...>, actual: <318885f0...>
To install it, sign it with ngr sign.
.cache/
The dex files that came out of reassembling smali/: classes.dex, classes2.dex... It exists only if you decoded
with smali (without --dex). Do not edit it. build uses and rewrites it
(“next section”). If you delete it, build creates it again.
How build decides whether to reassemble the smali
The help of build puts it this way: the code comes from dex/ if the tree has it, and otherwise from smali/.
Each smali/classes<N>/ reuses .cache/classes<N>.dex unless some .smali is newer than that dex. In that
case it reassembles it and rewrites .cache/. That is:
- If you touch no
.smaliofclasses2/,builddoes not reassembleclasses2.dex: it uses the one in.cache/. - If you touch one, only the dex of its folder is reassembled. In a test with an edit in
classes/, the date of the.cache/file forclasses.dexchanged and the one forclasses2.dexdid not. --no-cachereassembles everything and overwrites.cache/with the result, without looking at dates.
The comparison is by modification date. If you copy the tree from one place to another and the dates change, pass
--no-cache if in doubt.
archive-info.json
Data from the headers of the original zip (dos_time, ceh_version_made_by, lfh_version_made_by,
version_extract). build uses them when writing the zip. Changing dos_time changes the date the entries of the
built apk carry. You do not need it to edit.
path-map.json
A list of apk entries whose name is not valid as a file name. Each one gives the name it has in the zip (name) and
the one it was given in the folder (alias):
[
{
"name": "assets/colors/rosé-pine.properties",
"alias": "assets/colors/alias_afd0b72a"
},
{
"name": "res/drawable-hdpi-v4/photo.png",
"alias": "res/drawable-hdpi/photo.png"
}
]
The alias is the path inside root/ (for entries that are not resources) or inside resources/package_N/ (for
resources). You edit its content like that of any other file: the built apk carries it under the original name.
What you must not do is rename the alias file without changing path-map.json: the original entry disappears
from the apk and another one appears with the new name.
uncompressed-files.json
Which apk entries were stored uncompressed. It has two lists:
extensions: extensions (.png,.mp3...) that are stored uncompressed.paths: the paths, in the alias name, of the entries that were stored uncompressed.
build stores uncompressed whatever this file says. Removing assets/data.json from paths makes that entry come
out compressed in the built apk. A new file you want uncompressed has to be in paths.
The decode variants
| Command | What changes in the folder |
|---|---|
ngr decode app.apk |
The folder above |
ngr decode --dex app.apk |
dex/ instead of smali/ and .cache/. Everything else is the same |
ngr decode --type raw --dex app.apk |
Nothing decoded: AndroidManifest.xml.bin, resources.arsc and dex/; the res/ entries go inside root/res/ with their zip name. There is no resources/ or AndroidManifest.xml |
The raw tree is useful for extracting the entries as they are, but build does not accept it:
ngr build: unable to build 'appraw': no <package>/values/public.xml found under 'appraw/resources': the tree is incomplete or was not written by 'ngr decode'.
The tree with --dex does build.
Uppercase and lowercase
Two entries whose names differ only in case, such as assets/Case.txt and assets/case.txt, are two files in the
zip. On a file system that does not distinguish case (the usual on macOS and Windows), they land on the same file
and the second overwrites the first. decode warns about it on stderr:
WARN: 1 entry name(s) collided case-insensitively and overwrote each other on this filesystem
The apk you build with that tree does not have both entries: in a test, the built apk carried Case.txt and not
case.txt. If you see the warning, decode on a volume that distinguishes case.
See also
ngr decodeandngr build.- “Modifying and rebuilding an apk”, the full walkthrough.
- “Files each command writes”.