How do I prevent the Android "build process" from optimizing .png images?
I have an Android project with the following res directories:
- /res/
- /res/drawable
- /res/drawable-hdpi
- /res/drawable-hdpi-v5
- /res/drawable-ldpi
- /res/drawable-ldpi-v5
- /res/drawable-mdpi
- /res/drawable-mdpi-v5
These directories contain many .png files. I optimize PNG sizes with PNGOUTWin, and the overall size is reduced by more than 20%. When I build the .apk file, the images are "optimized" by the build process and the overall size is now 10% above the initial size, or 30% above my/PNGOUTWin optimized size.
My goal is to reduce the .apk size, even if it will affect the final performance, memory requirements, etc. How do I prevent the "build process" from optimizing .png images?
I'm targeting Android 2.2 and above.
P.S.: I am currently building my Android project from Eclipse, but I will switch to the automated build later (Ant?).
Note about JPG images: JPG will not work, because they do not have transparency.
Finally there is an official way to disable the PNG cruncher with Gradle which hasn't been mentioned here yet:
Edit main build.gradle to require gradle version 1.1.3 (or newer):
In the individual apps's build.gradle, section android {}, insert:
Reference: https://code.google.com/p/android/issues/detail?id=65335
Specifying PNG crunching is now a BuildType property and is disabled by default on debug builds:
Note: It's available from Android Studio 3.0 Canary 5 .
Google recently introduced a new PNG processor in aapt 0.9.1 in the Android SDK Build Tools that fixes this issue of increased PNG sizes after aapt optimization.
With this update, it is now possible for Android Studio & Gradle to switch between the PNG processors with the following change in your build.gradle configuration file:
By adding this line, aapt uses the new PNG processor in which it checks to see if the "optimized" PNG files are smaller than the original PNG files. I was able to reduce 4.8 MB in my compiled APK and have not encountered any bugs/issues with the new build configuration.
UPDATE: This has been deprecated in later versions of Android Studio. Please refer to the answer provided by ChrisG.
Android Studio: Since Gradle Android plugin 1.0.0:
Eclipse: Override the crunch task writing this in your build.xml:
As mentioned in the Android documentation: http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables
So if you want to reduce the size of your application you should either reduce the color-depth of your PNG files (this helps a lot) or switch to .JPG files wherever possible.