Tuesday, 18 March 2014

Decompiling Android | How to get source code from an .apk file.

DISCLAIMER: This tutorial is only for emergency and evaluation purposes. Do not use it for extracting source code of a proprietary application.


Ok, so have you ever been in a condition when you don't have the source code of your application, your whole pc has crashed, your backup hard disk just exploded last night unexpectedly, your pen drive has been just ran over by a full loaded truck and your cloud service provider's server caught fire, you lost your github account info and you have lost all your source and your hope and now your left with only your apk file uploaded to a store. You might think of writing your whole application from the scratch but what if it is a really heavy application with huge blocks of code and numerous java and xml files.


Then, then, then, what would happen?


You don't need to get depressed as we are going to show you how you can extract source code, i.e. Java and XML files from a .apk file. Its not a kind of magic, its just reverse engineering!

Note: We do not take credit of any software used in this tutorial. All these softwares are created by some other people and all credit goes to them.

First and foremost, the prerequisites for this tutorial are:



  1. A Windows (either XP, Vista, 7 or 8, any edition and either 32-bit or 64-bit will do). We will try to release a Linux or Mac version ASAP.
  2. dex2jar from Google.
  3. Java Decompiler : Go here and download JD-GUI.
  4. apktool-version.tar.bz2 and apktool-install-windows-something-.tar.bz2. For extraction use any compression utility e.g. WinZip, WinRAR, 7Zip, etc.
  5. framework-res.apk file.


Now, lets turn on your Windows pc and start:

Step 1: First create a folder sourcecode wherever you want to in your computer and put the downloaded apk file inside of that folder and rename the apk file to app.apk, its not necessary but for simplicity.


Creating "sourcecode" folder and renaming apk to "app.apk".


Step 2: Now rename the app.apk file to app.zip.



At this stage, if you are unable to see the extension of the app.zip after renaming it then press Alt+T+O. Then in the View tab, untick “Hide file extension for common files” and now you should be able to see the .zip extension of the app.zip file.



In Windows 8 and above, you only need to press Alt + H + double-click V to view hidden file extensions.

Step 3: Extracting the zip file. Right-click on app.zip and click on “Extract All...” option and extract the zip file.




In that folder you will be able to see drawable folders, etc. but you cannot directly see .java and .xml files right now.

Step 4: Now copy downloaded dex2jar.zip file into the sourcecode folder and extract it there.





Next, copy the classes.dex file from the extracted folder of the app.zip and put it in the extracted folder of the dex2jar.zip file.




Step 5: Getting .jar file from .dex file.

Now we need to open a command prompt and navigate to this folder. Or easily you can hold the Shift key and right-click on the dex2jar folder and select “Open command window here...”.



Type in the following command :

D:\sourcecode\dex2jar>d2j-dex2jar classes.dex



This will give you the classes-dex2jar.jar file containing the .java files.

Step 6: Getting .java files.

The above step will create the .jar file named as classes-dex2jar.jar which is the jar file of the package containing the sources i.e. .java files. For extracting .java files from .jar file open the downloaded file jd-gui.exe.Select “Open file...” and browse to the generated classes-d2j-dex2jar.jar file. Then select “Save All Sources...” or simply press Ctrl + Alt + S and save the sources in the sourcecode folder with filename src.zip. This will save the .java files as a .zip file.




Now, you have the .java files and the only thing you need now is .xml files.

Step 7: Extracting .xml files.

Now create another folder name “layout” inside the sourcecode folder and put all these inside these:

  • app.apk file
  • extracted files of apktool-v1.x
  • extracted files of apktool install window
  • framework-res.apk file



Open a command windows in this directory as specified in Step 5. Type in the following command:

D:\sourcecode\layout>apktool if framework-res.apk

D:\sourcecode\layout>apktool d app.apk




After the command has completed succesfully you will have folder inside “layout” which contains the .xml files.

Copy all the generated files and folders, i.e. src folder containing .java files and layout containing .xml files in a single folder and open it up either in Eclipse or Android Studio or whatever IDE you work in but you might face some errors that we will soon update on how to remove those errors, until then have fun.

And voila, now you have a complete android package source.

UPDATE: We lately noticed that we have originally downloaded the Linux version of apktool but later on that is rectified.

Thank You and please share if we helped you.

No comments:

Post a Comment