Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"minifyEnabled" vs "shrinkResources" - what's the difference? and how to get the saved space?

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,"
like image 331
android developer Avatar asked Jun 12 '15 10:06

android developer


People also ask

What does shrinkResources do?

Resource shrinking: removes unused resources from your packaged app, including unused resources in your app's library dependencies. It works in conjunction with code shrinking such that once unused code has been removed, any resources no longer referenced can be safely removed as well.

What is the use of minifyEnabled?

This explains it. So minifyEnabled removes dead code but does not obfuscate or optimize. Seems to directly contradict this answer which is saying that minify does obfuscate a bit stackoverflow.com/questions/17290023/… updated link to the documentation : developer.android.com/studio/build/…

What is minifyEnabled Android?

minify is an Android tool that will decrease the size of your application when you go to build it. It's extremely useful as it means smaller apk files! It detects any code or libraries that aren't being used and ignores them from your final apk.

What is R8 Android studio?

R8 is an app shrinking tool that is used to reduce the size of your application. This tool present in Android Studio works with the rules of Proguard. R8 will convert your app's code into optimized Dalvik code.


2 Answers

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.

like image 151
Budius Avatar answered Sep 19 '22 12:09

Budius


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.

like image 23
Florin Avatar answered Sep 19 '22 12:09

Florin