重新编译APK文件遇阻:已用apktool、dex2jar反编译,求教编译方法
Hey there, let's break down how to tackle this APK recompilation issue step by step—I’ve run into these exact headaches before, so let’s start with clarifying the tools you’re using since they serve distinct purposes.
These two tools work on different layers of the APK, so you can’t mix their workflows directly:
- apktool: Decompiles the APK into smali code (Android’s custom bytecode) plus resources (XML, images, etc.) and the
AndroidManifest.xml. This is the tool you’ll use for most full-APK recompilation. - dex2jar: Converts the APK’s
classes.dexfile into a JAR, then lets you decompile that JAR into human-readable Java code. This is great for inspecting/modifying logic, but you can’t directly recompile Java back into an APK—you need to convert it back to dex first.
Since you mentioned apktool is throwing errors, let’s start with the standard apktool workflow and common fixes:
Step 1: Ensure you used apktool correctly for decompilation
Make sure you ran:
apktool d your-app.apk
This creates a folder with smali code, resources, and manifest files—this is the folder you need to recompile.
Step 2: Recompile with apktool (and fix errors)
Run the compile command:
apktool b your-decompiled-folder
If this fails, here are the most common fixes:
- Outdated apktool version: Your 2.3.1 version is pretty old—newer APKs use Android framework features that old apktool can’t handle. Upgrade to the latest stable version (2.9.0+).
- Resource file corruption: Check the error message for mentions of XML files in the
res/folder. Often, decompilation can break XML syntax (e.g., unclosed tags, invalid attributes). Fix those issues manually. - Missing framework dependencies: Some apps rely on device-specific framework resources. Export
framework-res.apkfrom your target device, then install it with:apktool if framework-res.apk - Force compilation (last resort): If you’re sure resources are correct but still get errors, try adding the
-fflag to force overwrite:apktool b -f your-decompiled-folder
If you made changes to the Java files from dex2jar, you need to convert them back to dex and inject them into the APK:
Step 1: Compile Java files to class files
Use javac with the Android SDK’s android.jar as a dependency (match the app’s target SDK version):
javac -cp /path/to/android-sdk/platforms/android-XX/android.jar your-modified-java-files/*.java
Step 2: Convert class files to dex
Use the dx tool from Android SDK’s build-tools folder:
dx --dex --output=classes.dex /path/to/your-class-files/
Step 3: Inject the new dex into your apktool project
Replace the classes.dex file in your apktool-decompiled folder’s smali/ directory (or directly in the APK if you’re working with a zip), then recompile with apktool as outlined earlier.
No matter which method you use, the recompiled APK will be unsigned—you can’t install it until you sign it:
Step 1: Generate a keystore (if you don’t have one)
keytool -genkey -v -keystore my-release-key.keystore -alias my-alias -keyalg RSA -keysize 2048 -validity 10000
Step 2: Sign the APK
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore your-recompiled.apk my-alias
Step 3 (Optional): Optimize with zipalign
zipalign -v 4 your-recompiled.apk your-final-aligned.apk
内容的提问来源于stack exchange,提问作者yeo4




