Modifying and rebuilding an apk
Five steps: decode, change, build, sign and check.
ngr decode app.apk
# ...change whatever you want in the app/ folder...
ngr build app
ngr sign --debug-key app_built.apk
ngr verify -v app_built_signed.apk
1. Decode
ngr decode app.apk
ngr decode writes the app/ folder next to the apk. If it prints a warning about names
that clash by case, read it before going on: the apk you build will not have all the entries of the original
(ngr decode).
If you are only going to touch resources, --dex leaves the code as it is and saves time in both directions:
ngr decode --dex app.apk
2. Change
Inside app/ everything is where you would look for it:
| I want to change... | Where it is |
|---|---|
| a text, a color, a dimension | resources/package_1/res/values*/ |
| a layout, a menu or other xml | resources/package_1/res/<type>/ |
| an image | resources/package_1/res/drawable*/, mipmap*/ |
| a permission, an activity, the version | AndroidManifest.xml |
| the code | smali/classes*/, one .smali per class |
an assets/ file or a native library |
root/assets/, root/lib/ |
“The decode tree” describes each part and what build does with it.
The code is edited in smali, not in Java. ngr decompile is for reading and understanding the code, and with that
finding which line of smali to change: its output is not put back into the apk.
3. Build
ngr build app
ngr build writes app_built.apk next to the folder. It only reassembles the dex files
whose smali you have touched. The others it takes from .cache/.
The built apk is not the original with your changes, byte for byte: the order of the zip, the header marks and the line numbers of the xml files are derived again.
4. Sign
The apk you have just built cannot be installed: the signature it carried no longer matches its content. Sign it.
To test on your device, with the debug key:
ngr sign --debug-key app_built.apk
With your own key:
ngr sign --ks release.p12 --ks-pass env:KS_PASS app_built.apk
ngr sign aligns the apk when it signs it and writes app_built_signed.apk, with its
.idsig next to it. You do not need a separate alignment step.
5. Check
ngr verify -v app_built_signed.apk
If it ends with code 0 and the first line is Verifies, the apk is properly signed
(“Signing and verifying”).
Install
ngr does not install. With the Android platform tools:
adb install app_built_signed.apk
Android does not install an apk over another with the same package name and a different signature. If the original app is installed, signed by its author, you will have to uninstall it first, and its data goes with it.
Repeat
While you iterate, -f lets you overwrite the outputs of the previous step:
ngr build -f app && ngr sign -f --debug-key app_built.apk
Do not use ngr decode -f on a folder you have worked in: it deletes the whole folder, with your changes.
See also
ngr decode,ngr build,ngr signandngr verify.ngr disassembleandngr assemble, if you only want to work with a dex.