Sunday, April 21, 2013

Batch File Decompiles Android apk to Java Source With a Single Command

Batch File Decompiles Android apk to Java Source With a Single Command

This batch file decompiles an apk to its corresponding java sources. People who are looking forward to do a code review on an android app who’s source code is not readily available can utilize this bat. This batch runs various free tools available on the internet in a sequence to obtain the java source files.
This is not made to encourage piracy/plagiarism in any case.

How To

1. Extract batch file and lib folder to C:\apk2java\ (or any folder that doesnt have space in its path)
2. Backup the target app’s apk from phone to PC via ASTRO Browser (check this post for details)
3. Keep the target apk in the root folder where batch file is present
Copy target apk to exec folder
4. Run ‘apk2java.bat target.apk’ in cmd
c:\apk2java>apk2java.bat target.apk
Execute Command
Process Complete
5. Result : java and resource files available in ‘src’
src folder containing decompiled files
Note: This batch just automates the sequence in which various tools are initiated and does not handle any error events. You will have to go through the cmd verbose to figure out the problem.
Note 2: ‘lib’ folder contains apk-tool files (apk-tool.jar, aapt.exe), jad.exe, 7zip (7za.exe), dex2jar files (all other jars).  If required, update each of those tools by replacing it with latest copy from links mentioned below.

Requirements

  • Windows (but can be ported to *NIX)
  • JRE 1.6 (Java Runtime Environment)

Tools in lib

  • Dex2jar – Converts Android dex format to jar (link)
  • JAD – Java Decompiler CLI (link)
  • 7Zip – Unarchival  (link)
  • apk-tool – Extracts resources from apk (link)
  • aapt – Android Asset Packaging Tool (link)
  • aapt commands (link)
@echo off
echo *********************************************
echo **          Convert 'apk' to 'jar'         **
echo *********************************************
set CLASSPATH=
FOR %%i IN ("%~dp0lib\*.jar") DO CALL "%~dp0lib\setclasspath.bat" %%i
java -Xms512m -Xmx1024m -cp "%CLASSPATH%" com.googlecode.dex2jar.v3.Main %*

cd /d %~dp0

echo *********************************************
echo **    Remove if any 'src' folder exists    **
echo *********************************************
rmdir /s /q src

echo *********************************************
echo **    Create 'src' and 'class' folders     **
echo *********************************************
mkdir src
cd src
mkdir other
cd..
mkdir class

echo *********************************************
echo **     Expand, delete the 'jar' file       **
echo *********************************************
%~dp0lib\7za.exe x *.jar -o%~dp0class -aou
del *.jar 

echo *********************************************
echo **        Decompiling class files          **
echo *********************************************
xcopy /Q /E /Y class src
for /R src %%a in (*.class) do %~dp0lib\jad.exe -d %%~dpa -o -s .java "%%a"

echo *********************************************
echo ** Delete .class files and 'class' folder  **
echo *********************************************

cd src
del /S /Q *.class
cd..
rmdir /s /q class

echo *********************************************
echo **      Extract, fix resource files        **
echo *********************************************
java -jar "%~dp0lib\apktool.jar" decode -s -f *.apk %~dp0src\other\

echo ]                                               
echo *********************************************
echo **  Process complete - Check 'src' folder  **
echo *********************************************
echo ]
apk2java Info
App Name apk2java
License free
Type code
App URL Download
More Info link

No comments:

Post a Comment