preface

Most Android apps have a problem with the build directory taking up too much storage when developing Android projects using AndroidStudio. In the absence of a version control tool, some inexperienced Android developers simply package their apps and send them to others. These code packages can range from tens of megabytes to hundreds of megabytes, most of which are files in the Build directory. (The bin and gen directories are similar when developing with Eclipse.)

In an AS project, there is at least one project-level build directory, with each module having its own build directory. You may find it easy to delete a few build directories before sending code, and you don’t need to go through the trouble of writing a script tool. However, due to the nature of the work, there will often be colleagues to send code, and send code to colleagues, as well as the computer stored in the company dozens of project source code, occasionally change the bug, compile once. That’s how I got the idea for a script.

The following figure shows the comparison before and after removing the Build, saving 24 GB of storage space.


A shell script

Directory traversal

Traversing files and folders is a recursive process, so declare a function called readDir recursively. Use the command ls path to traverse all files (folders) under the path, use the for loop to obtain all the output, determine the folder, output the folder path first and then recurse, determine the file, directly output.

The script content

#! /bin/bashFunction readDir(){for element in 'ls $1' # do dir_or_file=$1"/" if [-d $dir_or_file] # Then echo "$dir_or_file" $dir_or_file" readDir "$dir_or_file" else echo "$dir_or_file" $fi done} readDir $1Copy the code

There are two variables, $1. The $1 in the for loop refers to the first argument received by the readDir function. ReadDir $1 represents the first parameter received when the terminal executes the script file. Where $0 indicates the path to the script file.

echo $0
echo $1
echo $2
Copy the code

Output from the above code:

Del - build parameters 1 2 / Volumes/flueky/shell/del - build parameters 1 2Copy the code

Test script

Del - build/Volumes/flueky/lot/flueky/blog/shell delete the build directory/Volumes/flueky/lot/flueky/blog/shell delete the build directory/blog md / Volumes/flueky/lot/flueky/blog/shell delete the build directory/build/Volumes/flueky/lot/flueky/blog/build/test/shell to delete the build directory / Volumes/flueky/lot/flueky/blog/shell delete the build directory/build/test1 / Volumes/flueky/lot/flueky/PIC/blog/shell to delete the build directory / Volumes/flueky/lot/flueky/blog/shell delete the build directory/PIC / 52 baadaa2a0974465f2a4873dc306d9b. JPG / Volumes/flueky/lot/flueky/blog/shell build directory to delete/PIC/B18656263D5D14EE02C0B47A7C707BEF JPG / Volumes/flueky/lot/flueky/blog/shell delete the build/test/Volumes/flueky/lot/flueky/blog/shell delete the build/test/build / Volumes/flueky/lot/flueky/blog/shell delete the build/test/build/heheda / Volumes/flueky/lot/flueky/blog/shell/test/heheda delete the build directoryCopy the code

Del-build is the name of the script file. You can execute the script file outside the shell directory because you directly add the shell directory where the script resides to the environment variable.

After creating a script file, run commandsChmod a+x File nameAdd the execute permission to a file.

Delete build directory

Once you understand the traversal script, you can delete the specified file (folder) on this basis. Since build is a folder, you only need to add and delete in if [-d $dir_or_file].

The script content

$file__file =$1"/"$element if [-d $file__file] then if [$element = "build"] Rm -rfv $dir_or_file # delete folder else readDir $dir_or_file # delete folder fi fiCopy the code

$element is the file name of the output from the ls command, which needs to be concatenated with $1 to form a complete path. If dir_or_file is the folder, check whether the file name is build and delete and traverse the file.

Test script

Del - build/Volumes/flueky/lot/flueky/blog/shell delete the build directory/Volumes/flueky/lot/flueky/blog/shell build directory to delete/build / Volumes/flueky/lot/flueky/blog/shell delete the build directory/build/test / Volumes/flueky/lot/flueky/blog/shell delete the build directory/build/test1 / Volumes/flueky/lot/flueky/blog/shell delete build directory/build / Volumes/flueky/lot/flueky/blog/shell delete the build/test/build / Volumes/flueky/lot/flueky/blog/shell delete the build/test/build/heheda / Volumes/flueky/lot/flueky/blog/shell delete the build/test/buildCopy the code

conclusion

The complete script content is as follows:

#! /bin/bash
function readDir(){
    for element in `ls $1`
    do
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then
            if [ $element = "build" ]
            then
                echo "$dir_or_file"
                rm -rfv $dir_or_file
            else
                readDir $dir_or_file
            fi
        fi
    done
}
readDir $1
Copy the code

A simple batch operation script is now complete. You can replace “build” with $2 to dynamically specify the folder to delete when executing the del-build script.