Disable Android resource / image / PNG optimization

23,320

Solution 1

As mentioned in the Android documentation: http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables

Note: Image resources placed in res/drawable/ may be automatically optimized with lossless image compression by the aapt tool during the build process. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. This will result in an image of equal quality but which requires less memory. So be aware that the image binaries placed in this directory can change during the build.

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.

Solution 2

Finally there is an official way to disable the PNG cruncher with Gradle which hasn't been mentioned here yet:

  1. Edit main build.gradle to require gradle version 1.1.3 (or newer):

    buildscript {  
        repositories {  
            mavenCentral()  
        }  
        dependencies {  
            classpath 'com.android.tools.build:gradle:1.1.3'  
        }  
    }  
    
  2. In the individual apps's build.gradle, section android {}, insert:

    aaptOptions {  
        cruncherEnabled = false  
    }  
    

Reference: https://code.google.com/p/android/issues/detail?id=65335

Solution 3

Specifying PNG crunching is now a BuildType property and is disabled by default on debug builds:

android {
    …
    buildTypes {
        release {
            crunchPngs false // or true
        }
    }
}

Note: It's available from Android Studio 3.0 Canary 5 .

Solution 4

Android Studio: Since Gradle Android plugin 1.0.0:

android {
...
  aaptOptions {
    useNewCruncher false
  }
....
}

Eclipse: Override the crunch task writing this in your build.xml:

<target name="-crunch">
   <echo message="This will skip PNG optimization"/>
</target>

Solution 5

 buildTypes {
        release {
            crunchPngs false // or true
        }
    }

add above the line on android block in

in ionic/Cordova project : root_folder_Of_App/platforms/android/app/build.gradle

in Android project: app/build.gradle

enter image description here

For More Information Visit: https://androidstudio.googleblog.com/2017/06/android-studio-30-canary-5-is-now.html

Share:
23,320

Related videos on Youtube

Maris B.
Author by

Maris B.

I love software development! ;)

Updated on July 09, 2022

Comments

  • Maris B.
    Maris B. almost 2 years

    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.

  • Maris B.
    Maris B. over 11 years
    8-bit PNG files with alpha channel -- pretty shadows, many colors, size 200x200 looks ugly. Not perfect. Currently using Photoshop + Web Image Guru + PNGOUTWin postprocess.
  • Vinayak Bevinakatti
    Vinayak Bevinakatti over 11 years
    Yes you cannot have gradient effects, shadow effects etc.. as in PNG in JPG, so only I mentioned use jpg wherever possible.
  • Maris B.
    Maris B. over 11 years
    Accepting your answer because indeed, images that looks nice in 8-bit PNG format are not optimized by the "build" process. However I ended up using Ant + 7-zip + original PNGs.
  • Display Name
    Display Name over 8 years
    this answer does not answer the question at all, I don't understand how it got accepted
  • Patrick
    Patrick over 8 years
    I think thats obselete as of now, use the answer by @ChrisG
  • Patrick
    Patrick over 8 years
    Wow this just cut about 1 min of build time for my debug builds
  • android developer
    android developer about 7 years
    Is it possible to do it only for debug build? Maybe it can reduce build time
  • Taras Vovkovych
    Taras Vovkovych over 6 years
    Thank you. After a lot of time wasted your's only answer helped me.
  • pRaNaY
    pRaNaY over 6 years
    Glade to helps you. Thanks :)
  • xwindows -on strike-
    xwindows -on strike- over 4 years
    I confirm that your Eclipse example also works with command line Ant-based workflow; by enclosing the code in <project> tag, and place them in custom_rules.xml, placed on project's root directory-- I originally thought the override wouldn't work (since PNGs won't be copied to bin/res folder), but it turned out that the toolchain would just use PNGs from original res folder in this case.