新建modue: java_apt
在build.gradle添加下面内容:
1 2
| implementation 'com.google.auto.service:auto-service:1.0.1' annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
|
新建类BaseProcessor继承AbstractProcessor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import com.google.auto.service.AutoService;
import javax.annotation.processing.*; import javax.lang.model.SourceVersion; import javax.lang.model.element.TypeElement; import java.util.Set; @AutoService(Processor.class) @SupportedAnnotationTypes("sunrise.annotation.Hello") public class BaseProcessor extends AbstractProcessor {
@Override public synchronized void init(ProcessingEnvironment env) { System.out.println("sadasdasdsad"); }
@Override public boolean process(Set<? extends TypeElement> annoations, RoundEnvironment env) { System.out.println("sadasdasdsadasdasdasddsad");
return true; } }
|
然后在最外层的build.gradle添加下面内容:
1
| annotationProcessor(project(":java_apt"))
|
这个时候build下我们的工程:会看到生成下面这些内容:
这就是使用这种方法的好处,他会自动帮你生成这些文件.