ButterKnife使用
总结自
简单使用
https://www.bilibili.com/video/BV19W411G7ua?from=search&seid=14833371388338262043
butterknife依赖注入框架源码解析
https://www.bilibili.com/video/BV1y54y1p7Fs?from=search&seid=7909994573264708291
1简单使用
省略findViewById()







例子
安装完ButterKnife插件、编写完html后



手动添加两个按钮的点击事件:

2butterknife依赖注入框架源码解析

最新的源码已经和以前不一样了
如何运行的?
传入context是使用反射了吗?
之前早的注解库是用反射完成的;
但是大量使用反射,会影响app性能,创建大量临时变量会UI卡顿 和GC。注解声明的生命周期是RUNTIME
如何解决?
APT 编译时解析技术,虽然是注解和注入,但不是RUNTIME的是编译时完成代码,对运行时不影响。
什么是注解----可以理解为减轻工作量
普通注解:
1@Override;
2@Deprecated;
3@SuppressWarings 。
元注解:注解其他注解的注解
1@Document 被JavaDoc记录;
2@Target 注解适用范围 7个值;
3@Retention 生命周期;
4@Inherited 可被继承 应该被用于class子类。
自定义注解
例子1

@interface表明了metaTest 是一个注解
@Document 被JavaDoc记录
@Target(ElementType.TYPE) 描述类或接口
@Retention(RetentionPolicy.RUNTIME)在运行时有效,可以在运行时通过反射获取内容,这里还有其他作用
@Inherited 可继承的
ButterKnife提供的一个注解源码

@Retention(RetentionPolicy.CLASS)在Class文件保留,在RUNTIME 不存在,在编译时生成绑定代码。
@Target(ElementType.FIELD) 是用来修饰域变量的
@IdRes
APT注解处理器-----javac中的工具
和运行时用反射处理注解不同,这里要讨论在编译时处理注解。
每一个注解处理器都继承于AbstractProcessor类
里面的process方法 是重点。相当于main

反射

注解和反射相结合的例子


定义RUNTIME的,可以通过反射 在运行时获取她的注解。
ButterKnife工作原理

butterKnifeProcessor