參考文章
GitHub
https://github.com/alibaba/ARouter
1.簡單用法
http://www.itdecent.cn/p/7cb2cc9b726a?from=groupmessage
2.分析
http://www.itdecent.cn/p/83990e97e74e
demo
https://github.com/juexingzhe/AnnotationCompile
ARouter作者原文
https://baijiahao.baidu.com/s?id=1561188227593347&wfr=spider&for=pc
概要:
1.ARouter如何使用
http://www.itdecent.cn/p/7cb2cc9b726a?from=groupmessage
2.Android 模塊開發(fā)之APT技術(shù)
(1)為什么要說APT
為什么非要說這個(gè)東西呢?因?yàn)檫@是ARouter這個(gè)框架中的核心技術(shù)之一,如果不了解APT談深入調(diào)研ARouter那根本就是瞎扯
(2)什么是APT
APT就是Annotation Processing Tool 的簡稱,就是可以在代碼編譯期間對注解進(jìn)行處理,并且生成Java文件,減少手動的代碼輸入。Java注解分為三類編碼注解、編譯注解、運(yùn)行時(shí)注解詳細(xì)可參考http://www.itdecent.cn/p/fad15887a05e
(3)開始寫一個(gè)例子

新建一個(gè)modlue,注意一定要是Java的Library,然后新建一個(gè)注解類如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface RouteAnnotation {
String name();
}
創(chuàng)建注解處理器
注解處理器一般會是一個(gè)項(xiàng)目比較底層的模塊,因此我們創(chuàng)建一個(gè)Java Library,annotationprocess模塊,要依賴前面的注解。
apply plugin: 'java'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':annotation')
compile 'com.squareup:javapoet:1.7.0'
compile 'com.google.auto.service:auto-service:1.0-rc2'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
自定義的處理器需要繼承AbstractProcessor,需要自己實(shí)現(xiàn)process方法,一般我們會實(shí)現(xiàn)其中的4個(gè)方法:
public class AnnotationProcessor extends AbstractProcessor {
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) { }
@Override
public SourceVersion getSupportedSourceVersion() { }
@Override
public Set<String> getSupportedAnnotationTypes() { }
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { }
}
init()方法可以初始化拿到一些使用的工具,比如文件相關(guān)的輔助類 Filer;元素相關(guān)的輔助類Elements;日志相關(guān)的輔助類Messager;
getSupportedSourceVersion()方法返回 Java 版本;
getSupportedAnnotationTypes()方法返回要處理的注解的結(jié)合;
上面幾個(gè)方法寫法基本都是固定的,重頭戲是process()方法。
這里我下的很簡單,ARouter的自定注解處理器寫的很復(fù)雜