How to decompile, recompile APK and Frameworks

| 4 min read

Decompile, Recompile APK and Frameworks

It has been over 2 months after I started this blog. Since I work on Android and insides, many of you might be expecting articles related to Android. Today I am going to teach you how to "properly" decompile, edit and recompile android application files (say framework-res.apk which is Android framework. One of the most sensitive apk in Android system, if it is broken your device might end with bootloops)

Caution : This article serves only information on decompiling and recompiling APKs. Do this only if you know what you are doing.

Tools required

  1. [scbutton link="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="blank" variation="green" size="large"  ]JDK or JRE[/scbutton]
  2. [scbutton link="http://forum.xda-developers.com/showthread.php?p=28366939" target="blank" variation="green" size="large"  ]Apktool[/scbutton]
  3. [scbutton link="http://miui.connortumbleson.com/other/apktool/aapt/win/aapt.exe" target="blank" variation="red" size="large"  ]AAPT for Windows[/scbutton] / [scbutton link="http://miui.connortumbleson.com/other/apktool/aapt/linux/aapt" target="blank" variation="red" size="large"  ]AAPT for Linux[/scbutton] / [scbutton link="http://miui.connortumbleson.com/other/apktool/aapt/osx/aapt" target="blank" variation="red" size="large"  ]AAPT for Mac OSX[/scbutton]

Early Setup

  1. Make sure JDK is already present. If not, Install JDK
  2. Rename apktool 1.x.x.jar to apktool.jar
  3. Place apktool.jar and aapt or aapt.exe in the same folder (e.g. C:/apktool/). I will call it apktool directory throughout this article

Setting up base framework

Framework plays important role while decompiling framework dependent apks or system applications. In order to decompile such APK's we need to install framework. Android framework is usually named as "framework-res.apk". Some device manufactures have developed their own framework. E.g. HTC use "com.htc.resources.apk". You need to perform following steps to install framework.

  1. Paste framework in apktool directory.
  2. Run following code[bash] apktool if [framework-name][/bash] You have now installed the framework.
Installing multiple frameworks

What if a developer wants to work on APKs from different devices? Don't worry, we can manage multiple frameworks by tagging them with separate name. Tagging is explained in example below :

  1. Galaxy Nexus framework[bash]C:\apktool>apktool if framework-res.apk gnex[/bash]
  2. HTC Framework[bash]C:\apktool>apktool if com.htc.resources.apk htc[/bash]

Decompile APK

Copy the APK you want to decompile in apktool directory and run following command.

[bash] apktool d nameofapk.apk [/bash]

Decompiling APK with framework dependency

[bash] apktool d [nameofapk.apk][folder][/bash]


[bash]apktool d music.apk music[/bash]

Decompiling APK Options

-s, --no src This will prevent the decompile of the java source. This keeps the APK classes.dex file and simply moves it during re-compile. If your only editing the resources. This is recommended for faster decompile & rebuild.

-r, --no-res This will prevent the decompile of resources. This keeps the resources.arsc intact without any decompile. If only editing Java (smali) then this is the recommend for faster decompile & rebuild.

-d, --debug Decodes in Debug Mode. Currently not supported. Needs rewrite.

-b, --no-debug-info Prevents baksmali from writing out debug info (.local,.param,.line, etc). Credits: Ziggy. Perferred to use if you are comparing smali from the same APK of different versions. The line numbers and debug will change among versions, which can make DIFF reports a pain.

-f, --force Force delete destination directory. Use if trying to decompile to a folder that already exists.

Make your edits, CAREFULLY

Well, changing framework or APK content can break the package. Separate article on "Troubleshooting apktool errors" is on the way, you will learn about all the errors and their solutions.

Ready to cook, recompile the package (APK or Framework).

Recompiling framework or APK is easy but it has been a real pain for many developers. Usually, it takes hours to fix errors that we get while compiling frameworks or apk.  Don't worry about that, my next article will be on troubleshooting such errors.

Once you have finished with editing, just run the following command..

[bash]apktool b [Directory-in-which-package-is-decompiled][/bash]

You are not done yet. Some APKs needs to be signed. There are many APK signing tools available online. However in my next article, I will share my own APK multipurpose tool which Decompiles, Compiles, Optimizes and signs APKs  :-)