Friday, May 1, 2015

Android - Decompile/Recompile an apk & basic editing

Android application package file (APK) is the package file format used to distribute applications for Google’s Android operating system.
An APK file is an archive that usually contains the following directories:
assets: a directory containing applications assets, which can be retrieved by AssetManager
res: the directory containing resources not compiled into resources.arsc
lib: the directory containing the compiled code that is specific to a software layer of a processor, the directory is split into more directories within it:
    armeabi: compiled code for all ARM based processors only
    armeabi-v7a: compiled code for all ARMv7 and above based processors only
    x86: compiled code for x86 processors only
    mips: compiled code for MIPS processors only
META-INF directory: a directory containing app certificate
    MANIFEST.MF: the Manifest file
    CERT.RSA: The certificate of the application.
    CERT.SF: The list of resources and thier SHA-1 digest
AndroidManifest.xml: Android manifest presents essential information about the app.
classes.dex: The executable code understood by the Dalvik VM
resources.arsc : a file containing precompiled resources

Different approaches to Decompile/Recompile an apk

a. apk
    1. apktool + Notepad++
    2. Virtuous Ten Studio
    3. AndroChef Java Decompiler
b. classes.dex
    1. smali/baksmali
    2. dex2jar + JD-GUI

Steps

1. download the app from phone/tab to computer using adb pull or and Android File manager (e.g. ES File Explorer)
adb devices – List all connected devices/emulator
adb push <local_path> <remote_path> – Copy file/dir to device/emulator
adb pull <remote_path> [<local_path>] – Copy file/dir from device/emulator
adb logcat – View device log
adb install [-l] [-r] [-s] <file> – push this package file to the device
adb uninstall [-k] <package> – remove this app package from the device
If using File manager, backup the app and copy it to sdcard to transfer it to computer
2. Now use any of the above approach as applicable

a. apk

apktool -> command line approach
install an apk            java -jar apktool.jar if app.apk
                          java -jar apktool.jar if framework-res.apk
decompiling an apk        java -jar apktool.jar d app.apk
recompile the folder      java -jar apktool.jar b appFolder


1. Install Framework file {only once}
2. Decompile the apk
3. Edit the code as required {using Notepad++, others}
4. Recompile the apk
5. Sign the apk {See Below}
6. ZipAlign the apk {See Below}
Virtuous Ten Studio -> GUI + very useful smali help {opcode details on hover} + drop smali files from other projects to understand the code
It automates the whole process. You only need to EDIT files and it does everything else.
AndroChef Java Decompiler -> very useful java decompiler to understand the code

b. classes.dex

Sometimes apktool is not able do decompile an app because of DexGuard, Proguard, etc; in that case alternative is to extract classes.dex from the apk and go for any of the 2 below mentioned approaches
smali/baksmali
smali.jar        smali files to classes.dex
java -jar smali.jar OutputDir -o classes.dex

baksmali.jar     classes.dex to smali files
java -jar baksmali.jar classes.dex -o OutputDir


1. Install Framework file {only once}
2. Extract classes.dex from the apk
3. Decompile the classes.dex using baksmali.jar
4. Edit the code as required {using Notepad++, others}
5. Recompile the classes.dex using smali.jar
6. Replace original classes.dex in the apk with the new classes.dex
7. Sign the apk {See Below}
8. ZipAlign the apk {See Below}
dex2jar+JD-GUI
Convert .dex -> .jar using dex2jar.jar and open it in JD-GUI
3. Sign the apk
Every app needs to be signed before it can be installed on Android OS.
java -jar signapk.jar certificate.pem key.pk8 app.apk app_signed.apk
4. Zipalign the apk
is an archive alignment tool that provides important optimization to Android application (.apk) files
zipalign -fv 4 app_signed.apk app_final.apk
5. The modified app.apk is ready to by deployed.

Source: https://themasterofmagik.wordpress.com/2014/03/08/decompilerecompile-an-apk-basic-editing/

0 comments:

Post a Comment