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

后端 未结 9 1038
傲寒
傲寒 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: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
    ...
    

提交回复
热议问题