“ minifyEnable”和“ shrinkResources”-有什么区别? 如何获得节省的空间?

背景资料

根据 Andriod 文档(给你)的“资源收缩”网页,你可以通过 build.gradle 文件最小化应用程序的大小,方法是使用以下几行:

android {
...


buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

而且,他们说,当使用它的时候,它还会告诉你在这个过程中节省了多少:

当您启用 shrinkResources 时,应该显示构建应用程序的过程 在构建期间输出如下:

删除了未使用的资源: 二进制资源数据从2570KB 减少到 1711KB: 删除33%

那些问题

我找不到这些问题的答案:

  1. 当使用 Android-Studio 本身创建签名应用程序时,我可以在哪里找到保存了多少以及删除/修改了哪些文件的信息?
  2. 究竟什么是“ shinkResources”做的而“ minifyEnable”不做的? 为什么“ shinkResources”依赖于“ minifyEnable”?
  3. 这些选项是否会影响图像文件的大小和/或质量?
  4. ProGuard 不是负责缩小源代码吗?我这样问是因为它说“您必须启用 minifyEnable 才能打开代码收缩”
54824 次浏览

Let's see

When using Android-Studio itself to create the signed app, where can I find the information of how much was saved and which files were removed/modified?

Those are gonna be in the gradle log. Inside Android studio I believe those are shown in the Messages window (next to Android, Run, TODO windows).

What exactly does "shrinkResources" do that "minifyEnabled" don't? And why do "shrinkResources" depend on "minifyEnabled" ?

minify runs ProGuard. shrink remove resources that ProGuard flagged as unused.

Do any of those options affect the size and/or quality of image files?

No!

Isn't Proguard responsible of shrinking source code? I ask this because it says "you have to enable minifyEnabled in order to turn on code shrinking,"

ProGuard shrinks CODE ONLY; shrinkResources it's just the stuff from the /res/ folder. shrinkResources depends on the log output from ProGuard to run. ProGuard is the one who actually analyses the code to know what is unused.

edit:

I've just found a very nice blog post. CommonsWare posted it on some other stackOverlow question: http://cyrilmottier.com/2014/08/26/putting-your-apks-on-diet/

it explains it perfectly your follow up question:

why would one depend on the other?

from the post:

Proguard works on the Java side. Unfortunately, it doesn’t work on the resources side. As a consequence, if an image my_image in res/drawable is not used, Proguard only strips it’s reference in the R class but keeps the associated image in place.

that means, that shrinkResources only compares if a drawable is in the folder but not on the R class.

The answers to the questions 2 and 4 can be found in this video from Android Dev Summit 2015 along with some other useful information on this topic.

An overview of the points discussed were:

  • shrinkResources is taken into account only if minifyEnabled is true

  • minifyEnabled shrinks code, while shrinkResources shrinks resources that are not referenced from the code

  • By default shrinkResources runs in safe mode. If you switch it to strict you can provide tools:keep and tools:discard flags manually to influence the resource shrinking.

  • shrinkResources is useful to reduce the dimension of your generated APK, stripping out any unused resource.
  • minifiedEnabled simply runs Proguard which helps android plugin to package the APK without unused code, in order to shrink it