Thursday 29 December 2011

Decompiling and displaying the source of an Android APK

This is a Windows-centric post, but it should be fairly straight-forward to make this work in Linux/MacOS by porting the script file and downloading the platform-correct version of JD-GUI.

Why do it
  1. You are curious to see how an application does what it does by reading its code.
  2.  You want to make sure that your application is properly obfuscated before making it public (i.e. you don't want people doing #1 and/or ProGuard is giving you grief).
You will need
What to do
  • Create a directory and name it something appropriate (for example apk-decompile).
  • Download and extract dex2jar and JD-GUI in the directory you created.
  • Make sure you rename the folders to match the file structure below:
  • Create a new text file in the root directory (apk-decompile in our example) and name it "go.bat".
  • Open it in notepad and paste the following:
echo off

REM Make sure we are working off the local directory.
set _=%~dp0
chdir /d "%_%"
echo Working directory is: "%_%"

IF "%~1"=="" GOTO BAD_SYNTAX

echo Converting dex to jar:
call dex2jar\dex2jar %1
echo.

echo Opening Class Viewer:
start "" jd-gui\jd-gui.exe %1.dex2jar.jar
echo.

echo DONT CLOSE THIS DIALOGUE UNTIL YOU'VE FINSHED CHECKING THE FILE AS IT WILL AUTOMATICALLY BE DELETED.
pause

del %1.dex2jar.jar

exit 0

:BAD_SYNTAX
echo Error! Incorrect syntax.
echo The correct syntax is:
echo  %0 ^<path_to_apk^>;
pause
exit 1

How to use this
  • You can either use the command line and type the following: go.bat <path_to_apk>
  • You can drag and drop an apk file onto go.bat.
Notes
 I'm certain that there are other, probably more efficient ways to do this but this suits my purposes (for now).