blob: 6db99d5e7d815f97bee87177503b2fddeef1c896 [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001#!/bin/sh
2
Marco Nelissen291bb9f2010-01-14 16:04:35 -08003LANG=C
4
The Android Open Source Project52d4c302009-03-03 19:29:09 -08005if [ "$1" == "-h" ]
6then
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
13fi
14
15showall=no
16if [ "$1" == "-p" ]
17then
18 showall=yes
19 shift
20fi
21
22apps=$1
23if [ "$apps" == "" ]
24then
25 apps=$ANDROID_BUILD_TOP/packages/apps/*
26fi
27
28for app in $apps
29do
30 echo '-----------------------------------------------------------'
Marco Nelissen6b0277d2009-06-24 09:02:06 -070031 if [ "$app" == "." ]
32 then
33 app=$(pwd)
34 fi
The Android Open Source Project52d4c302009-03-03 19:29:09 -080035 if [ -d $app/res ]
36 then
37 appname=$(basename $app)
Marco Nelissen936e6482012-01-27 16:07:59 -080038 iappname=$(grep LOCAL_PACKAGE_NAME $app/Android.mk | sed 's/.*:= *//')
The Android Open Source Project52d4c302009-03-03 19:29:09 -080039 resources=
Marco Nelissen4357bb82009-06-25 15:53:36 -070040 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 -080041 do
42 resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')"
43 done
44 sources=$app/src
45 if [ -d $app/tests ]
46 then
47 sources="$sources $app/tests"
48 fi
49 if [ -d $app/samples ]
50 then
51 sources="$sources $app/samples"
52 fi
53
54 # find the R.java file that contains all the generated resource identifiers
Marco Nelissen936e6482012-01-27 16:07:59 -080055 rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${iappname}_intermediates/ -name R.java)
The Android Open Source Project52d4c302009-03-03 19:29:09 -080056
57 # Simplistically process the content of the file to get the names of all the constants,
58 # and try to find a reference to each constant.
Marco Nelissend0a241c2009-06-25 14:04:06 -070059
60 # First take all the input files and concatenate them, removing newlines. This allows us to
61 # find expressions that are broken up over multiple lines, i.e. R.drawable.\nsomeconstant
Marco Nelissen4357bb82009-06-25 15:53:36 -070062 find $resources $sources $app/AndroidManifest.xml -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$
Marco Nelissend0a241c2009-06-25 14:04:06 -070063
64 # Now look for each of the constants in the contatenated file.
The Android Open Source Project52d4c302009-03-03 19:29:09 -080065 for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//')
66 do
67 # Since periods in the names get translated to underscores in R.java, and you can actually
68 # refer to such constants from java by using an underscore instead of a period, we also
69 # replace all underscores with a pattern that will match periods and underscores.
70 p=$(echo $i | sed 's/_/[\\._]/g')
Marco Nelissen7d421c52011-07-20 14:48:43 -070071 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 Project52d4c302009-03-03 19:29:09 -080072 done | grep " 0$" | {
Marco Nelissend0a241c2009-06-25 14:04:06 -070073 # 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 -080074 if [ "$showall" == "yes" ]
75 then
76 echo $app
77 cat
78 else
79 count=$(wc -l)
80 if [ "$count" != "0" ]
81 then
82 echo $app: $count unused resources
83 fi
84 fi
85 }
Marco Nelissend0a241c2009-06-25 14:04:06 -070086 rm /tmp/everything$$
The Android Open Source Project52d4c302009-03-03 19:29:09 -080087 fi
88done