创建 router-annotations 文件夹,并在文件夹下新建 build.gradle,配置信息如下
// 应用java 插件apply plugin: 'java'// 设置jdk 版本targetCompatibility = JavaVersion.VERSION_1_7sourceCompatibility = JavaVersion.VERSION_1_7
在工程 settings.gradle 配置注解工程
include ':app'include ':router-annotations'rootProject.name = "pluginPro"
创建自定义注解类 Destination
// 说明注解可以修饰的元素,表示该注解可以用于标记在类上@Target(ElementType.TYPE)// 说明当前注解可以被保留的时间@Retention(RetentionPolicy.CLASS)public @interface Destination { // 当前页面的URL String url(); // 当前页面的描述 String description();}
使用注解工程在主工程 build.gradle 中引入注解工程
dependencies {implementation project(':router-annotations')}
添加自定义注解
@Destination( url = "router://page-main", description = "首页")public class MainActivity extends AppCompatActivity {
创建注解处理器工程
新建注解处理器工程 router-processor,新建 build.gradle,添加配置信息
// 应用java 插件apply plugin: 'java'// 设置jdk 版本targetCompatibility = JavaVersion.VERSION_1_7sourceCompatibility = JavaVersion.VERSION_1_7dependencies { implementation project(':router-annotations')}
在工程 settings.gradle 添加注解处理器工程
include ':app'include ':router-annotations'include ':router-processor'rootProject.name = "pluginPro"
新建注解处理器类 DestinationProcessor ,在 boolean process() 中编写业务逻辑
生成类-类信息拼接
@Overridepublic boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { // 避免多次调用 process if(roundEnvironment.processingOver()){ return false; } System.out.println(TAG+" >>> process start..."); // 获取所有标记了 @Destination 注解的类信息 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(Destination.class); System.out.println(TAG + " >>> all Destination elements count = "+ elements.size()); // 当未搜集到 @Destination 注解的时候,跳过后续流程 if (elements.size() <1 ){ return false; } // 将要自动生成的类的类名 String className = "RouterMapping_" + System.currentTimeMillis(); StringBuilder builder = new StringBuilder(); builder.append("package com.imooc.router.mapping;nn"); builder.append("import java.util.HashMap;n"); builder.append("import java.util.Map;n"); builder.append("public class ").append(className).append(" {nn"); builder.append(" public static Map
添加插件注册service依赖
注解处理器工程 build.gradle
dependencies { implementation project(':router-annotations') implementation 'com.google.auto.service:auto-service:1.0-rc6' annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'}
注册注解处理器
@AutoService(Processor.class)public class DestinationProcessor extends AbstractProcessor {
使用注解处理器
主工程 build.gradle 引入注解处理器工程
implementation project(':router-annotations') annotationProcessor project(':router-processor')
为需要的类添加注解
@Destination( url = "router://page-main", description = "首页")public class MainActivity extends AppCompatActivity {
发布注解处理器插件
在跟工程 gradle.properties 中配置要提交maven的信息
POM_URL=../repoGROUP_ID = com.imooc.routerVERSION_NAME = 1.0.0
在 router-annotations 和 router-processor 项目各自新建gradle.properties,配置 POM_ARTIFACT_ID 信息
POM_ARTIFACT_ID = router-annotationsPOM_ARTIFACT_ID = router-processor
跟工程新建 maven-publish.gradle ,用于将插件发布到 maven 仓库
apply plugin: 'maven'// 读取公共配置信息Properties gradleProperties = new Properties()gradleProperties.load(project.rootProject.file('gradle.properties').newDataInputStream())def VERSION_NAME = gradleProperties.getProperty('VERSION_NAME')def POM_URL = gradleProperties.getProperty('POM_URL')def GROUP_ID = gradleProperties.getProperty('GROUP_ID')// 读取项目配置的 POM_ARTIFACT_ID 信息Properties gradleProperties2 = new Properties()gradleProperties2.load(project.file('gradle.properties').newDataInputStream())def POM_ARTIFACT_ID = gradleProperties2.getProperty('POM_ARTIFACT_ID')println "maven-publish VERSION_NAME = $VERSION_NAME"println "maven-publish POM_URL = $POM_URL"println "maven-publish GROUP_ID = $GROUP_ID"println "maven-publish POM_ARTIFACT_ID = $POM_ARTIFACT_ID"uploadArchives { repositories { mavenDeployer { // 填入发布的信息 repository(url: uri(POM_URL)) { pom.groupId = GROUP_ID pom.artifactId = POM_ARTIFACT_ID pom.version = VERSION_NAME } // 处理对工程中对其他工程的引用 pom.whenConfigured {pom -> pom.dependencies.forEach {dep-> if(dep.getVersion() == "unspecified") { dep.setGroup(GROUP_ID) dep.setVersion(VERSION_NAME) } } } } }}
在 router-annotations 和 router-processor 的 build.gradle 引入 maven-publish.gradle 插件
apply from : rootProject.file('maven-publish.gradle')
执行maven发布命令
gradle :router-annotations:uploadArchivesgradle :router-processor:uploadArchives
应用注解处理器插件在主工程 build.gradle 引入注解插件的maven 地址,然后就可以应用注解了。
implementation 'com.imooc.router:router-annotations:1.0.0' annotationProcessor 'com.imooc.router:router-processor:1.0.0'