The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ "$1" == "-h" ] |
| 4 | then |
| 5 | cat <<- EOH |
| 6 | Usage: $0 [-p] [folder] |
| 7 | -p option prints out unused resources, otherwise a total count is printed |
| 8 | folder option causes only that app folder to be scanned, default is to scan all folders onder apps/ |
| 9 | EOH |
| 10 | exit |
| 11 | fi |
| 12 | |
| 13 | showall=no |
| 14 | if [ "$1" == "-p" ] |
| 15 | then |
| 16 | showall=yes |
| 17 | shift |
| 18 | fi |
| 19 | |
| 20 | apps=$1 |
| 21 | if [ "$apps" == "" ] |
| 22 | then |
| 23 | apps=$ANDROID_BUILD_TOP/packages/apps/* |
| 24 | fi |
| 25 | |
| 26 | for app in $apps |
| 27 | do |
| 28 | echo '-----------------------------------------------------------' |
| 29 | if [ -d $app/res ] |
| 30 | then |
| 31 | appname=$(basename $app) |
| 32 | resources= |
| 33 | for res in $(echo $app/res/*) |
| 34 | do |
| 35 | resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')" |
| 36 | done |
| 37 | sources=$app/src |
| 38 | if [ -d $app/tests ] |
| 39 | then |
| 40 | sources="$sources $app/tests" |
| 41 | fi |
| 42 | if [ -d $app/samples ] |
| 43 | then |
| 44 | sources="$sources $app/samples" |
| 45 | fi |
| 46 | |
| 47 | # find the R.java file that contains all the generated resource identifiers |
| 48 | rDotJava=$(find out/target/common/obj/APPS/${appname}_intermediates/ -name R.java) |
| 49 | |
| 50 | # Simplistically process the content of the file to get the names of all the constants, |
| 51 | # and try to find a reference to each constant. |
| 52 | for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//') |
| 53 | do |
| 54 | # Since periods in the names get translated to underscores in R.java, and you can actually |
| 55 | # refer to such constants from java by using an underscore instead of a period, we also |
| 56 | # replace all underscores with a pattern that will match periods and underscores. |
| 57 | p=$(echo $i | sed 's/_/[\\._]/g') |
| 58 | echo $i $(grep -Rw R\\..*\\.$i\\\|@style/$p\\\|@drawable/$p\\\|@anim/$p\\\|@color/$p\\\|@xml/$p\\\|@layout/$p\\\|@menu/$p\\\|@+id/$p\\\|@array/$p\\\|@string/$p\\\|@dimen/$p $resources $sources $app/AndroidManifest.xml | wc -l) |
| 59 | done | grep " 0$" | { |
| 60 | # this block gets as its input a list of constants which no references were found, one per line |
| 61 | if [ "$showall" == "yes" ] |
| 62 | then |
| 63 | echo $app |
| 64 | cat |
| 65 | else |
| 66 | count=$(wc -l) |
| 67 | if [ "$count" != "0" ] |
| 68 | then |
| 69 | echo $app: $count unused resources |
| 70 | fi |
| 71 | fi |
| 72 | } |
| 73 | fi |
| 74 | done |