• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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 [ "$app" == "." ]
30    then
31        app=$(pwd)
32    fi
33    if [ -d $app/res ]
34    then
35        appname=$(basename $app)
36        resources=
37        for res in $(echo $app/res/* $(find $ANDROID_BUILD_TOP/vendor -type d -wholename $ANDROID_BUILD_TOP/vendor/*/$appname/res | grep overlay))
38        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
52        rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${appname}_intermediates/ -name R.java)
53
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.
56
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.
62        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')
68            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$$)
69        done | grep " 0$" | {
70            # this block gets as its input a list of constants for which no references were found, one per line
71            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        }
83        rm /tmp/everything$$
84    fi
85done
86