使用python 和 lint 删除项目无用资源的方法
作者:南波万 发布时间:2023-10-22 16:56:08
标签: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()
这里给一个检查结果实例吧
<issue
id="UnusedResources"
severity="Warning"
message="The resource `R.layout.activity_all_player` appears to be unused"
category="Performance"
priority="3"
summary="Unused resources"
explanation="Unused resources make applications larger and slow down builds."
errorLine1="<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
"
errorLine2="^"
quickfix="studio">
<location
file="res\layout\activity_all_player.xml"
line="2"
column="1"/>
</issue>
我们能用到的信息有 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 = 0
class _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
总结
以上所述是小编给大家介绍的使用python 和 lint 删除项目无用资源的方法网站的支持!
来源:https://juejin.im/post/5a39ccc65188251fbd33c37f?utm_source=tuicool&utm_medium=referral


猜你喜欢
- 本代码将用到wxpy模块,使用前请确保已成功安装。我喜欢命令行安装:接着就可以开始码啦:开头的红色部分为注释,去掉仍然可以运行,有效代码仅七
- Python 是一种高级的,动态的,多泛型的编程语言。Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代
- 解决方法一:使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输出的总是乱码,找了好久找到解决办法,下面分享给大家
- 本文实例为大家分享了python tkinter实现屏保程序的具体代码,供大家参考,具体内容如下该脚本摘录自:2014年辛星tkinter教
- 本文实例讲述了PHP简单实现正则匹配省市区的方法。分享给大家供大家参考,具体如下:省市区正则匹配preg_match('/(.*?(
- 在使用ionic开发IOS系统微信的时候会有一个苦恼的问题,填写表单的时候键盘会挡住输入框,其实并不算什么大问题,只要用户输入一个字就可以立
- 仿照常见的那个图片变换flash做的效果,纯js。不过滤镜变换只对应ie,ff只能看到一般的切换。这个js做的效果最早在sina看到,这里把
- 四、 用户注册(reg.php)在看用户注册之流程之前,我把表的用途做个简单说明,现在只是大概的说明,后面我们再仔细了解,大家可以记下这个说
- 解读model.named_parameters()与model.parameters()model.named_parameters()迭
- 删除链表中重复的结点: 定义两个指针pre和current两个指针同时往后移动,current指针如果与后一个结点值相同,就独自往前走直到没
- 本文用python实现线性回归算法,供大家参考,具体内容如下# -*- coding: utf-8 -*-"""
- 1.简介 蒙特卡洛又称随机抽样或统计试验,就是
- 最近 UCDChina 以“注意界面上的文字”为主题写了一系列的文章,使我在界面文字上的使用受益匪浅。之后,我对按钮上的内容的表现也做了一些
- 相信大家对javascript中的面向对象写法都不陌生,那还记得有几种创建对象的写法吗?相信大家除了自己常写的都有点模糊了,那接下来就由我来
- python 3.x 环境下,使用h5py加载HDF5文件,查看keys,如下:>>> import h5py>&g
- 如果你遇到了这个错误–MySQL Strict Mode is not set for database connection ‘defau
- 解决2个问题:1.身份证之类的文本数据自动转为科学计数法的问题。2.中文乱码的问题excel从web页面上导出的原理。当我们把这些数据发送到
- K-means(K均值)算法是最简单的一种聚类算法,它期望最小化平方误差注:为避免运行时间过长,通常设置一个最大运行轮数或最小调整幅度阈值,
- 一、数据可视化1.pyecharts介绍官方网址:https://pyecharts.org/#/zh-cn/intro📣 概况:Echar
- 字典由多个键及与其对应的值构成的对组成(把键值对成为项),每个键和它的值之间用冒号(:)隔开,项之间用逗号(,)隔开,而整个字典由一对大括号