89

Background

According to the "Resource Shrinking" webpage of Andriod documentations (here), you can minimize the app's size via the build.gradle file, by using these lines:

android {
    ...

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

And, they say that when using it, it will also tell you how much is saved in the process:

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%

The questions

I can't find out the answers to those questions:

  1. 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?
  2. What exactly does "shrinkResources" do that "minifyEnabled" don't? And why do "shrinkResources" depend on "minifyEnabled" ?
  3. Do any of those options affect the size and/or quality of image files?
  4. 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,"
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    shrinkResources removes all unused resources, so no quality issues or other. mimifyEnabled doesn't remove it. I think that it's mandatory use mimifyEnabled to have the list of unused resources. – Ivan Jun 12 '15 at 10:37
  • So having "minifyEnabled " alone doesn't do anything? and "shrinkResources" doesn't shrink anything? How can I see the saved space? and which files were removed? – android developer Jun 12 '15 at 10:38
  • 1
    I think yes, as you can read here http://tools.android.com/tech-docs/new-build-system/resource-shrinking to use shrinking you must have mimifyEnabled true. This because mimifyEnabled removes unused code and shrinkResources removes unused resources (most or all of them called in unused code). – Ivan Jun 12 '15 at 10:42
  • Isn't proguard responsible of removing unused code? – android developer Jun 12 '15 at 10:47
  • 1
    minifyEnabled enables proguard which removes unused classes/code and shrinkResources removes unused resources (pngs, xmls, mostly introduced by libraries which you don't fully utilize). – Eugen Pechanec Jun 12 '15 at 10:51

3 Answers3

79

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • 1
    So, "shrinkResources" doesn't shrink at all. Should have been called "excludeUnusedResources" instead, no? why do "shrinkResources" depend on "minifyEnabled" , if one is related to resources and one to source code? Also, for the amount of saved MB, I can't see it there (tested). I've also checked the "Gradle Console" (on the bottom right). – android developer Jun 12 '15 at 15:02
  • yeah, maybe it's not the greatest naming, but what can we do? Maybe in some version they change it. It depends on `minify` because that is where the analysis for unused resources happen, the `shrink` is just deleting. – Budius Jun 12 '15 at 15:03
  • regarding log, I don't know then. According to the document you pointed if you build via command line you can get it. – Budius Jun 12 '15 at 15:04
  • About the requirement, but since one is different than the other (source vs resources), why would one depend on the other? Also, can you please try it out on Android Studio and see if you find the log? I've tried so many things there... – android developer Jun 12 '15 at 16:57
  • Makes sense, but what about resources that are referenced from others? and how does the R gets stripped anyway using proguard? this is weird. – android developer Jun 17 '15 at 14:33
  • there's nothing weird. ProGuard analyses generated java classes and strip away unused stuff. `R.java` is a generated class full of `static int`, any of those that are not used get removed by ProGuard. If you really want to understand more I suggest you clone the source code (as this is all open source) and check it for yourself. – Budius Jun 17 '15 at 14:38
  • But if the resource exists, its R value also exists, even if it's not used anywhere. – android developer Jun 17 '15 at 14:44
  • How could proguard remove it, if there are non-java-files that reference to resources (like manifest file/s)? – android developer Jun 17 '15 at 14:48
  • you can check there http://proguard.sourceforge.net/ please stop messaging me. It's bothering. – Budius Jun 17 '15 at 14:49
  • @Budius any reason to keep alone **proguardFiles getDefaut...** setting with **minifyEnabled false**. Because it will not do anything !! OR it still do something else. – CoDe Aug 08 '16 at 07:39
  • 1
    @Shubh ProGuard still does everything that ProGuard does (removes unused java code, obfuscate code, opmise class, etc). – Budius Aug 08 '16 at 08:26
  • In my recent testing, contrary to the impression that this post left me with, unused files are not removed, but are reduced in size (image -> single pixel, xml -> nearly empty). I understand they are reduced in size now and not removed outright. @androiddeveloper The concept "shrink resources" might be interpreted as "removing unused resources" or "reducing the size of resources" -- with that name they can change/improve behavior without changing the name. – neuralmer Sep 06 '16 at 21:32
  • need help, when i enable "minifyEnabled true" my notification not received and "minifyEnabled false" my notification received , i can't understand , why this happen ? – Hitesh Tarbundiya May 13 '19 at 13:03
33

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.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
Florin
  • 631
  • 6
  • 9
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/10411866) – Yossarian Dec 02 '15 at 10:06
  • Budius' answer is good, I would rather provide the link as the proof of his words in comments, but unfortunately I don't have enough reputation to post the comment under his answer. I will modify my answer to give some details. – Florin Dec 02 '15 at 10:28
4
  • 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
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64