https://juejin.cn/post/7374677514536812571
https://github.com/android/gradle-recipes/blob/agp-8.4/transformAllClasses/build-logic/plugins/src/main/kotlin/ModifyClassesTask.kt
-
字节dexBuilder优化 -
得物优化
.forScope(ScopedArtifacts.Scope.ALL)
.use(taskProvider)
.toTransform(
ScopedArtifact.CLASSES,
LRouterClassTask::allJars,
LRouterClassTask::allDirectories,
LRouterClassTask::output
)
.forScope(ScopedArtifacts.Scope.PROJECT)
.use(taskProvider)
.toTransform(
ScopedArtifact.CLASSES,
LRouterClassTask::allJars,
LRouterClassTask::allDirectories,
LRouterClassTask::output
)
LRouterAsmClassVisitor::class.java,
InstrumentationScope.PROJECT
) {}
// ……
val generatedDir = “generated/ksp/” // ksp 生成目录
variant.instrumentation.transformClassesWith(
LRouterAsmClassVisitor::class.java,
InstrumentationScope.PROJECT
) { param ->
param.genDirName.set(generatedDir) // 目录名称参数
val list = project.rootProject.subprojects.plus(project)
.map { it.layout.buildDirectory.dir(generatedDir).get() } // 过滤所有 KSP 生成目录
param.inputFiles.set(list) // 设置所有子模块和主模块的生成目录
}
variant.instrumentation.setAsmFramesComputationMode(FramesComputationMode.COPY_FRAMES)
variant.instrumentation.excludes.addAll(
“androidx/**”,
“android/**”,
“com/google/**”,
)
}
abstract class LRouterAsmClassVisitor : AsmClassVisitorFactory<ParametersImpl> {
override fun createClassVisitor(
classContext: ClassContext,
nextClassVisitor: ClassVisitor
): ClassVisitor {
if (classContext.currentClassData.className == GENERATE_INJECT) {
val inputFiles = parameters.get().inputFiles.get() //取出所有 KSP 生成目录
val genDirName = parameters.get().genDirName.get()
return InsertCodeVisitor(nextClassVisitor, inputFiles, genDirName)// 插桩操作
}
return nextClassVisitor
}
override fun isInstrumentable(classData: ClassData): Boolean {
return classData.className == “com.router.LRouterGenerateImpl”
}
}
interface ParametersImpl : InstrumentationParameters {
@get:Internal
val genDirName: Property<String>
@get:Internal
val inputFiles: ListProperty<Directory>
}
原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/90308.html