Merge change 5519

* changes:
  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.
This commit is contained in:
Android (Google) Code Review
2009-06-26 09:54:04 -07:00
2 changed files with 87 additions and 3 deletions

View File

@@ -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" ]

View File

@@ -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