Removing Unused Resources

Some resources, like strings, are each fairly tiny. Some resources, like layouts, may be comparable in size to a source file. And some resources, like bitmaps, can be fairly large.

Ideally, building the app would get rid of any resources that you are not using. As with shrinking code, this should include both resources that you added to your project and resources that are being added by libraries.

And, as with code shrinking, removing unused resources is not automatic, but it is relatively easy to do.

Remove Manually

For your own project resources, you can remove them manually. Choose “Refactor” > “Remove Unused Resources” from the Android Studio main menu. This will display a dialog of options:

Android Studio Remove Unused Resources Dialog
Android Studio Remove Unused Resources Dialog

The safest thing is to leave the checkbox unchecked, then click the “Preview” button. This will show you what changes will be made, before they are made:

Android Studio Remove Unused Resources Preview
Android Studio Remove Unused Resources Preview

Here, it shows three resources that are unused: two drawables and a string.

Clicking “Do Refactor” will then remove those resources.

Remove Automatically

If you opted into code shrinking via minifyEnabled true, you can also opt into automatic resource shrinking via shrinkResources true:

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

This works automatically, affecting your APK but not your project source code. It also removes library-supplied resources, whereas “Remove Unused Resources” does not. On the other hand, it needs to keep removing those resources on every build — “Remove Unused Resources” can improve your build speed a bit.

Whitelist Resource Sets

Libraries may supply resources that your app is unlikely to ever use.

For example, Jetpack libraries that have string resources often will supply string resource translations for every language, as the Jetpack developers do not know what languages your app will support. By default, the build tools also do not know what languages your app will support, so the build tools will package all translations. You may know what languages your app will support, in terms of the string translations that you intend to provide. By teaching the build tools about that language list, the build tools can strip out translations in other languages that your app is not going to use.

To do this, in the defaultConfig closure of your module’s build.gradle file, you can list language prefixes in a resConfigs list:

android {
  defaultConfig {
    // other cool stuff goes here

    resConfigs "en", "es", "zh"
  }
}

Here, we indicate that we want the app to contain English, Spanish, and Chinese resources. Resources targeting other languages will be removed from the APK automatically by the build tools.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.