注解主要有以下幾個來源:
- 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"
自定義注解
環(huán)境配置
Android Support配置
在build.gradle中加入依賴:
dependencies {
compile 'com.android.support:support-annotations:XXX'
}
如果依賴中有appcompat-v7空繁,該包默認(rèn)依賴support-annotations舔琅,不用再配置
support-annotations包中的內(nèi)容如下:
AndroidAnnotation配置
def AAVersion = 'XXX'
dependencies {
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
androidannotations包中的內(nèi)容如下:
官網(wǎng):http://androidannotations.org/
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.
- 體積小捏悬,無反射佩脊, 無運(yùn)行時影響,所以性能也不會受到影響 AndroidAnnotations provide those good things and even more for less than 150kb, without any runtime perf impact!
用法:
關(guān)于Android Support注解常用的用法參考:
AndroidAnnotations框架詳解
關(guān)于AndroidAnnotation注解常用用法參考:
List of all available annotations
自定義Annotation插件
參考Creating an AndroidAnnotations plugin
- 創(chuàng)建兩個Java Model,名字可以隨便起
- 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中創(chuàng)建文件 tostring-api.properties,內(nèi)容如下:
version=1.0
- annotationtostring中創(chuàng)建文件 tostring.properties,內(nèi)容如下:
version=1.0
- annotationtostring-api中創(chuàng)建Annotation
@Retention(RetentionPolicy.CLASS) // required
@Target(ElementType.TYPE) // this can vary per annotation
public @interface ToString {
}
- annotationtostring中創(chuàng)建META-INF/services/org.androidannotations.plugin.AndroidAnnotationsPlugin文件,內(nèi)容為:
com.example.ToStringPlugin
- annotationtostring中創(chuàng)建注解處理器
插件的接入類掘宪,繼承自AndroidAnnotationsPlugin
public class ToStringPlugin extends AndroidAnnotationsPlugin {
@Override
public String getName() {
return "ToString";
}
@Override
public List<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironment androidAnnotationEnv) {
List<AnnotationHandler<?>> handlers = new ArrayList<>();
handlers.add(new ToStringHandler(androidAnnotationEnv));
return handlers;
}
}
創(chuàng)建真正的處理類蛾扇,該類會處理并驗證注解
public class ToStringHandler extends BaseAnnotationHandler<EComponentHolder> {
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注解開頭的加強(qiáng)類中,但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!";
}
}
參考:
AndroidAnnotations框架詳解
Android注解(Annotation)知識點(diǎn)總結(jié)整理
官網(wǎng)
Android注解AndroidAnnotation的使用及實現(xiàn)流程分析