[android] Remove all unused resources from an android project

I want to remove all unused layouts, strings, drawables, colors, etc from my Android res directory. Are there any tools that will give me a list of files and I can remove from my repository and elements within specifics files (e.g. unused string entries) that are no longer used?

This question is related to android android-resources

The answer is


In Android Studio 2.0 and above in menu select Refactor-->click on Remove Unused Resources...

(or)

shortcut also available

Press Ctlr+Alt+Shift+i one dialog box will apper, then type unused , you will find number of options select and remove unused resources


To check string.xml.

It's easy (at least in my version of Eclipse)

In Eclipse for Android (I have version v22.6.2-1085508)

  • Left click on the project name in "Package explorer"
  • Select "Android Tools".
  • Select "Run Lint: Check for common Errors".

Now when you open strings.xml, you will see that unused string are highlighted.

You can fix other potential issues.


In Android Studio,

Analyze -> Run Inspection by Name

(Shortcut For Mac shift+command+option+i , Ctrl + Shift + A on Windows/Linux).

And type 'unused resources'.

This way you can remove unused resources, variables ,symbols ,localization ,libraries ..etc.

Select the desired inspection from the list, then select inspection scope ->OK enter image description here


We open source a tool that removes all the unused resources in your android project based on the lint output. It can be found here: https://github.com/KeepSafe/android-resource-remover


On Windows: Press Ctlr+Alt+Shift+i one dialog box will appear, then type unused, you will find number of options select and remove unused resources


After you run Lint in Android Studio and find all the unused resources, you can click on one of them from the Inspection tab. It provides some detail about the issue and a few options to fix it. One of them is Remove All Unused Resources. Selecting that option deletes all the unused resources.


There really excellent answers in here suggesting good tools

But if you are intending to remove png-drawables (or other image files), you should also consider moving all the drawable-xxxx folders out of your project into a temporary folder, then do a rebuild all, and take a look at the build message list which will tell you which ones are missing.

This can be specially useful if you want to get an overview of which resources you are effectively using and maybe replace them with an icon font or svg resources, possibly with the help of the Android Iconics library.


Since Support for the ADT in Eclipse has ended, we have to use Android Studio.

In Android Studio 2.0+ use Refactor > Remove Unused Resources...

enter image description here


Android Assets Viewer is designed to help visually inspect the graphics packed (as drawables) within your .apk (you can also just upload a zip file of your res folder if you prefer):

http://www.cellebellum.net/AndroidAssetsViewer/

So for removing unused resources, this might work well in tandem with the Lint checks to help make sure everything that should be there is. It also helps you identify a few other potential problems with your drawables: wrong bucket, wrong graphic, etc.


When we define shrink resources true that time we can also define which resources we wanna keep and which don't I have added xml file in res/raw folder named keep.xml

before going further generate a single signed build and check in apk analyser tool which will show drawable-xhdpi-v4 has messenger_button_send_round_shadow.png which i want to remove for this test

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
       tools:shrinkMode="strict"
       tools:discard="@drawable/com_facebook_button_icon_blue.png,
       @drawable/com_facebook_button_icon_white.png,
       @drawable/com_facebook_button_like_icon_selected.png,
       @drawable/messenger_button_send_round_shadow.png,
       @drawable/messenger_*"  />

by doing messenger_* all files starting from name messenger in drawable folder will be removed or other way around is i have defines specific file to be removed

so that way you can remove files from library it self you can also remove layouts by @layout/layout name if that drawable has been used by layout and so....


Attention Android Wear developers: "Remove Unused Resources" will delete the xml file where you declare the capability name (res/values/wear.xml) and the phone won't be able to connect to the watch. I spent hours trying to figure out this bug in my app.


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.

To enable this add the line shrinkResources true in your gradle file.

   android {
        ...

        buildTypes {
            release {
                minifyEnabled true //Important step
                shrinkResources true
            }
   }
}

Check the official documentation here,

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


shift double click on Windows then type "unused", you will find an option Remove unused Resources,
also

 android {
        buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
            }
   }
}

when you set these settings on, AS will automatically remove unused resources.


Beware if you are using multiple flavours when running lint. Lint may give false unused resources depending on the flavour you have selected.


Since ADT 16 you can use Android Lint. It is really amazing tool.

Android Lint is a new tool for ADT 16 (and Tools 16) which scans Android project sources for potential bugs.

Here are some examples of the types of errors that it looks for:

  • Missing translations (and unused translations)
  • Layout performance problems (all the issues the old layoutopt tool used to find, and more)
  • Unused resources
  • Inconsistent array sizes (when arrays are defined in multiple configurations)
  • Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
  • Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
  • Usability problems (like not specifying an input type on a text field)
  • Manifest errors and many more.

However, it has some issues (don't know if they're already fixed) and if you want to delete hundreds of supposedly unused resources I'd recommend to manually compile project several times during resource removing to be sure that Lint didn't remove something needed.


Maybe useful Andround Unused Resources is a Java application that will scan your project for unused resources. Unused resources needlessly take up space, increase the build time, and clutter the IDE's autocomplete list.

To use it, ensure your working directory is the root of your Android project, and run:

java -jar AndroidUnusedResources.jar

https://code.google.com/p/android-unused-resources/