背景
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\(