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.
- [scbutton link=”http://www.oracle.com/technetwork/java/javase/downloads/index.html” target=”blank” variation=”green” size=”large” ]JDK or JRE[/scbutton]
- [scbutton link=”http://forum.xda-developers.com/showthread.php?p=28366939” target=”blank” variation=”green” size=”large” ]Apktool[/scbutton]
- [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]
- Make sure JDK is already present. If not, Install JDK
- Rename apktool 1.x.x.jar to apktool.jar
- 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.
- Paste framework in apktool directory.
- Run following code
apktool if [framework-name]
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 :
- Galaxy Nexus framework
C:\apktool>apktool if framework-res.apk gnex
- HTC Framework
C:\apktool>apktool if com.htc.resources.apk htc
Copy the APK you want to decompile in apktool directory and run following command.
apktool d name_of_apk.apk
Decompiling APK with framework dependency
apktool d [name_of_apk.apk] [folder]
apktool d music.apk music
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.
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.
Decodes in Debug Mode. Currently not supported. Needs rewrite.
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.
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..
apktool b [Directory-in-which-package-is-decompiled]
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 🙂