8

When I export an apk from eclipse, it compresses all of the png images. For example, one 4.6 KB png became 2.15 KB inside the apk. However I prefer to optimize png files using tinypng before the apk is built, which makes much smaller files. The same 4.6 KB png became 746 B using tinypng. When building the apk, compression ran on my 746 B png making it 1.6 KB.

Is there any way to disable this optimization for future builds?

dehrg
  • 1,721
  • 14
  • 17
Minas
  • 1,422
  • 16
  • 29
  • This link may help you :) http://stackoverflow.com/questions/12929884/disable-android-resource-image-png-optimization – Rajesh Mar 08 '14 at 13:24

5 Answers5

6

In your project build.xml ANT file you can override the -crunch task of the SDK, thus avoiding PNG compression, as you previously compressed them:

<target name="-crunch">
    <echo message="Skipping PNG optimization"/>
</target>
Juan Sánchez
  • 980
  • 2
  • 10
  • 26
  • Thanks, but I'm not using ANT, can you please tell me how can I create this build.xml file and build project using ANT? – Minas Mar 10 '14 at 06:35
  • 1
    You can copy the one from `android-support-v7-appcompat` for example to your project's root folder and tune it with the overriden target. If you are using ADT, move this tuned `build.xml` file then to an ANT view, open it to check all the targets, and run the `release` one. – Juan Sánchez Mar 11 '14 at 01:45
4

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 and Gradle to switch between the PNG processors with the following change in your build.gradle configuration file:

android {
    ...
    aaptOptions.useAaptPngCruncher = false
}

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.

Thomas Keller
  • 5,933
  • 6
  • 48
  • 80
Huh X0015
  • 69
  • 7
  • Good to know! thanks. I'm still using Eclipse & ant. Will use this option when I switch to Android Studio & Gradle – Minas Mar 28 '14 at 10:17
  • 3
    By now (Gradle Android plugin 1.0.0-rc1) the name of that option has changed to `aaptOptions.useNewCruncher` and the logic was inverted, i.e. setting `aaptOptions.useNewCruncher = false` would disable using the new PNG compressor. – sschuberth Nov 25 '14 at 11:14
  • As of Android Studio 1.0 & Gradle 1.0.0, this option doesn't seem to work. All of my already-optimized PNG files are still being re-processed, resulting in PNG files that are 10-15% larger than the original PNG files. – Huh X0015 Dec 09 '14 at 02:36
  • 1
    Did u add the new section aaptOptions {noCompress 'png', 'jpg'} – slott Jan 09 '15 at 13:26
  • Thanks for this! In my case this solved a problem of "No resource found that matches the given name..." that was in fact caused by a malformed .9.png (missing its borders). Only with this option the true cause was revealed. After fixing putting the Cruncher back in action worked as intended. – The Vee Sep 26 '15 at 17:09
  • This is deprecated now – sud007 Jun 13 '17 at 06:54
3

Since Gradle Android plugin 1.0.0:

android {
...
  aaptOptions {
    useNewCruncher false
  }
....
}
Yair Kukielka
  • 10,686
  • 1
  • 38
  • 46
2

Change the image file extension. For example: renaming someImage.png to someImage.jet worked for me. You can try this.

-1

Optimize the files using tinypng before importing them into the project.

Trent Pierce
  • 387
  • 3
  • 22
  • I do optimize before importing.. My problem is that it get compressed again (which makes png bigger in this case), so I'm trying to disable that. – Minas Mar 06 '14 at 09:57
  • 1
    @Minas, you should put your comment in your question, it will fully clears the question then. – Farhan Mar 06 '14 at 10:28