2012-02-04

Experiment the Tips for reducing APK file size

I came across this article: Tips for reducing APK file size at SonyEricsson Developer blog. Among them, the one I am aware of is the PNG file optimization. So, I took some time to experiment it.

First, I downloaded the GUI wrapper of command-line optimizer ImageOptim and optimize the PNG files at res/ directory. According to the result, it reduce total file size from 748K bytes to 700K bytes.

Second, I recompile the release binary of my application. But, the size of APK file remains the same as the one w/o PNG optimization.

Hmm. I repeated this process some times to make sure I didn't miss some important steps. While I was wondering, I noticed some messages spewed out during compilation like,
  [crunch] Processing image to cache: /Users/yenliangl/Work/Android/bozaalarm-android/res/drawable-hdpi/handler_app.png => /Users/yenliangl/Work/Android/bozaalarm-android/bin/res/drawable-hdpi/handler_app.png
Looks like PNG optimization has been included in the standard Android tool v14??

This should confirm my guess. From official site of Android tool, it says that in revision 14, aapt optimizes PNG during compilation.

png processing in aapt.
When aapt packages the resources, its main goal is to compile the XML to binary format and to create a resource table with all the resource values (string, color, ids, etc...). Additionally, it processes the png files to optimize them (for instance, pre-processing of 9-patches).
Because the aapt process is not incremental, this means every build goes through all png files and processes them always. For large projects with numerous (and/or large) png this process could take a long time.
Revision 14 now processes the png files outside of the aapt packaging step and caches them. Only modified png files are re-processed.