14

I'm setting shrinkResources to true as follows:

releasepro {
    minifyEnabled true
    shrinkResources true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    applicationIdSuffix ".pro"
}

But this is removing only a few unused resources. I am aware that gradle does some guessing and leaves resources that is is not sure are unused. Is there a way to remove all unused resources 100% ?

I read about setting the shrinkMode to strict in another thread. Will that help? I could not try it as I could not figure where to set it. Looked for examples and documents but unfortunately could not get to the right page.

Is there a way to ensure 100% removal of unused resources?

Ziem
  • 6,579
  • 8
  • 53
  • 86
Viral Patel
  • 32,418
  • 18
  • 82
  • 110

2 Answers2

5

R.raw.Keep (xml file)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict" />

Dont forget to Reference this Resource from .java source file. For more details see this

if you are worried about the size of your apk file then its good practice to shrink image files as well. That is convert RGB channel to Indexed channel,this can save up to 50% more space.

Use this site to Shrink your images media4x.com

Brijesh Masrani
  • 1,439
  • 3
  • 16
  • 27
Dilroop Singh
  • 544
  • 6
  • 16
  • I added keep.xml under res\raw\ folder. Got the error: **URI is not registered**. Fixed this by adding `http://schemas.android.com/tools` to schemas and DTDs under settings. Now I'm getting a new error on the resource tag in keep.xml: **Cannot find the declaration of element 'resources'**. What am i doing wrong? – Viral Patel Dec 17 '15 at 16:23
  • ok turns out if i just ignore the error shown about not being able to find the declaration of element 'resource' it still works as expected. But still curious why the error shows and how to remove it. I don't like the red highlighting for no reason. Also, there is no need to reference the keep resource from a .java file. simply adding `tools:keep="@raw/*"` to keep.xml did the trick. And i feel this is more meaningful that fooling to compiler with a false reference. Correct me if i am wrong. – Viral Patel Dec 17 '15 at 17:26
  • i was not shown any error , creating fake reference make sure it work on all devices . Also if `tools:keep="@raw/*"` did the trick its great . Make Sure to check on few other devices as well. Best of Luck – Dilroop Singh Dec 18 '15 at 04:04
  • 1
    what do you mean by 'Dont forget to Reference this Resource from .java source file.' – Anthony Jan 20 '16 at 15:20
  • R.raw.keep would never be used in project if left un- referenced, Referencing it ensure that it must be kept and not removed to shrunken the project. – Dilroop Singh Jan 20 '16 at 16:29
  • 1
    OK Dilroop. Can you explain how to reference it? – JaydeepW Oct 03 '17 at 06:42
  • In addition if you are using Android sturio 3.2+. Disable R8, because it seems to igonore keep.xml file. Add: android.enableR8=false to gradle.properties file – Oleksandr Albul Nov 09 '18 at 09:18
1

To turn off the safety checks, set the shrinkMode to "strict" as in the following keep.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict" />

From: http://tools.android.com/tech-docs/new-build-system/resource-shrinking

TheTool
  • 309
  • 2
  • 12