summaryrefslogtreecommitdiffstats
path: root/tools/findunused
diff options
context:
space:
mode:
authorMarco Nelissen <marcone@google.com>2009-06-25 15:53:36 -0700
committerMarco Nelissen <marcone@google.com>2009-06-26 09:36:32 -0700
commit4357bb8b8cd377b0a3c12eb2f270fabfd60fc1d5 (patch)
treeeb09f8046f970ca3baf9a5e6342a7eb57afa6cc6 /tools/findunused
parentd0a241c3e8e520221befeb9c2865af65c216db20 (diff)
downloadandroid_development-4357bb8b8cd377b0a3c12eb2f270fabfd60fc1d5.tar.gz
android_development-4357bb8b8cd377b0a3c12eb2f270fabfd60fc1d5.tar.bz2
android_development-4357bb8b8cd377b0a3c12eb2f270fabfd60fc1d5.zip
Add a 'removeunusedresources' script that goes through an app and removes
all the unused resources. It uses the 'findunusedresources' script to find the resources, and the 'xmlstarlet' command to edit xml files. The result is an uncommitted git change that you can verify, modify if needed, and then check in. Also put some more fixes in the findunusedresources script to keep it from reporting false positives.
Diffstat (limited to 'tools/findunused')
-rwxr-xr-xtools/findunused/findunusedresources6
-rwxr-xr-xtools/findunused/removeunusedresources84
2 files changed, 87 insertions, 3 deletions
diff --git a/tools/findunused/findunusedresources b/tools/findunused/findunusedresources
index 5a6ecc2e4..54b15964b 100755
--- a/tools/findunused/findunusedresources
+++ b/tools/findunused/findunusedresources
@@ -34,7 +34,7 @@ do
then
appname=$(basename $app)
resources=
- for res in $(echo $app/res/*)
+ 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
@@ -56,7 +56,7 @@ do
# 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 -name .git -prune -o -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$
+ 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.*//')
@@ -65,7 +65,7 @@ do
# 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\\\|@dimen/$p < /tmp/everything$$)
+ 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$$)
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" ]
diff --git a/tools/findunused/removeunusedresources b/tools/findunused/removeunusedresources
new file mode 100755
index 000000000..0c3849499
--- /dev/null
+++ b/tools/findunused/removeunusedresources
@@ -0,0 +1,84 @@
+#!/bin/bash
+
+if ! which xmlstarlet > /dev/null
+then
+ echo "You need to have the 'xmlstarlet' command in your path"
+ exit
+fi
+
+apps=$1
+CWD=$(pwd)/
+if [ "$apps" = "" ]
+then
+ echo "Please specify the path to an application, or '--all' to process all applications"
+ exit
+elif [ "$apps" = "--all" ]
+then
+ apps=$ANDROID_BUILD_TOP/packages/apps/*
+fi
+
+BASE=$(pwd)/$(dirname $0)
+
+for app in $apps
+do
+ pushd $app
+ $BASE/findunusedresources -p . | {
+ read LINE NUM
+ while [ "$LINE" != "" ]
+ do
+ if [ "Z$LINE" = "Z-----------------------------------------------------------" ]
+ then
+ # skip
+ true
+ elif [ "$LINE" = "$app" ]
+ then
+ # skip
+ true
+ else
+ # try to find the missing resource
+ find res | grep -w $LINE | {
+ read RESLINE
+ while [ "$RESLINE" != "" ]
+ do
+ if [ -f $RESLINE ]
+ then
+ echo REMOVING FILE: $RESLINE
+ git rm $RESLINE > /dev/null
+ else
+ echo WARNING unexpected result for $LINE
+ fi
+ read RESLINE
+ done
+ }
+ grep -Rwl $LINE res | {
+ read RESLINE
+ while [ "$RESLINE" != "" ]
+ do
+ ISSTRING=$(echo "$RESLINE" | grep -w "strings\.xml")
+ if [ -n "$ISSTRING" ]
+ then
+ echo REMOVING STRING $LINE from $RESLINE
+ xmlstarlet ed -P -S -d "/resources/string[@name='$LINE']" $RESLINE > tf$$
+ mv tf$$ $RESLINE
+ git add $RESLINE
+ else
+ echo REMOVING $LINE from $RESLINE
+ xmlstarlet ed -P -S -d "/resources/*[@name='$LINE']" $RESLINE > tf$$
+ mv tf$$ $RESLINE
+ git add $RESLINE
+ fi
+ read RESLINE
+ done
+ }
+ fi
+ read LINE NUM
+ done
+ }
+ popd
+done
+echo
+echo "Done."
+echo "Please rebuild the updated applications to make sure that everything still builds."
+echo "After rebuilding, rerun 'findunusedresources' or 'removeunusedresources' to see if any more resources are now unused."
+echo "When you're done, you can 'git commit' the change."
+echo