Fix -o <app>.apk writing the input archive into classes.dex - #171
Open
monperrus wants to merge 1 commit into
Open
Fix -o <app>.apk writing the input archive into classes.dex#171monperrus wants to merge 1 commit into
monperrus wants to merge 1 commit into
Conversation
…asses.dex The argument parser calls setOutFile(-o value) while looping over options, before setInFile() is called with the positional input path. So when -o is given, setOutFile runs with inputType still null: isZip() is false and outDexFile is set to outFile itself instead of a temp dex. For a zip input the Launcher then does: dexBuilder.writeTo(outDexFile); // writes a dex to out.apk Files.copy(inFile, outFile); // overwrites out.apk with input updateZip(outFile, outDexFile, classes.dex); // outDexFile == outFile now so it copies the INPUT APK into the output's classes.dex entry, and the result is unreadable as a dex. Recompute the out dex file in setInFile once the input type is known (only when an output file was already set). With this, 'simplify app.apk -o out.apk' writes the simplified dex into classes.dex; verified the output classes.dex is a real 'Dalvik dex file' instead of an APK. Co-Authored-By: Martin Monperrus (AI-assisted) <martin.monperrus+ai@gnieh.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Running
simplify some.apk -o out.apkproduces anout.apkwhoseclasses.dexentry is the input APK, not the simplified dex — the output is unusable.Cause
The option parser sets the output file while iterating over the flags, i.e.
SimplifyOptions.setOutFile()runs beforesetInFile()is called with the positional input path.setOutFile()picksoutDexFilebased onisZip():But at that point
inputTypeis stillnull, soisZip()isfalseandoutDexFileis set tooutFileitself. For a zip input theLauncherthen does:so it copies the input APK into the output archive's
classes.dexentry.Fix
Recompute the out dex file in
setInFile()once the input type is known (only when an output file was already set), so a zip input gets a distinct temp dex again.Verification
Before:
unzip -p out.apk classes.dex | file -→Zip archive data(the input APK).After:
unzip -p out.apk classes.dex | file -→Dalvik dex file version 035.