@Aroute路由大家都不陌生了;在组件化开发中,常用它作为组件之间的通信,如下配置:
@Aroute(path="account/login")
class LoginActivity : MyBaseActivity(){
.....
}
@Aroute(path="account/register")
class RegisterActivity : MyBaseActivity(){
.....
}
为了方便应用,我们通常会建立一个路由管理类,来统一路由的调用,每次添加一个@Aroute注解,都需要在RoutePath类中添加新的配置,如下:
public object RoutePath {
public val accountLogin: String = "/account/login"
public val accountRegister: String = "/account/register
}
虽然工作量不多;但是,也感觉不够智能;对与懒人来说,如果每个组件模块都能自动化统一路由配置,岂不是更好,接着往下看:
创建一个新的组件:C-Compiler,为java-library库
各个组件添加对该库的依赖:
dependencies {
kapt project(path: ':C-Compiler')
.
.
.
.
}
在该组件下创建如下类:
package com.chenliang.compiler
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
/**
*
* @Project: MVVM-Component
* @Package: com.chenliang.compiler
* @author: chenliang
* @date: 2021/07/28
*/
/**
* 路由全局自动生成器
* @property mFiler Filer
* @property mModuleName String?
*/
@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes(value = ["com.alibaba.android.arouter.facade.annotation.Route"])
class MyRouteCompiler : AbstractProcessor() {
private lateinit var mFiler: Filer
private var mModuleName: String? = null
init {
// System.out.println("MyRouteCompiler Processor Init--------------------------------------------")
}
override fun init(p0: ProcessingEnvironment?) {
super.init(p0)
mFiler = p0!!.filer
mModuleName = p0.options["AROUTER_MODULE_NAME"]
// System.out.println("MyRouteCompiler Processor mModuleName-------------------------------------------- $mModuleName")
}
override fun process(ms: MutableSet<out TypeElement>?, en: RoundEnvironment?): Boolean {
if (en == null) {
// return false
// System.out.println("MyRouteCompiler Processor return-------------------------------------------- ")
}
var routes = en!!.getElementsAnnotatedWith(Route::class.java)
var myRoute = TypeSpec.objectBuilder("MyRoute")
routes.forEach {
var route = it.getAnnotation(Route::class.java)
if (route != null) {
var path = route.path
var keys = path.split("/")
keys = keys.filter { !it.isNullOrEmpty() }.map {
it.substring(0, 1).toUpperCase() + it.substring(1)
}
var key = keys.joinToString(separator = "")
key = key.substring(0, 1).toLowerCase() + key.substring(1)
myRoute.addProperty(
PropertySpec.builder(key, String::class)
.initializer("%S", path)
.build()
)
}
}
val file =
FileSpec.builder("com.chenliang.processor" + mModuleName!!.replace("-", ""), "MyRoute")
.addType(myRoute.build()).build()
try {
file.writeTo(mFiler)
} catch (e: Exception) {
}
// System.out.println("MyRouteCompiler Processor Finish-------------------------------------------- ")
return true
}
}
到此就结束了:当有一个新的@Aroute配置添加后,代码Make Project后,各个组件模块都会自动创建MyRoute类,类名相同,可以根据不同包名来区分是哪个组件下的路由,以下为CAccount模块下的路由,该类统一配置了该组件的所有路由配置,如下:
package com.chenliang.processorCAccount
import kotlin.String
public object MyRoute {
public val accountLogin: String = "/account/login"
public val accountMy: String = "/account/my"
}
具体在项目中的使用,请查看Github源码:
https://github.com/chenliangj2ee/Android-MVVM-Component-Jetpack-Kotlin
|