新建两个JAVA library
在japtlib 里面
package com.example.japtlib;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface BindView {
int value();
}
在compiler里面
- build.gradle 里面 : compileOnly'com.google.auto.service:auto-service:1.0-rc4'
// annotationProcessor'com.google.auto.service:auto-service:1.0-rc4'
//javaPoet可以更方便的创建java文件
implementation 'com.squareup:javapoet:1.9.0'
新建一个文件 在目录 compiler/src/main/resources/META-INF/services
com.example.compiler.BindViewProcessor (包名+注解器名称)
- 新建一个类
package com.example.compiler;
import com.example.japtlib.BindView;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
@AutoService(Processor.class)//自动注册
@SupportedAnnotationTypes("com.example.japtlib.BindView")//指名要处理的注解
@SupportedSourceVersion(SourceVersion.RELEASE_7)//知名支持的java版本
public class BindViewProcessor extends AbstractProcessor {
private Messager messager;//打印日志的类
private Filer filer;
private Elements elementUtils;//元素节点工具
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
filer = processingEnvironment.getFiler();
messager = processingEnvironment.getMessager();
elementUtils = processingEnvironment.getElementUtils();
messager.printMessage(Diagnostic.Kind.NOTE, "=====init=====");
}
@Override
public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {
for (TypeElement element : set) {
messager.printMessage(Diagnostic.Kind.NOTE, "element-toString()"+(element.getQualifiedName().toString()));
if (element.getQualifiedName().toString().equals(BindView.class.getCanonicalName())) {
// main method
MethodSpec main = MethodSpec.methodBuilder("main")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(String[].class, "args")
.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
.addStatement("$T obj", ClassName.get("com.example.aptmodule","MainActivity"))
.build();
// HelloWorld class
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(main)
.build();
try {
// build com.example.HelloWorld.java
JavaFile javaFile = JavaFile.builder("com.example", helloWorld)
.addFileComment(" This codes are generated automatically. Do not modify!")
.build();
// write to file
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
@Override
public Set getSupportedAnnotationTypes() {
return Collections.singleton(BindView.class.getCanonicalName());
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
在app module里面
// 依赖注解处理器
annotationProcessor project(':compiler')
implementation project(':JAptLib')
使用:
@BindView(value = R.id.tv)
TextView textView;