博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用 python 和 lint 删除项目无用资源
阅读量:6894 次
发布时间:2019-06-27

本文共 3768 字,大约阅读时间需要 12 分钟。

利用 python 和 lint 删除项目无用资源

背景

有部分老项目是在Eclipse环境开发的,最近公司要求应用瘦身,老项目也在其中。如果在 AS 下开发就不会有这样的问题,但是在 Eclipse 中就不太方便了,于是就写了这个脚本。第一次用Python写东西,代码里可能会有许多Java、C这样的痕迹,见谅。

使用方法

python目录下的delUnused.py放到项目目录下,然后直接运行即可。

代码说明

利用lint进行代码审查

lint --check UnusedResources --xml [resultPath] [projectPath]

命令含义是检查项目中未使用的资源文件,并且用xml格式输出结果,需要提供检查结果输出的路径和项目路径。在脚本中已经自动提供了。

def exec_lint_command():    cmd = 'lint --check UnusedResources --xml %s %s' % (_get_lint_result_path(), _get_project_dir_path())    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)    c = p.stdout.readline().decode()    while c:        print(c)        c = p.stdout.readline().decode()复制代码

这里给一个检查结果实例吧

复制代码

我们能用到的信息有id message location 等。

解析检查结果

我是利用 minidom 解析的,具体的解析方法不多说,。

  • 获取根节点
def _parse_lint_report():    file = minidom.parse(_get_lint_result_path())    root = file.documentElement    beans = _parse_xml(root)    return beans复制代码
  • 解析第一层子节点
def _parse_xml(element, beans=None):    if beans is None:        beans = []    for node in element.childNodes:        if node.nodeName == ISSUE_KEY and node.nodeType is node.ELEMENT_NODE:            lint_bean = _LintBean()            lint_bean.id = node.getAttribute(ID_KEY)            lint_bean.severity = node.getAttribute(SEVERITY_KEY)            lint_bean.message = node.getAttribute(MESSAGE_KEY)            _parse_location(node, lint_bean)            lint_bean.print()            beans.append(lint_bean)    return beans复制代码
  • 解析location 子节点
def _parse_location(node, bean):    if not node.hasChildNodes():        return    for child in node.childNodes:        if child.nodeName == LOCATION_KEY and node.nodeType is node.ELEMENT_NODE:            bean.location.file = child.getAttribute(LOCATION_FILE_KEY)            bean.location.line = child.getAttribute(LOCATION_LINE_KEY)            bean.location.column = child.getAttribute(LOCATION_COLUMN_KEY)复制代码

用Java习惯了,解析数据喜欢用Bean

class _Location(object):    def __init__(self):        self.file = ''        self.line = 0        self.column = 0class _LintBean(object):    def __init__(self):        self.id = ''        self.severity = ''        self.message = ''        self.location = _Location()    def print(self):        print('find a %s, cause: %s. filePath: %s. line: %s' % (            self.id, self.message, self.location.file, self.location.line))复制代码

处理无用资源

解析完数据,可以得到三种资源:

  • Drawable,就一个文件,可以直接删
  • xml中的一个节点,但是这个xml中就这一个节点,直接删文件
  • xml中的一个节点,这个xml中有多个节点,删除节点

对这三种资源进行区分和删除

for lint in lint_result:    total_unused_resource += 1    if lint.id != 'UnusedResources':        continue    if lint.location.line != '':        is_single = _is_single_node(lint.location.file)        if is_single:            total_del_file += 1            del_file(lint.location.file)        else:            total_remove_attr += 1            node_name = get_node_name(lint.message)            del_node(lint.location.file, node_name)    else:        total_del_file += 1        del_file(lint.location.file)复制代码

删除文件

def del_file(file_path):    try:        os.remove(file_path)        print('remove %s success.' % file_path)    except FileNotFoundError:        print('remove %s error.' % file_path)复制代码

删除节点:

def del_node(file_path, node_name):    file = minidom.parse(file_path)    root = file.documentElement    nodes = root.childNodes    for node in nodes:        if node.nodeType in (node.TEXT_NODE, node.COMMENT_NODE):            continue        if node_name == node.getAttribute('name'):            root.removeChild(node)            file.writexml(open(file_path, 'w', encoding='UTF-8'), encoding='UTF-8')            print('remove %s, node_name:%s. success!' % (file_path, node_name))            return复制代码

至此,大功告成。

总结

当然,apk瘦身不仅仅是删除无用资源就可以的,还有冗余代码,重复文件等等,后续继续优化。

转载地址:http://kjudl.baihongyu.com/

你可能感兴趣的文章
mysql关于or的索引问题
查看>>
c#接口
查看>>
"只能在执行Render()的过程中调用RegisterForEventValidation" 解决方案
查看>>
vue中页面跳转拦截器的实现方法
查看>>
MySql连接异常解决
查看>>
mongodb Limit操作
查看>>
unbuntu14.04下的串口软件monicom的使用
查看>>
SQLite实践
查看>>
Jmeter分布式
查看>>
SQLserver数据库还原后显示"正在还原"
查看>>
Python基础-1
查看>>
jquery基础研究学习【HTML】
查看>>
几个C# Visual Studio编码技巧
查看>>
sql数据库各个版本清除日志
查看>>
jQuery扩展两类函数(对象调用,静态调用)
查看>>
nofollow标签使用方法
查看>>
sqlite实现新闻收藏和取消收藏
查看>>
Unity中的基础光照
查看>>
Final发布——视频博客
查看>>
SqlHelper类
查看>>