了解了butterknife的實(shí)現(xiàn)原理后,研究了一下apt技術(shù),接著自己查閱相關(guān)資料,擼了一遍apt的實(shí)現(xiàn)過程,因?yàn)榭吹馁Y料比較老舊,實(shí)現(xiàn)過程頗為曲折,所以把自己的實(shí)現(xiàn)過程記錄一下,方便新學(xué)習(xí)的小伙伴繞開這些坑。
ATP(Annotation processing tool)
Annotation processing tool也就是注解處理器了,原理是根據(jù)注解在代碼編譯的時(shí)候去生成相應(yīng)的功能代碼文件,打包的時(shí)候會(huì)跟著其他的源碼一起打包成class文件,這樣就避免了那些功能在運(yùn)行時(shí)全部用反射去實(shí)現(xiàn),從而提高了app的性能。
首先新建一個(gè)工程,然后新建一個(gè)java library module,取名binder_annotation,這里我們專門用來存放Annotation文件,接著自定義一個(gè)Annotation,取名BindView:
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
public @interface BindView {
int value();
}
接著新建另一個(gè)java library module,取名binder_compiler,我們?cè)谶@里做注解處理的工作,和生成相應(yīng)的java文件的操作,
在這個(gè)module的gradle文件里添加如下配置:
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//AutoService 主要的作用是注解 processor 類,并對(duì)其生成 META-INF 的配置信息。
implementation 'com.google.auto.service:auto-service:1.0-rc6'
//解決gradle的版本bug,不添加會(huì)導(dǎo)致我們的process類不被調(diào)用
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'
//JavaPoet 這個(gè)庫的主要作用就是幫助我們通過類調(diào)用的形式來生成代碼。
implementation 'com.squareup:javapoet:1.10.0'
implementation project(':binder_annotation')
}
注意:Android Plugin for Gradle: >3.3.2的時(shí)候要添加 :
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6' 這行依賴,這是gradle的一個(gè)版本bug,高版本的gradle不會(huì)去調(diào)用我們編寫好的process類,我在這里就陷進(jìn)去好久。
app module下的gradle文件做如下配置:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
annotationProcessor project(':binder_compiler')
implementation project(':binder_annotation')
}
接著新建BinderProcessor類,讓它繼承AbstractProcessor,并加上@AutoService(Processor.class)注解,這樣它才會(huì)在代碼編譯期被執(zhí)行:
@AutoService(Processor.class)
public class BinderProcessor extends AbstractProcessor {
private Elements mElementUtils; ///處理Element的工具類
private HashMap<String,BinderClassCreator> mCreatorMap = new HashMap<>();//構(gòu)造器工具的緩存map
}
重寫相關(guān)方法:
//初始化
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
//處理Element的工具類,用于獲取程序的元素,例如包、類、方法。
mElementUtils = processingEnvironment.getElementUtils();
}
//使用最新的版本
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
//支持的注解類名集合,這里我們只做BindView的注解處理
@Override
public Set<String> getSupportedAnnotationTypes() {
HashSet<String> supportTypes = new LinkedHashSet<>();
supportTypes.add(BindView.class.getCanonicalName());
return supportTypes;
}
重點(diǎn)需要處理的方法是process(),相關(guān)注釋在代碼里,算是比較詳細(xì)了,多看幾遍應(yīng)該看得懂:
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
//掃描整個(gè)工程里被BindView注解過的元素,會(huì)根據(jù)activity名來生成相應(yīng)的工具類BinderClassCreator
//BinderClassCreator里包含了生成相應(yīng)的activity的_ViewBinding類,里面有做了findViewById的事情
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(BindView.class);
for(Element element:elements){
VariableElement variableElement = (VariableElement) element;//強(qiáng)轉(zhuǎn)
//返回此元素直接封裝(非嚴(yán)格意義上)的元素。
//類或接口被認(rèn)為用于封裝它直接聲明的字段、方法、構(gòu)造方法和成員類型
//這里就是獲取封裝屬性元素的類元素
TypeElement classElement = (TypeElement) variableElement.getEnclosingElement();
//獲取簡單類名
String fullClassName = classElement.getQualifiedName().toString();
//先在map緩存里取BinderClassCreator
BinderClassCreator creator = mCreatorMap.get(fullClassName);
if(creator == null){
creator = new BinderClassCreator(mElementUtils.getPackageOf(classElement),classElement);
//保存在map里
mCreatorMap.put(fullClassName,creator);
}
//獲取元素注解信息
BindView bindAnnotation = variableElement.getAnnotation(BindView.class);
int id = bindAnnotation.value();
creator.putElement(id,variableElement);
}
//通過javaPoet構(gòu)建生成java文件
for(String key:mCreatorMap.keySet()){
BinderClassCreator classCreator = mCreatorMap.get(key);
JavaFile javaFile = JavaFile.builder(classCreator.getmPackageName(), classCreator.generateJavaCode()).build();
try {
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
這里用到一個(gè)BinderClassCreator類,用來幫助構(gòu)建相應(yīng)activity__ViewBinding類的工具:
/**
* @author: lookey
* @date: 2021/5/25
* 用來生成_BinderView類的工具類
*/
public class BinderClassCreator {
public static final String ParamName = "view";
private TypeElement mTypeElement;//類元素
private String mPackageName;
private String mBinderClassName;
private HashMap<Integer, VariableElement> mVariableElement = new HashMap<>();
public BinderClassCreator(PackageElement mPackageElement, TypeElement mTypeElement) {
this.mTypeElement = mTypeElement;
this.mPackageName = mPackageElement.getQualifiedName().toString();
this.mBinderClassName = mTypeElement.getSimpleName().toString()+"_ViewBinding";
}
public void putElement(int id,VariableElement variableElement){
mVariableElement.put(id,variableElement);
}
public String getmPackageName() {
return mPackageName;
}
//生成java類,及相應(yīng)的方法
public TypeSpec generateJavaCode(){
return TypeSpec.classBuilder(mBinderClassName)
.addModifiers(Modifier.PUBLIC) //public修飾
.addMethod(generateMethod()) //添加方法
.build();
}
private MethodSpec generateMethod(){
//獲取類名
ClassName className = ClassName.bestGuess(mTypeElement.getQualifiedName().toString());
return MethodSpec.methodBuilder("bindView")
.addModifiers(Modifier.PUBLIC)
.returns(void.class)
.addParameter(className,ParamName)
.addCode(generateMethodCode())
.build();
}
private String generateMethodCode() {
StringBuilder code = new StringBuilder();
for (int id : mVariableElement.keySet()) {
VariableElement variableElement = mVariableElement.get(id);
//使用注解的屬性的名稱
String name = variableElement.getSimpleName().toString();
//使用注解的屬性的類型
String type = variableElement.asType().toString();
//view.name = (type)view.findViewById(id)
String findViewCode = ParamName + "." + name + "=(" + type + ")" + ParamName +
".findViewById(" + id + ");\n";
code.append(findViewCode);
}
return code.toString();
}
}
再寫一個(gè)工具類BinderViewTools 讓我們的activity調(diào)用,類似ButterKnife.bind(),通篇下來也就在這里用到了反射:
/**
* @author: lookey
* @date: 2021/5/25
*/
public class BinderViewTools {
public static void bind(Activity activity){
Class aClass = activity.getClass();
try {
Class<?> bindClass = Class.forName(aClass.getCanonicalName() + "_ViewBinding");//找到生成的相應(yīng)的bind類
Method method = bindClass.getMethod("bindView", aClass);
method.invoke(bindClass.newInstance(),activity);
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后在activity使用一下,使用步驟類似butterknife:
package com.trendlab.aptex;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.trendlab.binder_annotation.BindView;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv111)
public TextView tv111;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BinderViewTools.bind(this); //運(yùn)行時(shí)會(huì)去找到MainActivity_ViewBinding類,然后實(shí)例化一個(gè)對(duì)象,再調(diào)用findview()方法
tv111.setText("hello binder");
}
}
運(yùn)行編譯一下,一切正常的話會(huì)在app module下生成這個(gè)文件:

模擬器運(yùn)行成功:

整體做完還是比較清晰的,重點(diǎn)是對(duì)javaPoet的熟練使用,和生成java文件的process()方法的構(gòu)思,調(diào)試過程中遇到不能生成java文件,或者提示寫入重復(fù)報(bào)錯(cuò)的情況可以嘗試invalidate caches/restart 重啟studio,如果調(diào)試還是不成功可以參考完整項(xiàng)目:https://github.com/gi13371/APTdemo
希望這篇文章對(duì)你有幫助!