Decompiling and Recompiling APKs with APKTOOL: A Beginner's Guide
Written on
In my quest to decompile and recompile an APK for personal experimentation, I encountered numerous articles and blogs. Many of these either omitted crucial steps or utilized various tools. Consequently, I decided to compile an article that details the process of using Apktool for decompiling and recompiling apps, integrating the insights I gathered along with a practical example.
Understanding the Essentials
Apktool: A utility for reverse engineering Android APK files. Keytool: Used to generate a new keystore file for signing the decompiled APK. Apksigner: A tool for signing APKs. Zipalign: Helps align the decompiled files.
Additional Tools: JD-GUI: A Java decompiler. dex2jar: Tools for working with Android .dex and Java .class files.
Requirements
Decompiling APKs is straightforward on Mac and Linux systems. Windows may require additional configurations, which I haven't explored. To start, ensure you have both the JDK and Android SDK installed on your machine.
To install Apktool on a Mac, simply use Brew:
brew install apktool
For a detailed installation guide, refer to this link.
The Experiment
I have an APK with ProGuard enabled that displays the text "Original App" on the screen. The objective of this experiment is to: - Decompile the APK - Alter the background and text colors - Change the text from "Original App" to "Recompiled App" - Successfully recompile and sign the app
Let's Begin
Stage 1: Decompilation
You can download the APK of any application from ApkMirror.com or similar sites. For this example, I'm using the signed APK of my sample app, "experiment_app.apk". To initiate the decompilation process, use the following command with apktool:
apktool d [apk location] -o [output folder location for decompiled files]
Upon executing the command, Apktool creates a new folder named "experimentapp_decompiled" in my documents directory.
If you prefer not to decompile resource files from the APK, use this command instead:
apktool d -r -s [apk location] -o [output folder location for decompiled files]
Stage 2: Modifying Files
Within the decompiled files folder, I located strings.xml and colors.xml.
You can open these resource files in Android Studio by dragging them into the window or using any XML editor. I modified the background color to Red and the text color to Yellow, changing the text to "Recompiled App".
Stage 3: Recompiling to APK
After making the necessary changes, I will recompile the modified files back into an APK using the command:
apktool b [decompiled files root folder location]
Apktool will compile the files and generate an APK, which will be placed in a new folder named "dist" located in the same root folder as the decompiled files.
Stage 4: Zipaligning the APK for Optimal Loading
Zipalign is a tool that ensures all uncompressed files in the archive are aligned correctly. You can find the Zipalign tool in the "Build Tools" folder within the Android SDK path.
To zipalign the APK, execute the following command:
zipalign -v 4 [your recompiled apk] [location to store the zip aligned apk with name and extension]
Stage 5: Creating a New Keystore File for Signing the Zipaligned APK
To create a keystore file using keytool, run the following command, which will prompt you for a password and additional keystore details:
keytool -genkey -v -keystore [your keystore name] -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
Stage 6: Signing the App with Apksigner
The Apksigner tool, included in Android SDK Build Tools (revision 24.0.3 and higher), allows you to sign APKs and confirm their signatures will be verified successfully on all supported Android platform versions. You can find Apksigner in the "Build Tools" folder.
Use the following command to sign the APK:
apksigner sign --ks [your keystore name] --v1-signing-enabled true --v2-signing-enabled true [your zip aligned apk location]
Stage 7: Verifying the Signed APK
You can verify the zip-aligned and signed APK using the same Apksigner tool:
apksigner verify [signed apk location]
Stage 8: Installing the App
You can install the verified APK using the adb command or manually:
adb install /Users/matrix/Documents/APK/experimentapp_zipaligned.apk
The background and text colors of the app have been successfully changed!
The experiment was a success!
dex2jar [classes.dex file location in the decompiled folder]
Now, utilize the JD (Java Decompiler) to examine the source:
jd-gui [classes-dex2jar.jar location]