4

批量清理 Android 项目 build 文件夹

 3 years ago
source link: http://i.lckiss.com/?p=6622
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

批量清理 Android 项目 build 文件夹

2021-03-21

这个标题真不好取~

Android 开发久了就会累积大量的垃圾文件,有些可以直接删除,有些又想保留源码,总不能每个都打开了执行 clean,这不现实。当然,自己用 Kotlin 或者 Java 遍历下文件夹也是很简单的事,不过这种批量的有规律的重复劳动首选 Python。

import os
import shutil

def clean(dir, deepth):
    if os.path.isdir(dir):
        # print 'scan dir: ' + dir + ' === deepth:' + str(deepth)
        (path, name) = os.path.split(dir)
        if(name == "build" or name == ".gradle"):
            print '===> clean dir: ' + dir
            shutil.rmtree(dir)
            return
        if(deepth <= 0):
            return
        else:
            for path in os.listdir(dir):
                appDir = os.path.join(dir, path)
                if os.path.isdir(appDir):
                    clean(appDir, deepth-1)
    else:
        return

top = os.getcwd()
print 'scan top: ' + top
for path in os.listdir(top):
    apps = os.path.join(top, path)
    clean(apps, 4)

PS:clean 方法传入了4,也就是多遍历了几层吧,毕竟有类似这样的 module :SomeApp/app/thirdpart/xxxLib ,
当然,也可以继续加深度啦~
if 判断条件是匹配了 build.gradle 文件夹,可以自己加,比如什么 .idea .nativeBuild 之类的也是可以删的~

放在存放多个 Android 项目的文件夹吧,想必每个人都有那么一个从 github 取下来的 垃圾堆 金矿吧~
保存为 py 后缀的文件,装好 Python 环境,cd 到 需要清理的文件夹,执行:

python buildClean.py

会打印删除了什么文件的~ 也适用于离职时的删库跑路噢~ 清理一下多出十几 G 还是可以的。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK