一、在android中写注解一般分两个module,一个是专门存放的注解类比如叫apt-lib,一个是注解解释类比如叫apt-process,后一个依赖前一个。 apt-lib提供api要打入apk的,apt-process只参与编译。
apt-process中的build.gradle
compile project(':apt-lib')
app中的build.gradle
compile project(":apt-lib")
annotationProcessor project(':apt-process')
具体可以参考github上的butterknife和ActivityRouter开源项目。
二、通过arguments设置给编译处理器的参数
深入理解编译注解(二)annotationProcessor与android-apt
annotationProcessor传参
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
}
}
apt传参
apt{
arguments{
module "app"
}
}
接受参数方式一样
Map<String, String> options = processingEnvironment.getOptions();
Set<Map.Entry<String, String>> entries = options.entrySet();
for (Map.Entry<String, String> entry : entries) {
ss = entry.getValue() + ss;
messager.printMessage(Diagnostic.Kind.NOTE, entry.getKey() + "----" + entry.getValue());
}
参考文章:
1. 你必须知道的APT、annotationProcessor、android-apt、Provided、自定义注解