Find out if resource is used
I am looking for an efficient way to find out if a resource (mostly a drawable) is used in Java or in an XML file.
The problem is, that on my current project the drawables are changed often and now I have some drawables, which might never be used.
Is there a tool/way to find those unused drawables witho开发者_如何学运维ut search each filename in the whole project?
I wrote a tool based on python to solve this problem. As this is not the place to share it directly, I created a project page which is now offline.
UPDATE:
The development has stopped since Lint can do the same and is already included in the Android SDK.
I just wrote this bash script just for fun:
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;
It works fine, though I'm a bash newbie so it can be highly improved:
It searches drawables resources only (@drawable/name
on the XML files, and R.drawable.name
on the Java files).
By the way, I didn't know that boxscore
and calendarlogos
were not being used in my project. Another funny fact is that most users don't use Linux, so this won't help too many people.
For MacOs would be something like this:
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;
Check this: http://code.google.com/p/android-unused-resources
UPDATE 14.12.2011: Now you can find unused resources and many more 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:
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.
精彩评论