Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've found an unused string resource, like:

<string name="obsoletestring">my name is null!</string> 

However it is in tens of files, of different languages, in different strings.xml files in values, values-af, values-be, etc folders.

I must have missed something not to know any way to do this in Android Studio other than modifying it by hand one by one.

tl;dr How to delete an unused string resource for all configurations?

like image 427
Randy Sugianto 'Yuku' Avatar asked Mar 09 '15 08:03

Randy Sugianto 'Yuku'


People also ask

How do I find unused resources in Android?

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

What are string resources in Android?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.


2 Answers

To identify all unused resources:

  • Open Menu > Analyze > Run Inspection by Name...
  • Select "Unused Resources"
  • Make sure Whole project is checked, then press OK.
  • Look through the list. You can always rerun the Inspection with the Rerun button.

There is no really easy way in Android Studio (v 1.0) to remove a resource string for all locales. However, you can search and replace in files. Fortunately, the translation files use only a single line in most cases so this works pretty well.

In Android Studio:

  • Start with no changes pending in your source control (makes it way easier to check and back out if this goes wrong).
  • Open Menu > Edit > Find > Replace in Path...
  • In Text to find: .*name="obsoletestring".*\n
  • In Replace with: (empty)
  • Check Regular expression
  • Press the Find button.
  • Inspect the results. If okay, press the "All files" button.
  • You will probably have to remove the comment in res/values/strings.xml manually.
  • Make sure your project still builds.
  • Take a look at the diff of your project in your source control to ensure the change is really what you intended...
like image 111
mbells Avatar answered Sep 22 '22 20:09

mbells


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

enter image description here

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

like image 29
chrisonline Avatar answered Sep 23 '22 20:09

chrisonline