找出是否使用了资源

War*_*ith 28 resources android tracking drawable

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

问题是,在我当前的项目中,drawables经常被更改,现在我有一些drawables,可能永远不会被使用.

有没有工具/方法找到那些未使用的drawables而不搜索整个项目中的每个文件名?

War*_*ith 31

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

更新:
开发已经停止,因为Lint可以做同样的事情并且已经包含在Android SDK中.


Cri*_*ian 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; 
Run Code Online (Sandbox Code Playgroud)

它工作正常,虽然我是一个bash新手,所以它可以大大改善:

替代文字

它仅搜索drawables资源(@drawable/name在XML文件和R.drawable.nameJava文件上).

顺便说一句,我不知道,boxscorecalendarlogos没有在我的项目中使用.另一个有趣的事实是大多数用户不使用Linux,所以这对太多人来说无济于事.


对于MacO会是这样的:

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; 
Run Code Online (Sandbox Code Playgroud)

  • 效果很好!虽然我不得不用`cut -d \'替换`sed'的/\...+// g'.-f1` - 前者返回完整文件名,带扩展名(`.png`或`.9.png`).这是在Mac上使用zsh (2认同)

And*_*yeu 10

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

更新14.12.2011:现在您可以找到尽可能简单的未使用资源.更新到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)