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

后端 未结 9 1026
傲寒
傲寒 2021-01-30 03:03

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

相关标签:
9条回答
  • 2021-01-30 03:11

    On Android Studio:

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

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

    0 讨论(0)
  • 2021-01-30 03:11

    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!

    0 讨论(0)
  • 2021-01-30 03:22

    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.
    
    0 讨论(0)
  • 2021-01-30 03:29

    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.

    0 讨论(0)
  • 2021-01-30 03:29

    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
    ...
    
    0 讨论(0)
  • 2021-01-30 03:29

    For missing translation only:

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

    0 讨论(0)
提交回复
热议问题