官方文檔:Getting Started with the Annotation PRocessing Tool (apt)
如果想學(xué)習(xí)APT,那么就必須先了解Annotation的基礎(chǔ),這里附加我另外一篇文章的地址: Annotation - QQ_20198405的博客 - 博客頻道 - CSDN.NET
APT(Annotation Processing Tool)是一種處理注解的工具,它對(duì)源代碼文件進(jìn)行檢測(cè)找出其中的Annotation,使用Annotation進(jìn)行額外的處理。 Annotation處理器在處理Annotation時(shí)可以根據(jù)源文件中的Annotation生成額外的源文件和其它的文件(文件具體內(nèi)容由Annotation處理器的編寫(xiě)者決定),APT還會(huì)編譯生成的源文件和原來(lái)的源文件,將它們一起生成class文件。
新建一個(gè)名稱(chēng)為annotation的java Library,主要放置一些項(xiàng)目中需要使用到的Annotation和關(guān)聯(lián)代碼。 這里簡(jiǎn)單自定義了一個(gè)注解:
@Target(ElementType.TYPE)@Retention(RetentionPolicy.CLASS)public @interface AptAnnotation {}新建一個(gè)名為compiler的Java Library,這個(gè)類(lèi)將會(huì)寫(xiě)代碼生成的相關(guān)代碼。核心就是在這里。
tasks.withType
是設(shè)置編碼格式,避免AptProcesser 中的中文注釋報(bào)錯(cuò)依賴上面創(chuàng)建的annotation Module。生成代碼相關(guān)的邏輯就放在這里。
@AutoService(Processor.class) public class AptProcesser extends AbstractProcessor { @Override public Set<String> getSupportedAnnotationTypes() { return Collections.singleton(AptAnnotation.class.getCanonicalName()); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { return false; } }我們接下來(lái)要生成下面這個(gè)HelloWorld的代碼:
package com.example.helloworld; public final class HelloWorld { public static void main(String[] args) { System.out.println("Hello, JavaPoet!"); } }修改上述AptProcesser 的process方法
@Override public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { MethodSpec main = MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(void.class) .addParameter(String[].class, "args") .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") .build(); TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(main) .build(); JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld) .build(); try { javaFile.writeTo(processingEnv.getFiler()); } catch (IOException e) { e.printStackTrace(); } return false; }點(diǎn)擊Android Studio的ReBuild Project,可以在在app的 build/generated/source/apt/debug 目錄下,即可看到生成的代碼。
到目前我們還沒(méi)有使用注解,上面的@AptAnnotation 也沒(méi)有實(shí)際用上,下面我們做一些更加實(shí)際的代碼生成。實(shí)現(xiàn)基于注解的View,代替項(xiàng)目中的findByView 。這里僅僅是學(xué)習(xí)怎么用APT,如果真的想用DI框架,推薦使用ButterKnife,功能全面。
實(shí)際上就是通過(guò)apt生成以下代碼:
public final class DIMainActivity { public static void bindView(MainActivity activity) { activity.textView1 = (android.widget.TextView) activity.findViewById(2131427413); activity.textView2 = (android.widget.TextView) activity.findViewById(2131427414); activity.textView3 = (android.widget.TextView) activity.findViewById(2131427415); }}常用Element子類(lèi)
TypeElement:類(lèi)ExecutableElement:成員方法VariableElement:成員變量
通過(guò)包名和類(lèi)名獲取TypeName
TypeName targetClassName = ClassName.get("PackageName", "ClassName");通過(guò)Element獲取TypeNameTypeName type = TypeName.get(element.asType());獲取TypeElement的包名String packageName = processingEnv.getElementUtils().getPackageOf(type).getQualifiedName().toString();獲取TypeElement的所有成員變量和成員方法List<? extends Element> members = processingEnv.getElementUtils().getAllMembers(typeElement);總結(jié):
推薦閱讀dagger2、dbflow、ButterKnife等基于apt的開(kāi)源項(xiàng)目代碼。JavaPoet 也有很多例子可以學(xué)習(xí)。
源碼傳送 參考: Android APT(編譯時(shí)代碼生成)最佳實(shí)踐 - 推酷 例子: 代碼傳送(稍加注釋?zhuān)?Android 利用 APT 技術(shù)在編譯期生成代碼 - hb707934728的博客 - 博客頻道 - CSDN.NET
相關(guān)文章: android-apt 翻譯 - 簡(jiǎn)書(shū) 原文hvisser / android-apt — Bitbucket Annotation-Processing-Tool詳解 | qiushao http://qiushao.net/2015/07/07/Annotation-Processing-Tool%E8%AF%A6%E8%A7%A3/ AndroidSutdio 編譯時(shí)自動(dòng)生成源代碼 | Septenary http://www.septenary.cn/2015/12/19/AndroidSutdio-%E7%BC%96%E8%AF%91%E6%97%B6%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%BA%90%E4%BB%A3%E7%A0%81/
Android 打造編譯時(shí)注解解析框架 這只是一個(gè)開(kāi)始 - Hongyang - 博客頻道 - CSDN.NET http://blog.csdn.net/lmj623565791/article/details/43452969 Android 如何編寫(xiě)基于編譯時(shí)注解的項(xiàng)目 - Hongyang - 博客頻道 - CSDN.NET http://blog.csdn.net/lmj623565791/article/details/51931859
bug解決: Android Studio出現(xiàn)Error:No service of type Factor… - 簡(jiǎn)書(shū) http://www.jianshu.com/p/c4f4894ad215 Android Studio Error—Gradle: 錯(cuò)誤:編碼 GBK 的不可映射字符的 - 黑暗領(lǐng)域 - 博客頻道 - CSDN.NET http://blog.csdn.net/sljjyy/article/details/11976099
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注