Remove All Unused Resources from an Android Project

Remove all unused resources from an android project

You can easily search for unused resources from Android Studio. Just press CtrlAltShifti and type "unused resources" (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE).

OR

In Android Studio Menu > Refactor > Remove Unused Resources...

Select the resources you want to remove. You can exclude resources you want to keep by right-clicking on the resource item.

Use Do Refactor to remove all Resources at once.

Update: use OptionShifti for mac

How to clean up unused resource files in Android Studio

You can able to remove unused resources by following way.

  1. Analyze > Inspect Code and find Unused Declarations and Methods.

  2. Android Studio -> Menu -> Refactor -> Remove Unused Resources.

Remove unused resources using Android Studio?

The Gradle build system for Android supports Resource Shrinking : the automatic removal of resources that are unused, at build time, in the packaged app. In addition to removing resources in your project that are not actually needed at runtime, this also removes resources from libraries you are depending on if they are not actually needed by your application.

For example, your application is using Google Play Services to for example access Google Drive functionality, and you are not currently using Google Sign In, then this would remove the various drawable assets for the Sign In buttons.

Note: Resource Shrinking only works in conjunction with code shrinking (such as ProGuard). That's how it can remove unused resources from libraries; normally, all resources in a library are used, and it is only when we remove unused code that it becomes apparent which resources are referenced from the remaining code.

To enable resource shrinking, update your build type as follows:

android {
...

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

And google recently launched Android Studio 2.0 officially, Now they are giving an option in the IDE itself.

Right click on app --> Refactor --> Remove Unused Resources

It will prompt

Sample Image

Check the box prior confirm action so that you can get rid of unused @id declarations too.

  • In terms of APK optimization consider Selecting a Format fact as well.
  • Use WebP Images provide better compression than either JPEG or PNG. Lossy WebP images are supported in Android 4.0 (API level 14) and higher, and lossless and transparent WebP images are supported in Android 4.3 (API level 18) and higher.

Remove unused resources from Android app on compile time

ProGuard (or the new Jack compiler) only shrink and obfuscate bytecode. The resource shrinking tool subsequently removes or replaces unused resource files.

ProGuard's commercial extension DexGuard shrinks, optimizes, and obfuscates bytecode, the Android manifest, resources, resource files, asset files, and native libraries. As far as I'm aware, it's the only tool that analyzes and optimizes all contents at the same time. For instance, it indeed removes unused classes and corresponding unused resources and resource files.

[we develop ProGuard and DexGuard at GuardSquare]

Android studio - custom 'Remove unused resources'

I ended up writing a script that parses the custom file and creates an enum that holds a reference to the strings that should be kept. Generated enum looks like this.

enum class StringsToKeep(@StringRes stringRes: Int) {
STRING_KEY(R.string.string_key),
STRING_KEY_TWO(R.string.string_two),
}

How to delete an unused string resource for all configurations in Android Studio?

It is now possible inside Android Studio.
After Lint checks you see an option on the right Remove All Unused Resources!

Sample Image

To Delete a single string resource across all locale files, you can use the "Translation Editor".
1. Right click on the res directory to open the translation editor.
2. Select "Show All Keys" selector, and choose "Filter by Text". Supply the name of the resource that you want to delete.
3. Select the resource, and click on the "-" button



Related Topics



Leave a reply



Submit