ButterKnife使用方式
第一步
app/build.gradle配置
implementation 'com.jakewharton:butterknife:9.0.0-rc1' //依赖
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'//注解处理器第二步(可忽略)
插件配置
Android ButterKnife Zelezny
和IOC区别
![](https://i-blog.csdnimg.cn/blog_migrate/9db858c9334135ab891442ca4cd196af.png)
APT的Element
![](https://i-blog.csdnimg.cn/blog_migrate/237cb85cdeba93117d8852a00d144479.png)
APT的API
![](https://i-blog.csdnimg.cn/blog_migrate/df56eb2ce80f8ae5481e2082aff8380e.png)
java library annotation
/**
* ElementType
* TYPE, 类
* FIELD,
* METHOD,
* PARAMETER,
* CONSTRUCTOR,
* LOCAL_VARIABLE,
* ANNOTATION_TYPE,
* PACKAGE,
* TYPE_PARAMETER,
* TYPE_USE;
* RetentionPolicy
* SOURCE, 源码
* CLASS, 编译时
* RUNTIME; 运行时
*/
@Target(ElementType.FIELD)//属性
@Retention(RetentionPolicy.CLASS)//编译时
public @interface BindView {
//R.id.xx
int value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface OnClick {
int value();
}
java library compiler
AnnotationProcessor
谷歌提供的注解服务,写在Build.gradle RC6是向下兼容的
compileOnly 'com.google.auto.service:auto-service:1.0-rc6'
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'
package com.example.compiler;
import com.example.annotation.BindView;
import com.example.annotation.OnClick;
import com.google.auto.service.AutoService;
import java.lang.annotation.ElementType;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
/**
* 会全部扫描
*/
@AutoService(Processor.class)
public class ButterKnifeProcessor extends AbstractProcessor {
private Elements elementTypeUtls;//提供操作Elcment的方法
private Messager messager;//报告错误、警告及其他信息,在build过程中打印出来
private Filer filter;//用来创建源文件
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
Set<? extends Element> strings = roundEnvironment.getElementsAnnotatedWith(BindView.class);
return false;
}
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
elementTypeUtls = processingEnvironment.getElementUtils();
messager = processingEnvironment.getMessager();
filter = processingEnvironment.getFiler();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> stringSet = new HashSet<>();
//支出注解的类型
stringSet.add(BindView.class.getCanonicalName());
stringSet.add(OnClick.class.getCanonicalName());
return stringSet;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
android library library
package com.example.library;
import android.app.Activity;
public class ButterKnife {
public static void bind(Activity target) {
String clzName = target.getClass().getName() + "$ViewBinder";
try {
Class clz = Class.forName(clzName);
//这个类时ViewBinder 的实现类
ViewBinder obj = (ViewBinder) clz.newInstance();
obj.bind(target);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.library;
public interface ViewBinder<T> {
void bind(T target);
}