summaryrefslogtreecommitdiffstats
path: root/tools/findunused/findunusedresources
blob: 6db99d5e7d815f97bee87177503b2fddeef1c896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

LANG=C

if [ "$1" == "-h" ]
then
    cat <<- EOH
		    Usage: $0 [-p] [folder]
		      -p option prints out unused resources, otherwise a total count is printed
		      folder option causes only that app folder to be scanned, default is to scan all folders onder apps/
		EOH
    exit
fi

showall=no
if [ "$1" == "-p" ]
then
    showall=yes
    shift
fi

apps=$1
if [ "$apps" == "" ]
then
    apps=$ANDROID_BUILD_TOP/packages/apps/*
fi

for app in $apps
do
    echo '-----------------------------------------------------------'
    if [ "$app" == "." ]
    then
        app=$(pwd)
    fi
    if [ -d $app/res ]
    then
        appname=$(basename $app)
        iappname=$(grep LOCAL_PACKAGE_NAME $app/Android.mk | sed 's/.*:= *//')
        resources=
        for res in $(echo $app/res/* $(find $ANDROID_BUILD_TOP/vendor -type d -wholename $ANDROID_BUILD_TOP/vendor/*/$appname/res | grep overlay))
        do
            resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')"
        done
        sources=$app/src
        if [ -d $app/tests ]
        then
            sources="$sources $app/tests"
        fi
        if [ -d $app/samples ]
        then
            sources="$sources $app/samples"
        fi

        # find the R.java file that contains all the generated resource identifiers
        rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${iappname}_intermediates/ -name R.java)

        # Simplistically process the content of the file to get the names of all the constants,
        # and try to find a reference to each constant.

        # First take all the input files and concatenate them, removing newlines. This allows us to
        # find expressions that are broken up over multiple lines, i.e. R.drawable.\nsomeconstant
        find $resources $sources $app/AndroidManifest.xml -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$

        # Now look for each of the constants in the contatenated file.
        for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//')
        do
            # Since periods in the names get translated to underscores in R.java, and you can actually
            # refer to such constants from java by using an underscore instead of a period, we also
            # replace all underscores with a pattern that will match periods and underscores.
            p=$(echo $i | sed 's/_/[\\._]/g')
            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$$)
        done | grep " 0$" | {
            # this block gets as its input a list of constants for which no references were found, one per line
            if [ "$showall" == "yes" ]
            then
                echo $app
                cat
            else
                count=$(wc -l)
                if [ "$count" != "0" ]
                then
                    echo $app: $count unused resources
                fi
            fi
        }
        rm /tmp/everything$$
    fi
done