利用Python删除Android项目中未使用的layout

import os


# 查找所有没有用到的layout
def findLayout(path):
    filePath = "F:/workspace/android/app/src/main"
    keyWord = ["R.layout.","@layout/"]
    # 记录drawable文件夹下的XML文件名(不带路径)
    stringUsedLayout = []
    # 记录所有的图片(全路径名)
    stringLayout = []
    string1 = []
    with open(path, 'a', encoding='utf-8') as wf:
        # 遍历当前目录
        for root, dirs, files in os.walk(filePath):
            # 遍历文件
            for file in files:
                # pathFiles 是路径+文件名=绝对路径数组
                pathFiles = os.path.join(root, file)
                # 查找drawable目录下的xml文件
                if "\\layout\\" in pathFiles:
                    stringLayout.append(pathFiles)
                if ".java" in pathFiles or ".xml" in pathFiles:
                    # 读取每个文件
                    with open(pathFiles, 'r', encoding='utf-8') as w:
                        # 读取文件的每一行
                        for lines in w:
                            for key in keyWord:
                                if key in lines:
                                    str = lines.split(key)[1]
                                    if ")" in str:
                                        str = str.split(")")[0]
                                    if ";" in str:
                                        str = str.split(";")[0]
                                    if "," in str:
                                        str = str.split(",")[0]
                                    if "\"" in str:
                                        str = str.split("\"")[0]
                                    if str not in stringUsedLayout:
                                        stringUsedLayout.append(str)
        for s in stringLayout:
            str = s.split("\\layout\\")[1].split(".xml")[0];
            if str not in stringUsedLayout and str not in string1:
                string1.append(str)
                wf.write(str)
                wf.write("\n")
                # 删除未使用到的布局文件
                os.remove(s)



if __name__ == '__main__':
    path = "E:/text/Content.txt"
    findLayout(path)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值