有没有什么简单的方法可以找出 Android 项目中未使用的字符串?

我有一个巨大的 Android 项目与许多字符串声明在 strings.xml。我想删除未使用的字符串在 strings.xml

有什么简单的方法吗?

39196 次浏览

从项目的根运行此脚本。

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

它给了我这样的输出:

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

使用 ADT 16,您可以尽可能简单地做到这一点。更新到 ADT 16并使用 Android Lint。这真是个神奇的工具。它可以找到 所有未使用的资源(不仅仅是字符串)和更多的资源。来自其官方网站:

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.

检查 string.xml。

这很容易(至少在我的 Eclipse 版本中是这样)

在 Eclipse for Android 中(我有版本 v22.6.2-1085508)

  • 右键单击“ PackageExplorer”中的项目名称
  • 选择「 Android 工具」。
  • 选择“ Run Lint: Check for Common Error”。

现在,当您打开 strings.xml 时,您将看到未使用的字符串被突出显示。

你可以解决其他潜在的问题。

安卓工作室台:

菜单 -> 分析-> 按名称运行检查-> < em > 未使用的资源

选中 文件掩码复选框并将 strings.xml放在文本字段中。

仅供遗漏翻译:

使用 InteliJ,单击 InteliJ 的面板栏: “分析”> “按名称运行检查”> 输入: 不完整的翻译

在我的例子中,“按名称运行检查”不起作用,尽管事实上我使用的是“删除未使用的资源”。

解决方案:

  1. 打开 strings.xml
  2. 二次点击
  3. 重构—— > 删除未使用的资源

我不知道为什么“删除未使用的资源”以一种方式工作,而不是其他方式。

在 Android Studio Press 中

Alt + Shift + i

选择-> 未使用的资源
它显示未使用的字符串和图标。

谢谢,编码愉快:)

下面是另一个相当简单的解决方案

重构 > 删除未使用的资源...

enter image description here

单击 预览查看未使用的资源是什么,并有选择地删除它们。

这就是我用 Android 3.3做到的。

签入对存储库的任何未保存的更改。

  • 右键单击应用程序的模块-> 重构-> 删除未使用的资源-> 预览
  • 在“重构预览”中,同时折叠视图(“要删除的项”和“未使用的资源声明”)
  • 右键单击“要删除的项目”-> 排除
  • 右键单击“未使用的资源声明”-> 排除
  • 现在展开“ Unused Resource Declarations”并在其下定位应用程序特定的 strings.xml (将有多个 strings.xml)
  • 右键单击 strings.xml-> Include
  • 执行重构! 从 xml 文件中删除所有未使用的字符串!

注意: 尝试构建该项目。如果编译失败,这些 strings.xml 很可能是从某些布局/菜单 xml 中引用的,而这些布局/菜单 xml 本身是未使用的。 因此,这些布局 xml 也可以手动删除!

构建并运行。测试!