android - "minifyEnabled"与 "shrinkResources"- 有什么区别?以及如何获得节省的空间?

标签 android build.gradle

背景

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

android {
    ...

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

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

When you enable shrinkResources, building your app should display output like the following during the build:

... Removed unused resources: Binary resource data reduced from 2570KB to 1711KB: Removed 33%

问题

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

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

最佳答案

让我们看看

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?

这些将在 gradle 日志中。在 Android Studio 中,我相信这些都显示在 Messages 窗口中(在 Android、Run、TODO 窗口旁边)。

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

minify 运行 ProGuard。 shr​​ink 移除 ProGuard 标记为未使用的资源。

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

不!

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 缩小 CODE ONLYshr​​inkResources 它只是 /res/ 文件夹中的东西。 shr​​inkResources 依赖于 ProGuard 的日志输出来运行。 ProGuard 是真正分析代码以了解未使用的代码的人。

编辑:

我刚刚发现了一篇非常不错的博文。 CommonsWare 将其发布在其他一些 stackOverlow 问题上:http://cyrilmottier.com/2014/08/26/putting-your-apks-on-diet/

它完美地解释了你的后续问题:

why would one depend on the other?

来自帖子:

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.

这意味着,shr​​inkResources 仅比较可绘制对象是否在文件夹中,而不是在 R 类中。

关于android - "minifyEnabled"与 "shrinkResources"- 有什么区别?以及如何获得节省的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30800804/

相关文章:

android - 如何在 Android Studio 项目中包含库模块依赖项?

gradle - 将 gradle 中的包含文件(.properties 文件)构建到 .class 的同一目录中

android - 每次我点击 "run"按钮时,Gradle 中的构建自动化工具是否会替换 apk 文件?

android - 无法在 Android Studio 中删除目录

Android:与 OpenGL 渲染器线程同步

android - datePickerDialog如何设置显示的日期?

android - 尝试添加 firebase ml 视觉图像标签模型时 Gradle 同步失败

java - 关于 SQLite android 中的数组

java - 使用谷歌发布者API上传图标和屏幕截图

android - 如何将 versionCode 转化为 native 代码 (android)