blob: 54b15964b43f774bc9869bf532c7b05f91671e66 [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001#!/bin/sh
2
3if [ "$1" == "-h" ]
4then
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
11fi
12
13showall=no
14if [ "$1" == "-p" ]
15then
16 showall=yes
17 shift
18fi
19
20apps=$1
21if [ "$apps" == "" ]
22then
23 apps=$ANDROID_BUILD_TOP/packages/apps/*
24fi
25
26for app in $apps
27do
28 echo '-----------------------------------------------------------'
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080029 if [ "$app" == "." ]
30 then
31 app=$(pwd)
32 fi
The Android Open Source Project52d4c302009-03-03 19:29:09 -080033 if [ -d $app/res ]
34 then
35 appname=$(basename $app)
36 resources=
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080037 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 Project52d4c302009-03-03 19:29:09 -080038 do
39 resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')"
40 done
41 sources=$app/src
42 if [ -d $app/tests ]
43 then
44 sources="$sources $app/tests"
45 fi
46 if [ -d $app/samples ]
47 then
48 sources="$sources $app/samples"
49 fi
50
51 # find the R.java file that contains all the generated resource identifiers
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080052 rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${appname}_intermediates/ -name R.java)
The Android Open Source Project52d4c302009-03-03 19:29:09 -080053
54 # Simplistically process the content of the file to get the names of all the constants,
55 # and try to find a reference to each constant.
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080056
57 # First take all the input files and concatenate them, removing newlines. This allows us to
58 # find expressions that are broken up over multiple lines, i.e. R.drawable.\nsomeconstant
59 find $resources $sources $app/AndroidManifest.xml -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$
60
61 # Now look for each of the constants in the contatenated file.
The Android Open Source Project52d4c302009-03-03 19:29:09 -080062 for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//')
63 do
64 # Since periods in the names get translated to underscores in R.java, and you can actually
65 # refer to such constants from java by using an underscore instead of a period, we also
66 # replace all underscores with a pattern that will match periods and underscores.
67 p=$(echo $i | sed 's/_/[\\._]/g')
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080068 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\\\|@dimen/$p\\\|\[a-z\]\*:$p\\\|enumname=\"$p\\\|\<item\>$p\< < /tmp/everything$$)
The Android Open Source Project52d4c302009-03-03 19:29:09 -080069 done | grep " 0$" | {
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080070 # this block gets as its input a list of constants for which no references were found, one per line
The Android Open Source Project52d4c302009-03-03 19:29:09 -080071 if [ "$showall" == "yes" ]
72 then
73 echo $app
74 cat
75 else
76 count=$(wc -l)
77 if [ "$count" != "0" ]
78 then
79 echo $app: $count unused resources
80 fi
81 fi
82 }
Jean-Baptiste Queru2c8ead32009-11-12 18:45:17 -080083 rm /tmp/everything$$
The Android Open Source Project52d4c302009-03-03 19:29:09 -080084 fi
85done