After decompiling and modifying an APK file, you’ll need to recompile it into a working APK package that can be installed on Android devices.
Recompiling allows you to embed your code changes, resource edits, and any customization of the app back into a signed and distributed APK.
In this comprehensive guide, we’ll cover how to recompile an APK file using the key tools required in the build process.
Overview of Recompiling APKs
Here is a general outline of the APK recompiling process after making modifications:
- Add your source code changes – Merge any decompiled code you edited back into the project.
- Include custom resource files – Integrate new resource assets like images, UI layouts, strings etc.
- Rebuild R file – The R class mapping resources must be regenerated.
- Validate manifest – Double check the final manifest configuration.
- Generate unsigned APK – Bundle the files into an unsigned APK package.
- Sign APK package – Sign the APK with a certificate for authentication.
- Zipalign APK – Optimize APK for efficient installation.
- Verify and install – Validate the final APK and install it on a device.
With the right tools, these steps allow you to automate rebuilding a customized APK in the correct format needed for distribution and installation.
APK Recompiling Tools
Here are some key open source tools commonly used to recompile APKs after making modifications:
- Apktool – All-in-one tool for disassembling and rebuilding APKs
- AndroChef Java Decompiler – Decompile/recompile APKs and analyze Java sources
- Android Studio – Official IDE includes APK build/signing capabilities
- Apk Studio – Graphical IDE supporting easy APK decompilation and recompilation
- Zipalign – APK alignment tool included in Android SDK for optimization
- Keytool – Generates private keys for signing APKs (included in Java JDK)
- Jarsigner – Signs JAR/APK packages with private keys (included in Java JDK)
These tools together provide the required functionality to successfully rebuild a working APK file.
Step-by-Step Guide to Recompile an APK
Follow these key steps to recompile an APK file after making source code modifications or resource customizations:
Step 1: Set Up Build Tools
Install tools like Apktool, Android Studio, Zipalign etc. on your computer. Make sure you can access key commands needed for the build process like aapt, apktool, zipalign etc.
You’ll also need the latest Android SDK platform-tools and build-tools. Set up environment variables if needed.
Step 2: Add Source Changes
Merge any modified Java source code files you edited back into the project structure correctly.
This may involve reconstructing packages, making sure new classes compile properly against unmodified code etc.
Step 3: Include Custom Resources
Replace original app resources like layouts, drawable assets and values with your custom files. Make sure IDs, names and references match.
You can modify resources without changing source code, so this step is optional if you only decompiled the Java sources.
Step 4: Validate Manifest
Open the final AndroidManifest.xml used by your build. Make surepermissions, app name, activities declared properly match your modified package.
This ensures your app has the correct configuration.
Step 5: Generate R Class
The R class maps resource IDs to names. Regenerate this using:
aapt package -m -J gen -M AndroidManifest.xml -S res -I android.jar
This outputs an up-to-date R.java class with your resources.
Step 6: Compile Java Sources
Compile your edited Java sources and newly generated R class using the command:
javac -source 1.7 -target 1.7 -bootclasspath android.jar -d . src/**/*.java
This will output .class files under proper package directories.
Step 7: Convert Classes to DEX
Convert the compiled sources to Dalvik bytecode format using dx:
dx --dex --output=classes.dex src
This outputs a classes.dex file containing the compiled app code.
Step 8: Generate Unsigned APK
Bundle the manifest, resources, assets and classes.dex into an unsigned APK using:
aapt package -f -m -J gen -M AndroidManifest.xml -S res -I android.jar
This will generate an unsigned.apk package.
Step 9: Zipalign APK
Optimize the APK file for efficient install using:
zipalign -v -p 4 unsigned.apk aligned.apk
This produces an optimized aligned.apk per Google standards.
Step 10: Generate Private Key
Generate a private key for signing using keytool command:
keytool -genkeypair -validity 10000 -keystore mykeystore.keystore -alias myalias -keyalg RSA -keysize 2048
Save the mykeystore.keystore file containing certificate info.
Step 11: Sign the APK
Finally, sign the aligned APK package using the keys:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 aligned.apk myalias -keystore mykeystore.keystore
This signs aligned.apk with your private key and outputs signed.apk.
Step 12: Verify and Install
Visually inspect the final APK contents to verify correctness. Then install signed.apk on a device to validate functioning!
And that’s it! With these key steps, you can take a modified APK project and rebuild it into a proper installable package.
APK Signing Explained
When recompiling an APK, the most important step is properly signing the final package. Here’s some background on why signing is crucial:
- All APK files must be digitally signed with a certificate and private key.
- Signing tags an APK with author identity to prove authenticity.
- Android devices check signatures and won’t install apps without verified author.
- Signature requirements stop arbitrary APK tampering.
- Same author certificates must be used to sign updated versions of an app.
- Decompiling strips signatures. Recompiling must sign with new certificate.
- For personal use, you can generate a new dummy certificate for signing.
- For release, you’d need the original dev’s private key for official signing.
In summary, signing the recompiled APK proves you created the package even if the code is modified from original sources. Apps require verified signatures before Android will install them.
Troubleshooting APK Recompilation Issues
When trying to recompile an APK, you may run into errors like:
- Resources ID not found – R class mapping incorrect resources. Clean and regenerate.
- Manifest merger errors – Indicates manifest issues with your changes. Audit for inconsistencies.
- Apktool exceptions – Something wrong with decompiled files or framework version. Start over.
- Missing libraries – Your code changes may require additional libraries be packaged into APK.
- Certificate errors – Key store password incorrect or signature mismatch with previous APKs.
Thoroughly audit each stage of the recompile process until you isolate the cause. Common sources of errors include improper R file generation, manifest mismatches, custom code bugs, and incorrect certificates.
Legal Considerations of APK Recompilation
While decompiling apps for educational purposes falls under fair use exemptions, recompiling introduces some legal considerations:
- You likely violate copyright by redistributing recompiled APKs publicly without permissions.
- Making source code changes then publishing apps can infringe on original IP rights.
- However, modifying an APK for your own personal use may be defensible as fair use.
- Generating dummy certificates is fine, but you cannot sign APKs with stolen keys.
In summary, recompiling APKs is allowed for private education and research. But public distribution of modified APKs built on others’ code likely violates IP protections, absent a public license allowing derivates.
Recompiling an APK allows you to integrate your code and resource modifications back into a working Android application.
Key steps include:
- Merging source code changes and custom resources
- Validating manifest configurations
- Generating R class and compiling Java sources
- Aligning and signing package with private key
- Installing and testing final signed APK
Following this process carefully allows you to rebuild a fully functioning APK from decompiled sources. Just take care to comply with legal protections when publishing derivatives.
With the right tools and testing, you can customize APKs to your needs through decompilation and recompilation techniques.