注解主要有以下几个来源:
Java中的注解:
元注解 位于java.lang.annotation包中 @Documented @Inherited @Repeatable @Retention @Target
普通注解位于java.lang包中 @Override@Deprecated @FunctionalInterface(1.8后) @SuppressWarnings
Android 原生注解:
位于android.annotation包中 @TargetApi @SuppressLint
Android Support中的注解 :
位于android.support.annotation包中。@Nullable @NonNull @CheckResult @ColorRes(资源引用) @Size @IntRange(范围) @MainThread(线程) @Keep(Proguard) @RequiresPermission @RequiresApiss
通过appcompat-v7依赖间接引入 (compile 'com.android.support:appcompat-v7:XXX')
直接引入(compile ‘com.android.support:support-annotations:XXX’)
AndroidAnnotations注解
位于org.androidannotations.annotations包中。
包引入:compile "org.androidannotations:androidannotations-api: $AAVersion"
注解处理器引入:annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
自定义注解
环境配置
Android Support配置
在build.gradle中加入依赖:
dependencies {
compile 'com.android.support:support-annotations:XXX'
}
如果依赖中有appcompat-v7,该包默认依赖support-annotations,不用再配置
image.png
support-annotations包中的内容如下:
image.png
AndroidAnnotation配置
def AAVersion = 'XXX'
dependencies {
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
androidannotations包中的内容如下:
image.png
AndroidAnnotation Features(AA特性)
Dependency injection(依赖注入 ): inject views, extras, system services, resources, ...
Simplified threading model(简化了线程操作): annotate your methods so that they execute on the UI thread or on a background thread.
Event binding(事件绑定): annotate methods to handle events on views, no more ugly anonymous listener classes!
REST client(REST支持): create a client interface, AndroidAnnotations generates the implementation.
No magic(AA不是魔法,只是生成了"SampleActivity_"的子类): As AndroidAnnotations generate subclasses at compile time, you can check the code to see how it works.
体积小,无反射, 无运行时影响,所以性能也不会受到影响 AndroidAnnotations provide those good things and even more for less than 150kb, without any runtime perf impact!
用法:
关于Android Support注解常用的用法参考:
AndroidAnnotations框架详解
关于AndroidAnnotation注解常用用法参考:
List of all available annotations
自定义Annotation插件
创建两个Java Model,名字可以随便起
image.png
annotationtostring-api:包含自定义的注解@ToString
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
annotationtostring:定义注解处理器,及生成代码Api,需要依赖annotationtostring-api Model,及org.androidannotations:androidannotations
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':annotationtostring-api')
compile 'org.androidannotations:androidannotations:4.4.0'
compile 'com.helger:jcodemodel:3.0.1'//用于生成 Java 代码的 Java 库
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
确定一个唯一的名称:例如ToString
annotationtostring-api中创建文件 tostring-api.properties,内容如下:
version=1.0
annotationtostring中创建文件 tostring.properties,内容如下:
version=1.0
annotationtostring-api中创建Annotation
@Retention(RetentionPolicy.CLASS) // required
@Target(ElementType.TYPE) // this can vary per annotation
public @interface ToString {
}
annotationtostring中创建META-INF/services/org.androidannotations.plugin.AndroidAnnotationsPlugin文件,内容为:
com.example.ToStringPlugin
image.png
annotationtostring中创建注解处理器
插件的接入类,继承自AndroidAnnotationsPlugin
public class ToStringPlugin extends AndroidAnnotationsPlugin {
@Override
public String getName() {
return "ToString";
}
@Override
public List> getHandlers(AndroidAnnotationsEnvironment androidAnnotationEnv) {
List> handlers = new ArrayList<>();
handlers.add(new ToStringHandler(androidAnnotationEnv));
return handlers;
}
}
创建真正的处理类,该类会处理并验证注解
public class ToStringHandler extends BaseAnnotationHandler {
public ToStringHandler(AndroidAnnotationsEnvironment environment) {
super(ToString.class, environment); // this handles your @ToString annotation
}
@Override
protected void validate(Element element, ElementValidation validation) {
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validation);//该注解只能用在以@E注解开头的加强类中,但MainActivity中即使添加了@EActivity注解,最后还是报以下编译错误,注掉不验证便可。
}
@Override
public void process(Element element, EComponentHolder holder) throws Exception {
JMethod toString = holder.getGeneratedClass().method(JMod.PUBLIC, getClasses().STRING, "toString");
toString.body()._return(JExpr.lit("Hello, AndroidAnnotations!"));
toString.annotate(Override.class);
}
}
错误:
com.example.note.ToString can only be used in a package annotated with
@interface org.androidannotations.annotations.EApplication,
@interface org.androidannotations.annotations.EActivity,
@interface org.androidannotations.annotations.EViewGroup,
@interface org.androidannotations.annotations.EView,
@interface org.androidannotations.annotations.EBean,
@interface org.androidannotations.annotations.EService,
@interface org.androidannotations.annotations.EIntentService,
@interface org.androidannotations.annotations.EReceiver,
@interface org.androidannotations.annotations.EProvider,
@interface org.androidannotations.annotations.EFragment.
警告:
Element com.minicup.annotation.MainActivity invalidated by ToStringHandler
Build 后生成 MainActivity_
@EActivity(R.layout.activity_main)
@ToString
public class MainActivity extends AppCompatActivity {
}
public final class MainActivity_ extends MainActivity implements HasViews{
@Override
public String toString() {
return "Hello, AndroidAnnotations!";
}
}
参考: