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

use*_*956 142 android

我有一个巨大的Android项目,其中声明了许多字符串strings.xml.我想删除未使用的字符串strings.xml.

有没有简单的方法呢?

Ode*_*ner 221

Android Studio上:

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

选中File mask(s)复选框并strings.xml输入文本字段.

  • 哇.找到479件物品.任何一个单击方式将它们全部删除? (4认同)
  • 这个没有按预期工作.我试过添加未使用的字符串并检查,但它无法识别它 (4认同)
  • 然后你可以定义`自定义范围`,选择`生产类` - >`app` - >`values`来查找未使用的字符串,颜色...... (2认同)
  • 使用SDK Tools 26.0.2时,它不会找到我未使用的字符串,而“查找用法”不会显示它们的任何用法(对于实际使用的字符串也是如此)。 (2认同)
  • 这似乎坏了,它认为我的一堆字符串未使用,但快速“查找用法”发现了用途。当心。 (2认同)

cut*_*iko 26

在我的情况下,"按名称运行检查"没有工作,尽管事实上我正在使用"删除未使用的资源".

解:

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

我不知道为什么"删除未使用的资源"以一种方式工作而不是另一种工作方式.

  • 您也可以在项目资源管理器中执行此操作,选择所需的模块,然后右键单击并重构>删除未使用的资源,删除任何类型的资源,例如未使用的可绘制对象 (2认同)

Sur*_*gch 26

这是另一种相当容易的解决方案.在Android Studio菜单中转到

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

在此输入图像描述

单击" 预览"以查看未使用的资源,并有选择地删除它们.

  • 这消除了实际使用的大量资源。必须手动取消删除很多东西 (3认同)
  • @stoefln,如果单击“预览”,您可以手动选择要删除和不删除的内容。 (2认同)
  • 请注意“也删除未使用的@id 声明”。它删除了我的代码实际使用的许多 id(Kotlin Android 扩展插件 - 这将允许从活动、片段和视图中恢复视图) (2认同)

And*_*yeu 22

使用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.
Run Code Online (Sandbox Code Playgroud)

  • 从ADT 20.0.2起,Android Lint找不到未使用的字符串,也没有声称.我只是通过添加一个具有完全随机id的字符串然后运行Lint来测试它.我无法在上面投票回复,因为我没有足够的声誉:(. (10认同)
  • 在当前版本(ADT 21.1)中它运行良好! (4认同)

AAP*_*AAP 5

这就是我在 Android 3.3 上的做法。

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

  • 右键单击您的应用程序模块 -> 重构 -> 删除未使用的资源 -> 预览
  • 在重构预览中,折叠两个视图(“要删除的项目”和“未使用的资源声明”)
  • 右键单击“要删除的项目”-> 排除
  • 右键单击“未使用的资源声明”-> 排除
  • 现在展开“未使用的资源声明”并在其下找到您的应用程序特定的strings.xml(会有多个strings.xmls)
  • 右键单击该 strings.xml -> 包含
  • 进行重构!xml 文件中所有未使用的字符串都将被删除!

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

构建并运行。测试!


Car*_*uin -1

从项目的根目录运行此脚本。

对于“find res/values/*.xml”中的资源文件;做
  对于 `grep '.*/\1/g'` 中的字符串名称;做
    count1=`grep -rc "R.string.${stringname}" src | egrep -v ':0$' | 厕所-l`
    count2=`grep -rc "@string/${stringname}" res/layout | count2=`grep -rc "@string/${stringname}" res/layout | egrep -v ':0$' | 厕所-l`
    count3=`grep -rc "@string/${stringname}" res/menu | count3=`grep -rc "@string/${stringname}" res/menu | egrep -v ':0$' | 厕所-l`
    count4=`grep -rc "@string/${stringname}" AndroidManifest.xml | egrep -v '^0$' | 厕所-l`
    count5=`grep -rc "@string/${stringname}" res/xml | count5=`grep -rc "@string/${stringname}" res/xml | egrep -v ':0$' | 厕所-l`
    如果 [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; 然后
      echo $资源文件:$字符串名称
    菲
  完毕
完毕

对于 `find res/drawable* -type f -name '*.???'` 中的资源名称;做
  资源=`echo $资源名称| xargs 基本名称 | sed "s/^\(.*\)\....$/\1/g"`
  count1=`grep -rc "R\.drawable\.${资源}" src | egrep -v ':0$' | 厕所-l`
  count2=`grep -rc "@drawable/${resource}" res/layout | count2=`grep -rc "@drawable/${resource}" res/layout | egrep -v ':0$' | 厕所-l`
  count3=`grep -rc "@drawable/${resource}" res/drawable*/*.xml | egrep -v ':0$' | 厕所-l`
  count4=`grep -rc "@drawable/${resource}" res/menu | count4=`grep -rc "@drawable/${resource}" res/menu | egrep -v ':0$' | 厕所-l`
  count5=`grep -rc "@drawable/${resource}" AndroidManifest.xml | egrep -v '^0$' | 厕所-l`
  如果 [ $count1 -eq 0 -a $count2 -eq 0 -a $count3 -eq 0 -a $count4 -eq 0 -a $count5 -eq 0 ]; 然后
      echo $资源名
  菲
完毕

对于“find res/layout/*.xml”中的资源名称;做
  资源=`echo $资源名称| xargs 基本名称 | sed "s/^\(.*\)\....$/\1/g"`
  count1=`grep -rc "R\.layout\.${资源}" src | egrep -v ':0$' | 厕所-l`
  如果 [ $count1 -eq 0 ]; 然后
      echo $资源名
  菲
完毕

它给了我这样的输出:

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