当前位置:网站首页>APT + Transform 实现多模块应用Application生命周期分发
APT + Transform 实现多模块应用Application生命周期分发
2022-08-02 05:19:00 【JAVAQXQ】
前言
组件化开发过程中,各个模块内往往需要感知Application的生命周期,以获取context完成初始化工作等,业务上模块间或存在初始化顺序要求,比较常规的做法有:
有需要大厂面经和面试技巧思维导图的朋友可以点进去了解一下,点击——【传送门】——即可!

在common模块定义 AppLifecycleCallback
interface AppLifecycleCallback {
fun getPriority(): Int = 0
fun onCreate(context: Context)
}
复制代码各个模块实现 AppLifecycleCallback ,如Home模块
class HomeAppLifecycle : AppLifecycleCallback {
override fun getPriority(): Int = 0
override fun onCreate(context: Context) {
//todo
}
}
复制代码然后在 MainApplication 中实现生命周期分发
class MainApplication : Application() {
private val callbacks = mutableListOf<AppLifecycleCallback>()
override fun onCreate() {
super.onCreate()
callbacks.add(HomeAppLifecycle())
callbacks.add(UserAppLifecycle())
// add whatever you want
// 排序实现模块顺序分发
callbacks.sortBy { it.getPriority() }
callbacks.forEach { it.onCreate(this) }
}
}
复制代码这样能实现需求,但每增加一个模块,就得回到MainApplication中添加一个,不够优雅不够装,这时候就可以用上 APT + Transform
实现原理
- 各模块创建AppLifecycleCallback实现类,添加注解,apt为其生成代理类,以"_Proxy"结尾
- 构造 AppLifecycleManager 对外API,统一管理Proxyc allbacks
- gradle transform遍历.class文件,找到"_Proxy"的所有类,保存类路径,使用 asm 将代理类“加入”到AppLifecycleManager中
此方案主要是为了解学习apt,gradle transform,实际上有更好的实现方案。各位大佬畅所欲言,提出此方案的短板
实现过程
一、 AppLifecycle API
创建android-library,定义 AppLifecycleCallback 、 AppLifecycleManager

interface AppLifecycleCallback {
fun getPriority(): Int = 0
fun onCreate(context: Context)
}
复制代码AppLifecycleManager中 onCreate 是对外的API,在MainAppkication中调用; registerAppLifecycleCallback 则是在transform阶段使用asm在AppLifecycleManager的构造方法中调用,将代理类路径传入,最终通过反射存储在callbacks中
object AppLifecycleManager {
private var callbacks: MutableList<AppLifecycleCallback>? = null
fun onCreate(context: Context) {
callbacks?.run {
sortBy { it.getPriority() }
forEach { it.onCreate(context) }
}
}
private fun registerAppLifecycleCallback(name: String) {
try {
if (callbacks == null) {
callbacks = mutableListOf()
}
val instance = Class.forName(name).getConstructor().newInstance()
if (instance is AppLifecycleCallback && !callbacks!!.contains(instance)) {
callbacks!!.add(instance)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
复制代码二、 APT
- 创建kotlin-library,定义annotation

@Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) annotation class AppLifecycle() 复制代码
- 创建kotlin-library,定义Processor,获取所有 @AppLifecycle注解类,使用 KotlinPoet 生成Proxy类

@AutoService(Processor::class)
class AppLifecycleProcessor : AbstractProcessor() {
// 省略部分代码
override fun process(p0: MutableSet<out TypeElement>?, environment: RoundEnvironment?): Boolean {
environment?.run {
getElementsAnnotatedWith(AppLifecycle::class.java)
.filter { it.kind == ElementKind.CLASS }
.filter {
(it as TypeElement).interfaces.contains(
elements.getTypeElement(callbackName).asType()
)
}
.forEach {
AppLifecycleProxyBuilder(it as TypeElement, elements).build().writeTo(filer)
}
}
return true
}
}
复制代码KotlinPoet生成Proxy类
class AppLifecycleProxyBuilder(private val typeElement: TypeElement, elements: Elements) {
// 省略部分代码
fun build(): FileSpec {
return FileSpec.builder(packageName, fileName)
.addType(getTypeSpec())
.build()
}
private fun getTypeSpec(): TypeSpec {
return TypeSpec.classBuilder(fileName)
.addProperty(getProperty())
.addSuperinterface(superInterface)
.addFunction(getOnCreate())
.addFunction(getPriority())
.build()
}
private fun getProperty(): PropertySpec {
// 对应注解类实例
return PropertySpec.builder("callback", typeElement.asClassName(), KModifier.PRIVATE)
.initializer("%T()", typeElement.asType())
.build()
}
private fun getOnCreate(): FunSpec {
// onCreate(context: Context)
return FunSpec.builder("onCreate")
.addModifiers(KModifier.OVERRIDE)
.addParameter("context", contextType)
.addStatement("callback.onCreate(context)")
.build()
}
private fun getPriority(): FunSpec {
// getPriority(): Int
return FunSpec.builder("getPriority")
.addModifiers(KModifier.OVERRIDE)
.returns(Int::class)
.addStatement("return callback.getPriority()")
.build()
}
}
复制代码注:kotlin使用apt,要在build.gradle要有以下两个声明
plugins {
id 'kotlin-kapt'
}
dependencies {
kapt (project(":processor"))
}
复制代码三、 Gradle Transform
创建kotlin-library

定义AppLifecyclePlugin,继承Transform,实现Project接口
class AppLifecyclePlugin : Transform(), Plugin<Project> {
private val appLifecycleClassNames = mutableListOf<String>()
private var appLifecyclesJar:File? = null
override fun transform(transformInvocation: TransformInvocation?) {
// transform中遍历.class文件,类名以“_Proxy”结尾并且实现AppLifecycleCallback
...
if (name.endsWith(proxySuffix) && classReader.interfaces.contains(callbackInfo)) {
appLifecycleClassNames.add(name)
}
...
// 定位到包含 ApplifecycleManager的JarEntry
if(jarEntity.name == managerClassFile){
appLifecyclesJar = outputJar
}
// 最终使用ClassVistor将所有Proxy类加入到Manager中的callbacks里
val cv: ClassVisitor = AppLifecycleVisitor(classWriter,appLifecycleClassNames)
classReader.accept(cv, ClassReader.EXPAND_FRAMES)
classWriter.toByteArray()
}
}
复制代码AppLifecycleVisitor
class AppLifecycleVisitor(classVisitor: ClassVisitor,private val callbacks: List<String>) : ClassVisitor(Opcodes.ASM9,classVisitor) {
override fun visitMethod(
access: Int,
name: String?,
descriptor: String?,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
var visitor = super.visitMethod(access, name, descriptor, signature, exceptions)
if ("<init>" == name && "()V" == descriptor && access and Opcodes.ACC_PRIVATE != 0) {
visitor = object : AdviceAdapter(ASM9, visitor, access, name, descriptor) {
override fun onMethodExit(opcode: Int) {
for (item in callbacks) {
mv.visitVarInsn(ALOAD, 0)
mv.visitLdcInsn(item.replace("/", "."))
mv.visitMethodInsn(
INVOKESPECIAL,
"com/lauter/applifecycle/AppLifecycleManager",
"registerAppLifecycleCallback",
"(Ljava/lang/String;)V",
false
)
}
}
}
}
return visitor
}
}
复制代码创建完AppLifecyclePlugin,创建文件src/main/resources/META-INF/gradle-plugins/lauter.applifecycle.properties
implementation-class=com.lauter.applifecycle.AppLifecyclePlugin 复制代码
到此,工作基本完成。只需要将plugin发布到本地,就可以测试功能了
四、 发布测试
在上文创建的plugin项目下build.gradle中添加:
plugins {
...
// 添加publish
id 'maven-publish'
}
...
// 发布到本地
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
groupId = "io.github.chenlauter"
artifactId = "applifecycle"
version = "1.0"
}
}
repositories {
mavenLocal()
maven {
url = '../local-plugin-repository'
}
}
}
复制代码在gradle中执行publish,发布完成后项目中会新增 local-plugin-repository 文件夹

