Retrieving source code from android apk along with all the xml files like AndroidManifest.xml etc. can get really tricky sometimes. This needs a bunch of tools and a step by step procedure which is to be followed.

First thing you need to do is to get the following tools

  1. Android-apktool
  2. Dex2jar
  3. JD-Gui

First one can be downloaded from http://code.google.com/p/android-apktool/. It is a tool for reverse engineering 3rd party, closed, binary Android apps. It can decode resources close to their original form and rebuild them after making some modifications; it makes it possible to debug small amounts of code step by step.  Also it makes working with apps easier because of project-like files structure and automation of some repetitive tasks like building apk, etc.

Dex2jar is available at http://code.google.com/p/dex2jar/. We use this tool to create classes.dex.jar file from classes.dex file. Dex2jar contains 4 components

  1. dex-reader is designed to read the Dalvik Executable (.dex/.odex) format. It has a light weight API similar with ASM.
  2. dex-translator is designed to do the convert job. It reads the dex instruction to dex-ir format, after some optimize, convert to ASM format.
  3. dex-ir used by dex-translator, is designed to represent the dex instruction
  4. dex-tools tools to work with .class files.

JD-GUI is a standalone graphical utility that displays Java source codes of “.class” files. You can browse the reconstructed source code with the JD-GUI for instant access to methods and fields. This is available for download at http://java.decompiler.free.fr/?q=jdgui.

Now, we have all the resources and let us move on to extracting the source code from apk file. Follow the steps given below:-

  • Open the apk file with some file archiving programs like winrar or winzip. Extract the file named classes.dex to some folder.
  • Next to get jar file from this, we have to use dex2jar tool. Copy the classes.dex file to the folder where dex2jar is extracted (otherwise you can include them in path variable). Then open command prompt, navigate to the directory and type the following command

                           dex2jar classes.dex

This will create classes_dex2jar.jar file in the current directory.

  • Now, to obtain the source code, open jd-gui and open the classes_dex2jar.jar created. Hence, you will be able to see entire java files of the apk. Now, to save all files, go to File -> Save All Sources and save files to a new directory.

  • To get the other files, we have to use apktool. You can follow the method given in the screen shot or you can go for this command if your apk is located in the root directory of apktool.jar file and the current directory of command prompt points to the same.

                              java -jar apktool.jar d “Google Play Store.apk” “Google Play Store”  

 

  • Now, move the folders you got from classes_dex2jar.jar to the above folder and you have the complete source code.
 

One Response to Retrieve source code from Android .apk file

  1. soe yan naing says:

    Thanks, that is really useful post.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <p>



This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 47495 items have been purified.