背景
ButterKnife虽然帮助我们省去了繁琐的findViewById(),使用起来非常的快捷方便,但是有利必然也有弊,ButterKnife在编译期间会额外生成class文件,对App的方法数增加贡献不小,并且在library module中使用ButterKnife有着诸多不便,因此项目优化弃坑ButterKnife势在必行。
脚本说明
(1)解析@BindView注解得到view id,并在ButterKnife.bind调用处生成findViewById,从而达到替换效果。
(2)支持指定目录参数,遍历目录,递归替换,脚本运行完后需要手动清除imports。
(3)暂不支持替换@OnClick注解。
(4)console输出文件完成状态。
Example
sky:~ sky$ python replace_butterknife.py DiscountStartShowController.java
脚本code
#!/usr/bin/env python
# by sky
import os, sys, re
butter_knife_egx = re.compile('.*@BindView\(R[2]?.id.(.*)\)')
view_define_egx = re.compile('\s*(.*)\s+(.*);')
butter_bind_egx = re.compile('\s*ButterKnife.bind\([^,]+[,]?\s*(.*)\);')
def convert(file):
butterknifeline = False
view_id = None
list = file.readlines()
new_list = []
findview_lines = []
for index in range(len(list)):

本文介绍了由于ButterKnife带来的方法数增加和在library module中的不便,决定弃用该库,并提供了一个脚本来解析@BindView注解,生成findViewById的替换代码。脚本支持指定目录递归替换,并在完成后在console输出文件状态,但不处理@OnClick注解。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



