Disable Android resource / image / PNG optimization

左心房为你撑大大i 提交于 2019-11-28 20:29:27
Vinayak Bevinakatti

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.

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

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 .

Yair Kukielka

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>

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:

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.

UPDATE: This has been deprecated in later versions of Android Studio. Please refer to the answer provided by ChrisG.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!