在project的build.gradle中添加依赖
buildscript {
repositories {
...
// 添加依赖,gradle7.1之后是到setting.gralde-pluginManagement中添加
maven { url './local-plugin-repository' }
}
dependencies {
...
classpath('io.github.chenlauter:applifecycle:1.0')
}
}
复制代码最后在app的build.gradle中添加
plugins {
...
// 这里跟第三步创建的lauter.applifecycle.properties文件名对应
id 'lauter.applifecycle'
}
复制代码至此,所以依赖都配置完毕,运行工程,在app-build下,用Android Studio直接打开apk查看 AppLifecycleManager的字节码,可以看到,在构造方法中已加入两个Proxy类路径调用:

通过查看日志打印,也能看到功能正常
D/AppLifecycle: HomeAppLifecycle onCreate D/AppLifecycle: MainAppLifecycle onCreate 复制代码
项目地址
启发参考
边栏推荐
- Redis集群模式
- C 竞赛——捕鱼
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- 测试技术之APP蓝牙连接测试
- 【OpenCV从入门到实践】图像处理技术[像素](全网最详细)
- 软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
- 51 microcontroller peripherals article: dot-matrix LCD
- Differences between i++ and ++i in loops in C language
- Polar Parametrization for Vision-based Surround-View 3D Detection Paper Notes
猜你喜欢

C语言操作符详解(2)

OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?

Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记

go里面的基本知识

关于 VS Code 优化启动性能的实践

Double for loop case (use js jiujiu printing multiplication table)

Meta公司内部项目-RaptorX:将Presto性能提升10倍
![[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays](/img/eb/9b05508e88b7f17d80de2afa8c08ce.png)
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays

目标检测重要概念——IOU、感受野、空洞卷积、mAP

zabbix自动发现和自动注册
随机推荐
5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
5款经典代码阅读器的使用方案对比
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
pytorch基本操作:使用神经网络进行分类任务
整合ssm(一)
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
使用TinkerPop框架对GDB增删改查
跨桌面端Web容器演进
leetcode一步解决链表反转问题
Redis-----非关系数据库
Shuttle + Alluxio 加速内存Shuffle起飞
H5 access payment process - WeChat payment & Alipay payment
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
线程基础(一)
淘系资深工程师整理的300+项学习资源清单(2021最新版)
Use the browser's local storage to realize the function of remembering the user name
程序员写PPT的小技巧
反向解析dns服务器
Not annotated parameter overrides @NonNullApi parameter
How to perform concurrent calculation (stability test and stress test)?