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

9

35

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?

Lerner answered 9/3, 2015 at 8:13 Comment(3)
Menu -> Analyze -> Run Inspection by Name -> Unused resourcesNugent
@Nugent it's a nice way to find unused resources, is there any way to delete them automatically?Distorted
@Distorted perl is a nice optionNugent
I
23

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

Iveson answered 28/9, 2016 at 14:37 Comment(3)
How do you run it ?Pittance
Menu "Analyze" and "Inspect Code" !Iveson
How do you delete a single resource across multiple locale files?Vaden
B
34

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...
Bacteroid answered 19/3, 2015 at 15:39 Comment(0)
I
23

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

Iveson answered 28/9, 2016 at 14:37 Comment(3)
How do you run it ?Pittance
Menu "Analyze" and "Inspect Code" !Iveson
How do you delete a single resource across multiple locale files?Vaden
G
5

Until IDE support comes along, something along these lines will work:

find -name strings.xml|xargs -rd\\n  sed -ri '/"string_to_delete"/d'
Germano answered 20/5, 2015 at 22:15 Comment(3)
Awesome answer! It would be better if multi-lined string tags are also handled.Thickleaf
This doesn't seem to work. Error: xargs: illegal option -- rVaden
-r (aliased as --no-run-if-empty) is probably not available on your system's version of xargs. Maybe try the long form. According to the changelog, it looks like it's been available for about 27 years and I see it on my system's version, v4.7.0-git.Germano
A
4

In Android Studio 2.3 it's possible to remove all unused resources.

  • Open any *.xml in your res/values/ directory
  • Right click on any item's name
  • Refactor -> Remove Unused Resources...
Akron answered 23/2, 2017 at 22:50 Comment(1)
Be careful with this one. It deleted even resources that were still being used. :-(Houselights
V
3
  1. Menu -> Analyze -> Run Inspection by Name -> Unused resources
  2. From the results select all string resources that are unused.

    • Right-click highlighted rows and choose "Suppress with @SuppressLint(Java) or tools:ignore(XML)". This will add the attribute tools:ignore to all strings in all string files.
  3. Menu -> Find -> Replace in Path

    • Text to find: ^.*?tools:ignore="UnusedResources".*?\n
    • Tick regular expression box
    • Use Scope: Custom
      • Open custom scope editor and add pattern: file[app]:src/main/res//strings.xml
    • Find
    • Etc.
Vorfeld answered 14/7, 2015 at 5:27 Comment(0)
B
2

Unfortunately, You have to do it manually.

Check this answer to understand what exactly should you do to get rid of them using Eclipse

If you are using Android Studio find them in the whole application and also remove manually .. Check this answer

Blindworm answered 9/3, 2015 at 8:24 Comment(0)
I
2

Beware that the REMOVE UNUSED RESOURCES command cannot recognize a programmatically accessed resource as a used resource (such as getIdentifier(..) etc.). So, if you do access resources that way, it is highly risky to use that command!!

Inveigh answered 20/12, 2017 at 12:23 Comment(0)
L
0

In fact, Android Lint should report about the unused resources, but you can also try with this nice plugin.

Levant answered 9/3, 2015 at 8:16 Comment(0)
I
0

To automatically remove unnecessary resources of all types (strings, images, etc.), you must add a resource xml file (usually called res/raw/keep.xml) and add to it:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict"/>

However, the project build plugin will not be able to see resources accessed via reflection, so such resources should be labelled in tools:keep with a comma, for example:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict"
    tools:keep="@string/tracer_app_token,@string/tracer_mapping_uuid"/>

The application will crash if it tries to access removed resources. If you find that a library is responsible, add the required resources to `tools:keep' as a workaround, and notify their creators to add this to the publishing library files.

Interphase answered 8/5 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.