注解工程建立
-
创建 router-annotations 文件夹,并在文件夹下新建 build.gradle,配置信息如下
apply plugin: 'java'
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
-
在工程 settings.gradle 配置注解工程 include ':app'
include ':router-annotations'
rootProject.name = "pluginPro"
-
创建自定义注解类 Destination
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Destination {
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,添加配置信息
apply plugin: 'java'
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
dependencies {
implementation project(':router-annotations')
}
-
在工程 settings.gradle 添加注解处理器工程 include ':app'
include ':router-annotations'
include ':router-processor'
rootProject.name = "pluginPro"
-
新建注解处理器类 DestinationProcessor ,在 boolean process() 中编写业务逻辑 -
生成类-类信息拼接 @Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
if(roundEnvironment.processingOver()){
return false;
}
System.out.println(TAG+" >>> process start...");
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(Destination.class);
System.out.println(TAG + " >>> all Destination elements count = "+ elements.size());
if (elements.size() <1 ){
return false;
}
String className = "RouterMapping_" + System.currentTimeMillis();
StringBuilder builder = new StringBuilder();
builder.append("package com.imooc.router.mapping;\n\n");
builder.append("import java.util.HashMap;\n");
builder.append("import java.util.Map;\n");
builder.append("public class ").append(className).append(" {\n\n");
builder.append(" public static Map<String,String> get() {\n\n");
builder.append(" Map<String,String> mapping = new HashMap<>();\n");
for (Element element : elements) {
final TypeElement typeElement = (TypeElement) element;
Destination annotation = typeElement.getAnnotation(Destination.class);
if (annotation==null) continue;
String url = annotation.url();
String description = annotation.description();
String realPath = typeElement.getQualifiedName().toString();
System.out.println(TAG +" >>> url = "+ url);
System.out.println(TAG +" >>> description = "+ description);
System.out.println(TAG +" >>> realPath = "+ realPath);
builder.append(" ")
.append("mapping.put(")
.append("\""+url+"\"")
.append(",")
.append("\""+realPath+"\"")
.append(");\n");
}
builder.append(" return mapping;\n");
builder.append(" }");
builder.append("}\n");
String mappingFullClassName = "com.imooc.router.mapping."+className;
System.out.println(TAG + " >>> mappingFullClassName = "+mappingFullClassName);
System.out.println(TAG + " >>> class content = \n" + builder);
try{
JavaFileObject sourceFile = processingEnv.getFiler()
.createSourceFile(mappingFullClassName);
Writer writer = sourceFile.openWriter();
writer.write(builder.toString());
writer.flush();
writer.close();
}catch (Exception ex){
throw new RuntimeException("Error while create file",ex);
}
System.out.println(TAG + " >>> process finish.");
return false;
}
-
添加插件注册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=../repo
GROUP_ID = com.imooc.router
VERSION_NAME = 1.0.0
-
在 router-annotations 和 router-processor 项目各自新建gradle.properties,配置 POM_ARTIFACT_ID 信息 POM_ARTIFACT_ID = router-annotations
POM_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')
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:uploadArchives
gradle :router-processor:uploadArchives
应用注解处理器插件
在主工程 build.gradle 引入注解插件的maven 地址,然后就可以应用注解了。
implementation 'com.imooc.router:router-annotations:1.0.0'
annotationProcessor 'com.imooc.router:router-processor:1.0.0'
|