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

时间:2023-01-17 14:21:07

I have a huge Android project with many strings declared in strings.xml. I wanted to remove unused strings in strings.xml.

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

Is there any easy way to do so?

有什么简单的办法吗?

10 个解决方案

#1


172  

On Android Studio:

在Android上工作室:

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

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

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

检查文件掩码(s)复选框并设置字符串。文本字段中的xml。

#2


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:

有了adt16,你可以做的尽可能简单。更新到adt16并使用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.

#3


18  

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

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

Solution:

解决方案:

  1. Open strings.xml
  2. 打开strings.xml
  3. Secondary click
  4. 二次点击
  5. Refactor --> Remove Unused Resources
  6. 重构——>删除未使用的资源

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

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

#4


9  

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

这是另一个相当简单的解决方案。在Android Studio菜单中,转到

Refactor > Remove Unused Resources....

Refactor >删除未使用的资源....

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

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

单击Preview查看哪些资源是未使用的,并有选择地删除它们。

#5


5  

Take a look at my question: Find out if resource is used

看看我的问题:看看是否使用了资源

As nobody had a real answer, I programmed a script to search for unused resources which includes strings, too.

由于没有人有真正的答案,我编写了一个脚本来搜索包含字符串的未使用资源。

Hope that helps you.

希望可以帮助你。

#6


3  

Refer the link : http://code.google.com/p/android-unused-resources/ . it has a tool AndroidUnusedResources.jar. run this and get your unused strings or any resourced removed.

请参考链接:http://code.google.com/p/android- unusresources/。它有一个工具AndroidUnusedResources.jar。运行这个并删除未使用的字符串或任何资源。

  • Deeps
  • 深处

#7


3  

To check string.xml.

检查string.xml。

It's easy (at least in my version of Eclipse)

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

In Eclipse for Android (I have version v22.6.2-1085508)

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

  • Right click on the project name in "Package explorer"
  • 右键单击“Package explorer”中的项目名称
  • Select "Android Tools".
  • 选择“Android工具”。
  • Select "Run Lint: Check for common Errors".
  • 选择“运行Lint:检查常见错误”。

Now when you open strings.xml, you will see that unused string are highlighted.

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

You can fix other potential issues.

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

#8


2  

In Android Studio Press

在Android工作室新闻

Ctlr+Alt+Shift+i

Ctlr + Alt + Shift +我

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

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

Thanks Happy coding :)

由于编码快乐:)

#9


-1  

For missing translation only:

仅供缺失的翻译:

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

使用InteliJ,点击您InteliJ的面板栏:“分析”>运行检查名称“>类型:不完全翻译”

#10


-2  

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

#1


172  

On Android Studio:

在Android上工作室:

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

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

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

检查文件掩码(s)复选框并设置字符串。文本字段中的xml。

#2


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:

有了adt16,你可以做的尽可能简单。更新到adt16并使用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.

#3


18  

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

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

Solution:

解决方案:

  1. Open strings.xml
  2. 打开strings.xml
  3. Secondary click
  4. 二次点击
  5. Refactor --> Remove Unused Resources
  6. 重构——>删除未使用的资源

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

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

#4


9  

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

这是另一个相当简单的解决方案。在Android Studio菜单中,转到

Refactor > Remove Unused Resources....

Refactor >删除未使用的资源....

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

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

单击Preview查看哪些资源是未使用的,并有选择地删除它们。

#5


5  

Take a look at my question: Find out if resource is used

看看我的问题:看看是否使用了资源

As nobody had a real answer, I programmed a script to search for unused resources which includes strings, too.

由于没有人有真正的答案,我编写了一个脚本来搜索包含字符串的未使用资源。

Hope that helps you.

希望可以帮助你。

#6


3  

Refer the link : http://code.google.com/p/android-unused-resources/ . it has a tool AndroidUnusedResources.jar. run this and get your unused strings or any resourced removed.

请参考链接:http://code.google.com/p/android- unusresources/。它有一个工具AndroidUnusedResources.jar。运行这个并删除未使用的字符串或任何资源。

  • Deeps
  • 深处

#7


3  

To check string.xml.

检查string.xml。

It's easy (at least in my version of Eclipse)

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

In Eclipse for Android (I have version v22.6.2-1085508)

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

  • Right click on the project name in "Package explorer"
  • 右键单击“Package explorer”中的项目名称
  • Select "Android Tools".
  • 选择“Android工具”。
  • Select "Run Lint: Check for common Errors".
  • 选择“运行Lint:检查常见错误”。

Now when you open strings.xml, you will see that unused string are highlighted.

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

You can fix other potential issues.

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

#8


2  

In Android Studio Press

在Android工作室新闻

Ctlr+Alt+Shift+i

Ctlr + Alt + Shift +我

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

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

Thanks Happy coding :)

由于编码快乐:)

#9


-1  

For missing translation only:

仅供缺失的翻译:

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

使用InteliJ,点击您InteliJ的面板栏:“分析”>运行检查名称“>类型:不完全翻译”

#10


-2  

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