当前位置:   article > 正文

利用Python删除Android项目中未使用的layout_android as 清除无用的layout

android as 清除无用的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)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/365749
推荐阅读
相关标签
  

闽ICP备14008679号