This article uses examples to illustrate the solution to filter unnecessary resource files in Android projects, which is very practical! The specific description is as follows:
Many developers often encounter this situation during Android project development: interface developers have released a new version of resource package, but some pictures have been changed, and some pictures have been deleted. However, when the implementation is implemented, developers only overwrite new resources into the original resource folder. With the release of the version, more and more useless resources accumulated in drawables or values. Until the official version is finally released, they want to delete these extra files, so they have to check the files one by one to see if they are useful, and then decide whether to delete them.
In view of this, it is necessary to automate the detection process!
The first thing that comes to mind when dealing with this type of problem is the shell script. The following is a function of using shell scripts to automatically detect whether files are useful and to automatically delete unused files:
#!/bin/sh resfile= #drawdir=res/layout drawdir=res/drawable-hdpi tmpdrawfile="" #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=.$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 above code is very simple. First, list all files under drawable (or you can change it to any directory), and then iterate through the entire directory to check for each file whether there is any usage of the format ‘@drawable/$filename’ (you can change it to any format yourself). If it is not used, delete the file. When using this script, you need to place it in the same directory as res.
This code can also be used anywhere else where unused/used files need to be detected. Just modify the corresponding directory and matching pattern, or if it is for general purpose, you can write all configurations in one configuration file, or you can pass the configuration as parameters.