博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 注解器开发流程
阅读量:3556 次
发布时间:2019-05-20

本文共 4875 字,大约阅读时间需要 16 分钟。

如何停止时间:亲吻。

如何时间旅行:阅读。
如何逃脱时间:音乐。
如何感受时间:写作。
如何释放时间:呼吸。

前言

Android使用注解生成器生成Java代码~

  • 官方介绍:。

接入流程

创建Annotation

关于反射的基础知识可见:

  • 在Android Studio中创建一个Java Library,名称叫做x_annotation
  • x_annotation的Gradle的配置如下:
apply plugin: 'java-library'apply plugin: 'kotlin'dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"}sourceCompatibility = "7"targetCompatibility = "7"
  • 之后,在Library中定义一些注解。
@Target({
METHOD, CONSTRUCTOR})@Retention(CLASS)public @interface XAnnotation {
}

创建Compiler

  • 在Android Studio中创建一个Java Library,名称叫做x_compiler

配置Gradle

  • x_compiler的Gradle的配置如下:
apply plugin: 'java-library'apply plugin: 'kotlin'dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    // JavaPoet is a Java API for generating .java source files.    // 源码:https://github.com/square/javapoet    implementation 'com.squareup:javapoet:1.12.1'    implementation project(':x_annotation')    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"}sourceCompatibility = "7"targetCompatibility = "7"

注册Processor

手动注册

  • 创建javax.annotation.processing.Processor
    • 创建文件夹:mkdir -p src/main/resources/META-INF/services/
    • 进入目录:cd src/main/resources/META-INF/services/
    • 创建文件:touch javax.annotation.processing.Processor
    • 在文件中增加Process的包名和类名:com.notzuonotdied.flutter.x_compiler.XCompiler
➜  android git:(master) ✗ tree x_compiler super_channel_compiler├── build.gradle├── libs├── src│   └── main│       ├── java│       │   └── com│       │       └── notzuonotided│       │           └── flutter│       │               └── x_compiler│       │                   └── XCompiler.kt│       └── resources│           └── META-INF│               └── services│                   └── javax.annotation.processing.Processor└── super_channel_compiler.iml11 directories, 4 files
  • 看看文件的内容:
➜  services git:(master) ✗ cat javax.annotation.processing.Processorcom.notzuonotdied.flutter.x_compiler.XCompiler

主动注册

需要在gradle中依赖谷歌提供的库:

compileOnly 'com.google.auto.service:auto-service:1.0-rc5'

在自定义的Processor上增加注解即可。

@AutoService(Processor::class)class SuperChannelCompiler : AbstractProcessor() {}

自定义Processor

这里使用了,如果不懂的,可以见这份使用文档:。

src/main/java/com/notzuonotdied/flutter/x_compiler/XCompiler.kt的实现如下:

import java.util.*import java.io.IOExceptionimport javax.annotation.processing.*import javax.lang.model.element.Modifierimport javax.lang.model.element.TypeElementimport javax.lang.model.util.Elementsimport com.squareup.javapoet.JavaFileimport com.squareup.javapoet.TypeSpecclass SuperChannelCompiler : AbstractProcessor() {
/** * 文件相关的辅助类 */ private var mFiler: Filer? = null /** * 元素相关的辅助类 */ private var mElementUtils: Elements? = null /** * 日志相关的辅助类 */ private var mMessager: Messager? = null /** * 这个函数会被注解生成器调用,在这里初始化需要的一些变量 * * @param processingEnvironment */ @Synchronized override fun init(processingEnvironment: ProcessingEnvironment) {
super.init(processingEnvironment) mElementUtils = processingEnv.elementUtils mMessager = processingEnv.messager mFiler = processingEnv.filer } /** * 生成代码 * */ override fun process(set: Set
, roundEnvironment: RoundEnvironment): Boolean {
val finderClass = TypeSpec.classBuilder("XGeneratedClass") .addModifiers(Modifier.PUBLIC) .build() try {
JavaFile.builder("com.notzuonotdied.flutter.x_compile", finderClass) .addFileComment("Generated code from x_compile Do not modify!") .build() .writeTo(mFiler) } catch (e: IOException) {
e.printStackTrace() } return true } /** * 责任链模式:用于判断这个是否由该注解生成器处理 */ override fun getSupportedAnnotationTypes(): Set
{
val types: MutableSet
= LinkedHashSet() types.add(XAnnotation::class.java.canonicalName) return types }}

使用

在主工程的Gradle中配置如下:

dependencies {    implementation project(":x_annotation")    annotationProcessor project(":x_compiler")}

在主工程中使用x_annotation定义的注解,点击编译即可。编译完成后,可以在工程下的/build/app/generated/ap_generated_sources/debug/out/com/notzuonotdied/flutter/x_compile目录看到我们生成的类XGeneratedClass,文件内容如下:

// Generated code from x_compile Do not modify!package com.notzuonotdied.flutter.x_compile;public class XGeneratedClass {
}
  • 大功告成,_

支持Kotlin

在中可以看到一句话,If you are using Kotlin, replace annotationProcessor with kapt。我们的注解器也一样,如果要支持Kotlin,也需要使用kapt。在主工程的Gradle中配置如下:

apply plugin: 'kotlin-kapt'dependencies {    implementation project(":x_annotation")    kapt project(":x_compiler")    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"}

注意: 如果你不使用kapt,那么你的RoundEnvironment中是找不到kclass的。即没办法找到Kotlin的class文件。

JavaPoet使用教程

附录

Github

转载地址:http://gqzrj.baihongyu.com/

你可能感兴趣的文章
牛客_整数1出现的次数
查看>>
牛客_旋转数组
查看>>
类中类
查看>>
牛客_二维数组
查看>>
牛客_调整数组顺序
查看>>
牛客_顺时针打印矩阵
查看>>
牛客_数组中出现超过一半的数字
查看>>
牛客_最小的k个数
查看>>
Linux 多线程服务端编程:使用 muduo C++ 网络库_学习
查看>>
主从复制
查看>>
MySql_Limit
查看>>
MySql_慢查询日志
查看>>
堆排序
查看>>
牛客_连续子数组的最大和
查看>>
牛客_把数组排成最小的数
查看>>
牛客_数组中的逆序对
查看>>
牛客_数字在排序数组中出现的次数
查看>>
牛客_数组中只出现一次的数字
查看>>
牛客_和为s的两个数字
查看>>
牛客_滑动窗口
查看>>