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?
17 Answers
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
-
64
-
5@MikeMilla Right-click on the files you wish to delete and click on the option that says "Apply Lint Fixes". To delete multiple files in one go, select them. May 12, 2016 at 8:45
-
32Be advised that using this approach will also remove any resource that was used programmatically. Jan 25, 2017 at 5:41
-
36
-
8With AS 3.0, this feature remove some used layout file. Better make a backup or git commit before trying this.– StoneLamJan 30, 2018 at 7:15
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.
-
12I wouldn't rely to heavily on what Lint is proposing as being unused resources especially if you do a lot of resource lookups in your code instead of in xml. Lint will in that case find a lot of resources not being used while they actually are.– MrJreMar 7, 2012 at 12:40
-
63In Android Studio, you can use
Analyze > Inspect Code...
to find a number of things, including Unused Declarations and Methods. Oct 12, 2013 at 22:27 -
8An example, if you want to use the command line:
lint --check UnusedResources <project-path>
– espinchiJan 23, 2014 at 23:23 -
41
-
1Is there a way to delete all the
Extra Translations
together/automatically? Apr 16, 2015 at 7:37
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...
-
4
-
1Do not forget to Review in the next step, otherwise, some used files are also deleted. May 4, 2020 at 5:08
-
1Please take a backup of the whole project or just push to version control system before doing refactoring. I lost many drawables used, I was lucky to revert from the backup May 6, 2021 at 5:34
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
Beware if you are using multiple flavours when running lint. Lint may give false unused resources depending on the flavour you have selected.
-
-
1Until now, I haven't found out a way of doing it. Except running lint for each flavour and then marking the items unused on both flavours. Feb 27, 2019 at 14:19
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
-
how i use this to remove unused resource from my android studio project.– YaminiAug 5, 2016 at 10:56
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
1--> In
Android Studio Menu > Refactor > Remove Unused Resources
As Shown in Image Below:
**
OR
**
2--> Second Way
-> Ctrl Alt Shift i and type "unused resources" (without quotes) in Windows
-> use ⌘ Option Shift i for mac
or
-> cmd + Alt + Shift for mac
-
I added a random named string resource to the existing strings.xml file, and it didn't identify it as unused, so this doesn't work for that. Sep 25, 2022 at 3:35
-
1
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
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.
-
-
1Correct. In general, 'Remove Unused Resources' should not be recommended at all since it might create lots of untraceable bugs.– steliosfOct 1, 2017 at 15:25
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.
-
+1 nice solution, buddy. It allows to run lint for selected checks as well - exactly what I needed .... (p.s. almost the same with Android Studio: right click in Project Explorer, select Analyse, select Inspect Code or Run Inspection by Name) :D– TrinimonMar 25, 2015 at 8:03
-
This does not help much in removing those unused strings. Do you suggest to remove them manually as before?– StanJun 22, 2015 at 8:20
-
"Lint" is really an excellent way to "cleanup your project". You still have to remove them manually, but they are highlighted and you immediately see that they are not used. It's a few minutes work for a medium size project. :) Jun 22, 2015 at 14:56
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
-
2where ever I put it it keeps saying that is not the correct project root– kingstonNov 27, 2015 at 11:48
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....
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.
-
this is time consuming, but I agree this is too, a solution. I am doing this right now– DikaJan 24, 2018 at 14:58
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.
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.
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.