A solution for filtering redundant resource files in the Android project

  • 2020-06-01 10:58:57
  • OfStack

This paper describes the solution of filtering the redundant resource files in Android project with an example, which is of great practical value! The specific description is as follows:

Many developers in Android project development process often encounter such a situation: interface developers released a new version of the bundle, but some pictures they changed the name, some pictures deleted, but at the time of implementation developers had just put the new cover to its original resources folder, with the release, in drawable or values accumulated useless resources more and more, until finally released the official version again want to remove these extra files, so have to 1 1 file check to see whether useful, before we decide to do you want to delete it.

In view of this, it is necessary to automate the testing process!

The shell script should be the first thing that comes to mind when dealing with this type of problem. The following is a paragraph that USES shell script to automatically detect whether files are used and automatically delete files that are not used:


#!/bin/sh
resfile=result.txt
#drawdir=res/layout
drawdir=res/drawable-hdpi
tmpdrawfile="tmpdraw.txt"
#clear tmp file
echo "" > $tmpdrawfile
echo "" > $resfile
ls $drawdir > $tmpdrawfile
#ls $tmpdrawfile
cat $tmpdrawfile | while read line
do
filename=`echo $line | sed 's/..*//'`
#echo $filename
#start to search
"
grepDir=./res
#grepMode=R.layout.$filename
#grepDir=./com
result=`grep -r $grepMode $grepDir`
if [ "$result" == "" ]
then
echo $line
echo $line >> $resfile
rm -f $drawdir/$line
#else
# echo "----------------"
fi
done
rm -f $tmpdrawfile

The code above is simple. First, list all the files in drawable (or you can change it to any directory), then iterate through the entire directory, checking for the use of the '@drawable /$filename' format for each file (you can change it to any format yourself), and delete the file if it is not in use. Use this script in the same directory as res.

This code can also be used in any other place where you need to detect unused/used files by simply changing the corresponding directory and matching pattern by 1, or if you want to be generic, you can write all the configuration in a single configuration file, or you can pass the configuration as a parameter.


Related articles: