2010-09-21 35 views
28

我正在寻找一种有效的方法来确定在Java或XML文件中是否使用资源(通常是可绘制的)。找出是否使用资源

问题是,在我目前的项目中,drawable常常被改变,现在我有一些drawable,它可能永远不会被使用。

是否有一种工具/方法可以在整个项目中找到那些未使用的drawable而不搜索每个文件名?

+2

这将是不错的那种:)脚本的 – Macarse 2010-09-21 12:21:34

+0

好看起来像我必须写一个:) – WarrenFaith 2010-09-21 12:33:44

+0

@MaxUsanin感谢滥发我的问题。你的问题与我的问题有什么关系?这是垃圾邮件,我标记了它。 – WarrenFaith 2012-12-06 15:19:35

回答

31

我写了一个基于Python的解决这个问题的一个工具。由于这不是直接分享的地方,我创建了一个现在离线的项目页面。

UPDATE:
由于皮棉开发已停止可以做同样的,并已包含在Android SDK。

+0

嗯是我还是工具不工作?我提供了一个路径到我的android项目的根文件夹。但现在它说没有文件/path/to/project/root/gen/R.java。这是正确的,因为R.java文件在gen/de/somepackage/anotherpackage/clientpackage /是否有解决方案? – Janusz 2010-11-11 11:50:30

+0

我想最好在项目页面创建一张票。我很活跃,所以我会尽快回复:) – WarrenFaith 2010-11-11 13:19:19

+0

无法正常工作,必须在“parseR(rclass)”硬编码我的目录,它在C:\ Workspace \ Project_dir \ gen \ CVS \中寻找R.java,并且我给了dir选项'-i C:\ Workspace \ Project_dir'。所以它需要更多的工作,但我至少有工作。 – JPM 2011-04-07 10:51:43

17

我刚才写的bash脚本只是为了好玩:

PROJECT="/path/to/the/project" 
for file in $(ls $PROJECT/res/drawable -l | awk '{ print $8}' | sed 's/\..\+//g'); do count=0; for SRCFILE in `find $PROJECT -name "*.xml" -print 2> /dev/null`; do let "count+=$(grep -c @drawable/$file $SRCFILE)"; done; for SRCFILE in `find $PROJECT -name "*.java" -print 2> /dev/null`; do let "count+=$(grep -c R.drawable.$file $SRCFILE)"; done; if [ $count -lt 1 ]; then echo -e "\e[0;31m$file\e[0m not used"; else echo -e "\e[0;32m$file\e[0m used"; fi; done; 

它工作得很好,虽然我一个bash新手,所以它可大大地改善:

alt text

它搜索仅绘制资源(在XML文件上为@drawable/name,在Java文件上为R.drawable.name)。

顺便说一下,我不知道boxscorecalendarlogos没有在我的项目中使用。另一个有趣的事实是,大多数用户不使用Linux,所以这不会帮助太多的人。


对于Mac会是这样的:

PROJECT="/path/to/the/project" 
for file in $(ls -l $PROJECT/res/drawable | awk '{ print $9}' | sed 's/\..\+//g'); do count=0; for SRCFILE in `find $PROJECT -name "*.xml" -print 2> /dev/null`; do let "count+=$(grep -c @drawable/$file $SRCFILE)"; done; for SRCFILE in `find $PROJECT -name "*.java" -print 2> /dev/null`; do let "count+=$(grep -c R.drawable.$file $SRCFILE)"; done; if [ $count -lt 1 ]; then echo -e "$file not used"; else echo -e "$file used"; fi; done; 
+1

我更新了我的问题,并链接到了我写的脚本。这个脚本可能对你也很有趣。 (我总是对写bash脚本的人印象深刻...... +1 :) – WarrenFaith 2010-09-24 11:21:11

+0

OMFG !!!我只是试了一下,你做了一件很棒的工作!真的......谢谢,这将会非常有帮助。 – Cristian 2010-09-24 13:07:53

+0

辉煌。为我工作很好。我不得不将8改为9来指定文件名。由于所涉及的应用程序使用额外的文件夹进行像素密度和方向,因此我还稍微更改了可绘制文件夹路径。 – bhekman 2012-07-10 21:18:18

10

检查: http://code.google.com/p/android-unused-resources

更新2011年12月14日:现在你可以找到未使用的资源多以尽可能简单。更新至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.