Friday, July 5, 2013

Decoding/Converting .apk Files to Source Code

Procedure for decoding .apk files: a step-by-step method

* This article is written by a guy code named "prankul garg" in stackoverflow.com.

Step 1:

Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this.apk file to .zip (eg.: rename from filename.apk to filename.apk.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.

Step 2:

Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this linkhttp://code.google.com/p/dex2jar/ and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from http://java.decompiler.free.fr/?q=jdgui and now double click on jd-gui and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.

Step 3:

Now open another new folder and put these files
  1. put .apk file which you want to decode
  2. download apktool v1.x AND apktool install window using google and put in the same folder
  3. download framework-res.apk file using google and put in the same folder (Not all apk file need framework-res.apk file)
  4. Open a command window
  5. Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
  6. apktool d "fname".apk ("fname" denotes filename which you want to decode)
now you get a file folder in that folder and now you can easily read xml files also.

Step 4:

It's not any step just copy contents of both folder(in this case both new folder)to the single one
and now enjoy with source code...



This is an alternative description - just in case someone got stuck with the description above (as happened how to get source code form apk). Follow the steps:
  1. download apktool-install-windows-r05-ibot.tar.bz2 and apktool1.5.2.tar.bz2 fromhttp://code.google.com/p/android-apktool/
  2. put all files in one folder, i.e. aapt.exeapktool.bat and apktool.jar
  3. open a dos box (cmd.exe) and change into that folder; verify that a Java Environment is installed
  4. Start: apktool decode [apk file]
    Intermediate result: resource files, AndroidManifest.xml
  5. unzip APK file with an unpacker of your choice
    Intermediate resultclasses.dex
  6. download dex2jar-0.0.9.13.zip from http://code.google.com/p/dex2jar/downloads/detail?name=dex2jar-0.0.9.13.zip&can=2&q=
  7. drag and drop classes.dex onto dex2jar.bat
    Intermediate resultclasses_dex2jar.jar
  8. unpack classes_dex2jar.jar
  9. decompile your class files (e.g. with DJ Decompiler; check http://www.neshkov.com/)
    Result: source code
Note: it is not allowed to decompile third party packages; this guide is intended to recover personal source code from an APK file only; finally, the resulting code will most likely be obfuscated