Apktool: APK size increase because of empty file

Created on 18 Jul 2018  路  3Comments  路  Source: iBotPeaches/Apktool

The problem

If an empty file exists in an APK - it causes no-compression for files with the same extension after rebuilding the APK. And, as a result, APK size increases, sometimes significantly, compared to the original APK.

This issue is a special case of issue #1572, which can be solved much easier than the whole #1572. #1572 is currently blocked by #1272, that has no planned fix.

Information

  1. Apktool Version - Snapshot from Git master from 2018-07-17 (2.3.4-eecfc1-SNAPSHOT).
  2. Operating System - Linux.
  3. APK From - Any APK with an empty file and non-empty file(s) with the same extension. For example, it is possible to take https://github.com/iBotPeaches/Apktool/blob/master/brut.apktool/apktool-lib/src/test/resources/aapt1/unknown_compression/deflated_unknowns.apk and add the needed files there.

Steps to Reproduce

  1. Make an APK with an empty (zero-length) file and some (big) file with the same extension, for example into assets/ folder. Let's call these files empty.dat and big.dat. Don't try to avoid compression of the mentioned .dat files in the APK. Rather, try to add them compressed.
  2. List contents of the APK using unzip -v command.
    Observed: big.dat is added with DEFLATE compression method into the APK. For empty.dat the compression does not make sense, so it is added with STORE compression method into the APK.
  3. Rebuild the APK using apktool d and apktool b commands. Check the produced apktool.yml files.
    Observed: dat extension is added to the doNotCompress list.
    Expected: Should not be added just because of an empty file with the same extension.
  4. List contents of the rebuilt APK using unzip -v command.
    Observed: Both big.dat and empty.dat use STORE compression method in the rebuilt APK. The rebuilt APK size is significantly bigger than the original APK size.
    Expected: big.dat should still use DEFLATE method.

Suggested solution

Currently Apktool enforces non-compression for files with the same extension if at least one such file is stored non-compressed in the original APK. But, as mentioned above, "compression method" concept does not really make sense for zero-length files. Thus compression method should be ignored for zero-length files. Files should not be marked for non-compression just because there is an empty file with the same extension. The solution is thus to check that a file is not zero-length, before adding its extension to the doNotCompress list. Such solution should be easy to implement.

Questions to ask before submission

  1. Have you tried apktool d, apktool b without changing anything? - Yes.
  2. If you are trying to install a modified apk, did you resign it? - N/A.
  3. Are you using the latest apktool version? - Yes.
Bug

Most helpful comment

Merged. Thank you!

All 3 comments

Uploading reproduce.zip, containing scripts for reproducing the issue.

Usage:

  1. Unpack reproduce.zip into some directory.
  2. Download https://github.com/iBotPeaches/Apktool/blob/master/brut.apktool/apktool-lib/src/test/resources/aapt1/unknown_compression/deflated_unknowns.apk into the same directory.
  3. Copy the compiled Apktool "fat" JAR, apktool-cli-all.jar into the same directory.
  4. Run:
./reproduce.sh

reproduce.zip

Confirmed. Thanks for detailed solution.

I believe best course of action is what you described, a check for zero-length size and if so, skip adding that file/extension to the doNotCompress list.

Merged. Thank you!

Was this page helpful?
0 / 5 - 0 ratings