Home » Android » Is there any simple way to find out unused strings in Android project?

Is there any simple way to find out unused strings in Android project?

Posted by: admin March 10, 2020 Leave a comment

Questions:

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 way to do so?

How to&Answers:

On Android Studio:

Menu -> Analyze -> Run Inspection by Name -> Unused resources

Check File mask(s) checkbox and put strings.xml in the text field.

Answer:

Here is another solution that is fairly easy. In the Android Studio menu go to

Refactor > Remove Unused Resources….

enter image description here

Click Preview to see what the unused resources are and selectively remove them.

Answer:

In my case “Run Inspection by Name” didnt work, despite the fact I was using “Remove unused resources”.

Solution:

  1. Open strings.xml
  2. Secondary click
  3. Refactor –> Remove Unused Resources

I have no clue why “Remove Unused Resources” works one way but not the other.

Answer:

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.

Answer:

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)

  • Right 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.

Answer:

In Android Studio Press

Ctlr+Alt+Shift+i

Select -> Unused resources
It shows you unused unused strings and icons.

Thanks Happy coding 🙂

Answer:

This is how I did it with Android 3.3.

Check in any unsaved changes to your repository.

  • Right click your App’s module -> Refactor -> Remove Unused Resources -> Preview
  • In the Refactoring Preview, collapse both the views (‘Items to be deleted’ & ‘Unused Resource Declarations’)
  • Right click ‘Items to be deleted’ -> Exclude
  • Right click ‘Unused Resource Declarations’ -> Exclude
  • Now expand ‘Unused Resource Declarations’ and under that locate your app specific strings.xml (there would be multiple strings.xmls)
  • Right click that strings.xml -> Include
  • Do Refactor! All unused strings from the xml file are deleted!

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!

Answer:

Run this script from root of your project.

for resourcefile in `find res/values/*.xml`; do
  for stringname in `grep '.*//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/^\(.*\)\....$//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/^\(.*\)\....$//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
...

Answer:

For missing translation only:

Using InteliJ, click on the panel bar of your InteliJ: “Analyze” > “Run Inspection by Name” > Type in: Incomplete translation