At Picnic, one of the main contributors to the overall app size are the assets. There are several known ways to work around this issue, from using vector assets, to replacing them with XML drawables, which not only decreases the app size, but also saves up on runtime memory in some cases. However, sometimes you just need a good old fashioned PNG to get the desired effect.

Ok, so what’s the problem? You get your assets from your designers, drop them into the project, trigger a new build, and bam! Your app size just jumped up several MBs. If you’re serious about your product, this should bother you — especially if you have a regular 1 week release train, with a new update being delivered to clients every 7 days, like we have. Chances are that, on the long run, you’ll be adding much more assets than removing them, so do you really want o keep increasing your app’s size indefinitely? Of course not :)

Over time, we’ve experimented with a couple of different approaches, from locally optimizing PNGs every so often, to enabling the pngcrush option in aapt, among others. In the end, what we found worked best was plugging pngquant into our automated build process. Pngquant is a wonderful command-line tool which drastically optimizes PNG files without compromising their quality.

Our current approach consists of 2 parts:

A custom script which invokes pngquant with the desired arguments. A gradle task which executes this script as part of our build process.

So, how do we use it? We start off by defining our custom script (optimize_png.sh):

The above script simply iterates over a directory provided via an argument, and runs pngquant on the PNG files it finds, overwriting the original file with the optimized one (technically it also does some fancier things like checking if a file has already been optimized or not, outputs status updates to the console, keeps a record of the total files optimized, etc). Please note that the script has a dependency on exiftool, so make sure you have it installed, as well as the latest version of pngquant, since older versions are known not to yield the best result.