Reputation: 73453
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?
Upvotes: 35
Views: 22963
Reputation: 543
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.
Upvotes: 0
Reputation: 613
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!!
Upvotes: 2
Reputation: 7109
It is now possible inside Android Studio. After Lint checks you see an option on the right Remove All Unused Resources!
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
Upvotes: 23
Reputation: 294
In Android Studio 2.3 it's possible to remove all unused resources.
Refactor -> Remove Unused Resources...
Upvotes: 6
Reputation: 1872
From the results select all string resources that are unused.
Menu -> Find -> Replace in Path
Upvotes: 3
Reputation: 2637
Until IDE support comes along, something along these lines will work:
find -name strings.xml|xargs -rd\\n sed -ri '/"string_to_delete"/d'
Upvotes: 5
Reputation: 3858
To identify all unused resources:
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:
.*name="obsoletestring".*\n
Upvotes: 34
Reputation: 10009
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
Upvotes: 2