Smali files are a reliable representation of the code, but they're hard to use and they don't integrate with Eclipse and the typical development environment an Android developer is used to. Generating a jar file is more desirable. Of course, we need more than just the jar to compile the app; we also need its resources and the android manifest.
Most tutorials focus on either apktool to get the smali files and the resources or dex2jar to get a jar file which can then be decompiled. Here I'm going to demonstrate how to use both apktool and dex2jar together with Eclipse to reverse and then extend an Android app. The end result will be a development environment that is easy to use and should allow major changes to be made to the app without any significant hardship.
Step One: Get the application's APKThe APK file is how Android apps are stored on your device. Before we can do anything else we need to have the application's APK. There are a couple ways to get this file, but we're not going to get in to them here.
For the purposes of this article I'll be using an APK from a simple app of my own, GasMileage.apk.
Step Two: Deconstruct the APK with apktoolTo deconstruct the APK with apktool, run the following command:
apktool.bat d <path to apk> <destination directory name>
Note that the destination directory shouldn't exist until you run this command.
C:\play\inyourbits>apktool\apktool.bat d c:\play\inyourbits\GasMileage.apk GasMileageIn the destination folder we now have:
testI: Loading resource table...
I: Loading resource table from file: C:\Users\Tom\apktool\framework\1.apk
I: Decoding file-resources...
I: Decoding values*/* XMLs...
I: Copying assets and libs...
Step Three: Generate a jar file from the APKTo create the jar file, use the following command:
d2j-dex2jar.bat <path to apk>
C:\play\inyourbits>dex2jar-0.0.9.9\d2j-dex2jar.bat GasMileage.apkIn the working directory we now have 'GasMileage-dex2jar.jar.'
dex2jar GasMileage.apk -> GasMileage-dex2jar.jar
Step Four: Move the jar fileMove the jar file in to the destination directory created by apktool.
The directory should now contain:
Step Five: Import the directory in to EclipseImport the directory in eclipse as an Android project.
- Click File->Import
- Select 'Android->Existing Android Code Into Workspace'
- Enter the path to your destination directory, in this case 'C:\play\inyourbits\GasMileage'
- Click 'Finish'
Step Six: Import the Jar as a library
- Right click on the project name
- Click 'Properties'
- Click 'Java Build Path'
- Select 'Libraries'
- Click 'Add JARs'
- Select the dex2jar jar file
- Click 'OK'
- Click 'Order and Export'
- Check the box next to the dex2jar
- If we didn't do this then when we built our app the .jar file wouldn't be included with it
Step Seven: Remove the resources from the jarThe jar file created with dex2jar contains the resources classes, however Eclipse is going to build those resources fresh when it compiles the project. So, we need to remove the resource files from the jar file so that the compilation works.
- Open the dex2jar jar file with 7zip (or another zip utility)
- Navigate through the hierarchy until you find the resource class files
- There will be a number of R$ classes as well as one R.class
- Delete all of the resource class files
- Close the jar file
Step Eight: Run the app in the emulatorNow we're going to run the app in the Android emulator to make sure everything is working properly.
- Select the project in Package Explorer
- Click 'Run->Run'
- When asked choose 'Android Application'
- Click 'OK'
- Wait until the application starts and make sure it works