Android Studio: Disabling AAPT PNG Optimization with Gradle

For those that are developing Android apps and are using a lot of pre-optimized PNG images, you want to update to the latest Android SDK Build Tools 19.0.3:

0.9.2

Aapt-based PNG processor is now default again while we investigate some issues with the old one.

flavorGroups have been renamed flavorDimensions and the DSL has been updated. The old DSL is still available until 1.0 at which time it’ll be removed.

0.9.1

It’s now possible to include a file when there’s a conflict during packaging:

android.packagingOptions { pickFirst ‘META-INF/foo.txt’ }

New PNG processor.

Should be much faster when processing many files

Fix issue where crunched png are bigger than original file

To revert to the old cruncher: android.aaptOptions.useAaptPngCruncher = trueWARNING: We’ve seen reports of the new processor generating PNGs that make the app crash on GB

The plugin now enforces that all library dependencies have a unique package name.

To disable this you can use android.enforceUniquePackageName = false

WARNING: The ability to disable enforcement will disappear in 1.0

Fixes:

Generated POM files now have the proper dependencies even if the pom object is manipulated in build.gradle

libraryVariant API now gives access to the list of flavors.

fixed issue where changes to the manifests of libraries didn’t trigger a new manifest merge.

BuildConfig.VERSION_NAME is always generated even if the value is not set in the current variant.

BuildConfig is now packaged in libraries. This requires that all your libraries have a unique package name.

If you are disabling enforcement of package name, then you should disable packaging of BuildConfig with: android.packageBuildConfig = false

WARNING: the ability to disable packaging will disappear in 1.0

For those that are unfamilar with aapt, it is the SDK compile tool responsible for packing your app into an APK package. As part of the packing process, aapt also performs file optimization on certain media files, such as PNG files. It uses a built-in PNGcrush to reduce PNG file sizes as part of the process. Normally, this is a good step to perform prior to building the APK. However, when you have already optimized your PNG files with tools like TinyPNG and offer greater savings than PNGcrush, this step is completely unnecessary. Also, it greatly adds to the compile time for building your Android application.

While developing Dragon Geo, I was always suspicious on why my APK was much larger than it should be, given my already optimized PNG files. However, it now makes sense with the latest update notes on the Android SDK Build Tools, as it seems that for the longest time, the PNG processor in the aapt was not checking whether the ‘crushed’ PNG files were actually smaller than the original PNG files it was crushing! Google finally mitigated this issue by fixing aapt in 0.9.1. Along with this long-awaited fix, Google has now made it possible for Android Studio/Gradle users to use the new PNG processor in aapt with the following change in the build.gradle configuration file:

android { ..

aaptOptions.useAaptPngCruncher = false}

Although the new PNG processor is disabled in 0.9.2, it is still possible to use the new PNG processor in aapt with this command. I recently tested this Gradle configuration for the Dragon Geo app to see the difference in APK size from the older builds. In the case for Dragon Geo, the size reduction was dramatic; the APK size was roughly 4.8 MB smaller! Although a few Android developers have reported issues with the new PNG processor in aapt, I have found no bugs in Dragon Geo with this new build configuration.