I have a huge Android project with many strings declared in strings.xml
. I wanted to remove unused strings in strings.xml
.
Is there any easy wa
On Android Studio:
Menu -> Analyze -> Run Inspection by Name -> Unused resources
Check File mask(s) checkbox and put strings.xml
in the text field.
This is how I did it with Android 3.3.
Check in any unsaved changes to your repository.
Note: Try building the project. If compilation fails, its most likely that these strings.xml is being referred from some layout/menu xmls, which themselves are unused. So those layout xmls can also be deleted manually!
Build and run. Test!
With ADT 16 you can do it as simple as possible. Update to ADT 16 and use Android Lint. It is really amazing tool. It can find all unused resources (not only strings) and many more. From its official site:
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.
Here is another solution that is fairly easy. In the Android Studio menu go to
Refactor > Remove Unused Resources....
Click Preview to see what the unused resources are and selectively remove them.
Run this script from root of your project.
for resourcefile in `find res/values/*.xml`; do for stringname in `grep '.*/\1/g'`; do count1=`grep -rc "R.string.${stringname}" src | egrep -v ':0$' | wc -l` count2=`grep -rc "@string/${stringname}" res/layout | egrep -v ':0$' | wc -l` count3=`grep -rc "@string/${stringname}" res/menu | egrep -v ':0$' | wc -l` count4=`grep -rc "@string/${stringname}" AndroidManifest.xml | egrep -v '^0$' | wc -l` count5=`grep -rc "@string/${stringname}" res/xml | egrep -v ':0$' | wc -l` if [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; then echo $resourcefile : $stringname fi done done for resourcename in `find res/drawable* -type f -name '*.???'`; do resource=`echo $resourcename | xargs basename | sed "s/^\(.*\)\....$/\1/g"` count1=`grep -rc "R\.drawable\.${resource}" src | egrep -v ':0$' | wc -l` count2=`grep -rc "@drawable/${resource}" res/layout | egrep -v ':0$' | wc -l` count3=`grep -rc "@drawable/${resource}" res/drawable*/*.xml | egrep -v ':0$' | wc -l` count4=`grep -rc "@drawable/${resource}" res/menu | egrep -v ':0$' | wc -l` count5=`grep -rc "@drawable/${resource}" AndroidManifest.xml | egrep -v '^0$' | wc -l` if [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; then echo $resourcename fi done for resourcename in `find res/layout/*.xml`; do resource=`echo $resourcename | xargs basename | sed "s/^\(.*\)\....$/\1/g"` count1=`grep -rc "R\.layout\.${resource}" src | egrep -v ':0$' | wc -l` if [ $count1 -eq 0 ]; then echo $resourcename fi done
It gives me this kind of output:
res/values/activity_strings.xml : activity_more res/values/activity_strings.xml : activity_as_reply_to res/values/db_strings.xml : sql_backlog_count res/values/db_strings.xml : sql_backlog_update_last_resend ...
For missing translation only:
Using InteliJ, click on the panel bar of your InteliJ: "Analyze" > "Run Inspection by Name" > Type in: Incomplete translation