blob: 748139a701fe6d107b0b5468bfdca4481975433c [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 '-----------------------------------------------------------'
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
74done