The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Marco Nelissen | 291bb9f | 2010-01-14 16:04:35 -0800 | [diff] [blame] | 3 | LANG=C |
| 4 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 5 | if [ "$1" == "-h" ] |
| 6 | then |
| 7 | cat <<- EOH |
| 8 | Usage: $0 [-p] [folder] |
| 9 | -p option prints out unused resources, otherwise a total count is printed |
| 10 | folder option causes only that app folder to be scanned, default is to scan all folders onder apps/ |
| 11 | EOH |
| 12 | exit |
| 13 | fi |
| 14 | |
| 15 | showall=no |
| 16 | if [ "$1" == "-p" ] |
| 17 | then |
| 18 | showall=yes |
| 19 | shift |
| 20 | fi |
| 21 | |
| 22 | apps=$1 |
| 23 | if [ "$apps" == "" ] |
| 24 | then |
| 25 | apps=$ANDROID_BUILD_TOP/packages/apps/* |
| 26 | fi |
| 27 | |
| 28 | for app in $apps |
| 29 | do |
| 30 | echo '-----------------------------------------------------------' |
Marco Nelissen | 6b0277d | 2009-06-24 09:02:06 -0700 | [diff] [blame] | 31 | if [ "$app" == "." ] |
| 32 | then |
| 33 | app=$(pwd) |
| 34 | fi |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 35 | if [ -d $app/res ] |
| 36 | then |
| 37 | appname=$(basename $app) |
| 38 | resources= |
Marco Nelissen | 4357bb8 | 2009-06-25 15:53:36 -0700 | [diff] [blame] | 39 | for res in $(echo $app/res/* $(find $ANDROID_BUILD_TOP/vendor -type d -wholename $ANDROID_BUILD_TOP/vendor/*/$appname/res | grep overlay)) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 40 | do |
| 41 | resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')" |
| 42 | done |
| 43 | sources=$app/src |
| 44 | if [ -d $app/tests ] |
| 45 | then |
| 46 | sources="$sources $app/tests" |
| 47 | fi |
| 48 | if [ -d $app/samples ] |
| 49 | then |
| 50 | sources="$sources $app/samples" |
| 51 | fi |
| 52 | |
| 53 | # find the R.java file that contains all the generated resource identifiers |
Marco Nelissen | 6b0277d | 2009-06-24 09:02:06 -0700 | [diff] [blame] | 54 | rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${appname}_intermediates/ -name R.java) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 55 | |
| 56 | # Simplistically process the content of the file to get the names of all the constants, |
| 57 | # and try to find a reference to each constant. |
Marco Nelissen | d0a241c | 2009-06-25 14:04:06 -0700 | [diff] [blame] | 58 | |
| 59 | # First take all the input files and concatenate them, removing newlines. This allows us to |
| 60 | # find expressions that are broken up over multiple lines, i.e. R.drawable.\nsomeconstant |
Marco Nelissen | 4357bb8 | 2009-06-25 15:53:36 -0700 | [diff] [blame] | 61 | find $resources $sources $app/AndroidManifest.xml -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$ |
Marco Nelissen | d0a241c | 2009-06-25 14:04:06 -0700 | [diff] [blame] | 62 | |
| 63 | # Now look for each of the constants in the contatenated file. |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 64 | for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//') |
| 65 | do |
| 66 | # Since periods in the names get translated to underscores in R.java, and you can actually |
| 67 | # refer to such constants from java by using an underscore instead of a period, we also |
| 68 | # replace all underscores with a pattern that will match periods and underscores. |
| 69 | p=$(echo $i | sed 's/_/[\\._]/g') |
Marco Nelissen | 7d421c5 | 2011-07-20 14:48:43 -0700 | [diff] [blame] | 70 | echo $i $(grep -cw R\\..*\\.$i\\\|@style/$p\\\|@drawable/$p\\\|@anim/$p\\\|@color/$p\\\|@xml/$p\\\|@layout/$p\\\|@menu/$p\\\|@+id/$p\\\|@array/$p\\\|@string/$p\\\|@mipmap/$p\\\|@integer/$p\\\|@dimen/$p\\\|\[a-z\]\*:$p\\\|enumname=\"$p\\\|\<item\>$p\< < /tmp/everything$$) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 71 | done | grep " 0$" | { |
Marco Nelissen | d0a241c | 2009-06-25 14:04:06 -0700 | [diff] [blame] | 72 | # this block gets as its input a list of constants for which no references were found, one per line |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 73 | if [ "$showall" == "yes" ] |
| 74 | then |
| 75 | echo $app |
| 76 | cat |
| 77 | else |
| 78 | count=$(wc -l) |
| 79 | if [ "$count" != "0" ] |
| 80 | then |
| 81 | echo $app: $count unused resources |
| 82 | fi |
| 83 | fi |
| 84 | } |
Marco Nelissen | d0a241c | 2009-06-25 14:04:06 -0700 | [diff] [blame] | 85 | rm /tmp/everything$$ |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 86 | fi |
| 87 | done |