




Android开发AsmClassVisitorFactory使用详解.docx 免费下载
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、|这篇文章主要为大家介绍了 Android开花AsmClassVisitoFactoy使用详解,有I需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升就I加薪I目录, A.刖S. AsmClassVisitorFactory.新的 Extension.实战. ClassVisitor.实际代码分析.个人观点-A 乙刖S之前就和大家介绍过版本之后Transform已经过期即将废弃的事情。而且也简单的介绍了替换的方式是Transform Action,经过我这 一阵子的学习和调研,发现只能说答对了一半吧。下面介绍个新东西 AsmClassVisitorF.and
2、roid.build.apiJnstrumentation.AsmClassVisitorFactory A factory to create class visitor objects to instrument classes. The implementation of this interface must be an abstract class where the parameters and instrumentationcontext are left unimplemented. The class must have an empty constructor which
3、w川 be used to construct the factory object.当前官方推荐使用的应该是这个类,这个类的底层实现就是基于gradle原生的Transform Action,这次的学习过程其实走了一点点弯路,一开始尝试的是TransformAction,但是貌似弯弯绕绕的,最后也没有成功,而且Transform Action的输入产14)2这局部比拟简单,把逻辑抽象定义在类ClassNode内,然后在visitEnd方法的时候调用我之前说的 accept(nextVisitor)方法。另外就是注册逻辑了,和我前面介绍的内容基本都是一样的。个人观点AsmClassVisito
4、rFactory相比拟于之前的Transform确实简化了非常非常多,我们不 需要关心之前的增量更新等等逻辑,只要专注于asm api的操作就行了。其次就是因为减少了 io操作,所以其速度自然也就比之前有所提升。同时因为 基于的是Transform Action,所以整体性能还是非常ok的,那局部增量可以说是 更简单了。另外我也和我的同事大佬交流过哦,复杂的这种类似上篇文章介绍的,最好还是 使用Grade Task的形式进行修改。物都是单一文件,修改也是针对单一文件的,所以貌似也不完全是一个很好的替 换方案,之前文章介绍的那种复杂的asm操作那么无法负荷了。AsmClassVisitorFac
5、tory根据官方说法,编译速度会有提升,大概18%左右,这个 下面我们会在使用阶段对其进行介绍的。我们先从AsmClassVisitorFactory这个抽象接口开始介绍起吧。123456789101112131415161718192021222324252AsmClassVisitorFactoIncubatinginterface AsmClassVisitorFactory<jParametersT :InstrumentationParameters : Serializable (/*The parameters that will be instantiated config
6、ured using the given config when registeringthe visitor, and injected on instantiation.*This field must be left unimplemented.*/get:Nestedval parameters: Property/*Contains parameters to help instantiate the visitor objects.*This field must be left unimplemented.*/get:Nestedval instrumentationcontex
7、t: Instrumentationcontext/*Creates a class visitor object that will visit a class with the given classContext. Thereturned class visitor must delegate its calls to nextClassVisitor.*The given classContext contains static information about the classes before starting the627282930313233343536373839404
8、14243444546474instrumentation process. Any changes in interfaces or superclasses for the class with thegiven classContext or for any other class in its classpath by a previous visitor willnot be reflected in the classContext object. *classContext can also be used to get the data for classes that are
9、 in the runtime classpathof the class being visited. *This method must handle asynchronous calls. *param classContext contains information about the class that will be instrumented by thereturned class visitor.param nextClassVisitor the Classvisitor to which the created Classvisitor must delegatemet
10、hod calls.*/fun createClassVisitor( classContext: ClassContext., nextClassVisitor: Classvisitor):Classvisitor/*Whether or not the factory wants to instrument the class with the given classData.*If returned true, createClassVisitor will be called andthe returned class visitor will49* visit the class.
11、5*0* This method must handle asynchronous calls.*/fun isInstrumentable(classData: ClassData): Boolean简单的分析下这个接口,我们要做的就是在createClassVisitor这个方法中返回一个 ClassVisitor,正常我们在构造ClassVisitor实例的时候是需要传入下一个ClassVisitor实例的, 所以我们之后在new的时候传入nextClassVisitor就行了。另外就是islnstrumentable,这个方法是判断当前类是否要进行扫描,因为如果所有 类都要通过Clas
12、sVisitor进行扫描还是太耗时了,我们可以通过这个方法过滤掉 很多我们不需要扫描的类。1Incubating2interface ClassData /* Fully qualified name of the class. TOC o 1-5 h z */Qval className: String10/* List of the annotations the class has.1*/ val classAnnotations: List/*1* List of all the interfaces that this class or a superclass1 of this c
13、lass implements.51 */val interfaces: List*7/1* List of all the super classes that this class or a super class 8I of this class extends.9*/val superclasses: ListClassData并不是asm的api,所以其中包含的内容相对来说比拟少,但是应该也勉强够用 了。这局部大家简单看看就行了,就不多做介绍了呢。新的 ExtensionAGP版本升级之后,应该是为区分新旧版的Extension,所以在AppExtension的基 础上,新增了一个
14、AndroidComponentsExtension 出来。我们的transformClassesWith就需要注册在这个上面。这个需要考虑到变种,和之 前的Transform还是有比拟大的区别的,这样我们就可以基于不同的变种增加对 应的适配工作了。val androidComponents =project.extensions.getByType(AndroidComponentsExtension:class.java)androidComponents.onVariants variant - variant.transformClassesWith(PrivacyClassVisit
15、orFactory:class.java,Instrumentationscope.ALL) variant. setAsmFramesComputationMode(FramesComputationlvlode.COPY_FRAMES)实战这次还是在之前的敏感权限api替换的字节码替换工具的基础上进行测试开发。Classvisitor看看我们正常是如何写一个简单的Classvisitor的。ClassWriter classwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);1 一Classvisitor methodFilterCV =
16、new ClassFilterVisitor(classWriter);ClassReader cr = new ClassReader(srcClass);cr.accept(methodFilterCV ClassReader.SKIP_DEBUG);return classwriter.toByteArray();首先我们会构造好一个空的ClassWriter,接着会构造一个ClassVisitor实例,然后传入这个 ClassWritero然后我们构造一个ClassReader实例,然后将byte数组传入,之后调用 classReader.accept方法,之后我们就能在visitor
17、中逐个访问数据了。那么其实我们的类信息,方法啥的都是通过ClassReader读入的,然后由当前的 ClassVisitor访问完之后交给我们最后一个ClassWritero其中ClassWriter也是一个ClassVisitor对象,他复杂重新将修改过的类转化成byte 数据。可以看得出来ClassVisitor就有一个非常简单的链表结构,之后逐层向下访 问。介绍完了这个哦,我们做个大胆的假设,如果我们这个ClassVisitor链表前插入几 个不同的ClassVisitor,那么我们是不是就可以让asm修改逐个生效,然后也不需 要多余的i。操作了呢。这就是新的asm api的设计思路了,
18、也是我们这边大佬 的字节码框架大佬的设计。另外bytex内的设计思路也是如此。tips ClassNode因为是先生成的语法树,所以和一般的ClassVisitor有点小区另U, 需要在visitEnd方法内调用accept(next)实际代码分析接下来我们上实战咯。我将之前的代码套用到这次的逻辑上来。demo地址1abstract class PrivacyClassVisitorFactory :AsmClassVisitorFactory 456789101112131415161718145678910111213141516171814 override fun createClas
19、sVisitor(classContext: ClassContextnextClassVisitor: Classvisitor): Classvisitor return PrivacyClassNode(nextClassVisitor)override fun isInstrumentable(classData: ClassData): Boolean return true我在islnstrumentable都返回的是true,其实我可以将扫描规那么限定在特定包名内,这样就 可以加快构建速度了。1class PrivacyClassNode(private val nextvisitor: Classvisitor):3 ClassNode(0pcodes.ASM5) override fun visitEnd() super.visitEnd()PrivacyHelper.whiteList.let val result = it.firstOrNull whiteName - name.contains(whiteName, true)result.apply if (this = null) / pri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 45270-2025化学纤维超短纤维拉伸性能试验方法
- 2025计算机与软件专业考试模考试题及答案
- 妇幼健康教育策略试题及答案
- 茶文化交流的重要性试题及答案
- 2025健康管理师常见试题及答案
- 2024年人力资源管理师考试价值观试题及答案
- 2025年独特土木试题及答案发掘
- 人力资源管理的未来发展趋势试题及答案
- 2025年健康管理师考试讲座要点试题及答案
- 2024年全媒体信息传播路径试题及答案
- 一评三考工作总结
- 专升本计算机教学课件-第一章-计算机基础知识(2023新版大纲)
- GB/T 23587-2024淀粉制品质量通则
- DL∕T 1120-2018 水轮机调节系统测试与实时仿真装置技术规程
- 公积金归集委托协议
- JTG D30-2015 公路路基设计规范
- 一年级下册口算题卡大全(50套直接打印版)
- 慢性肾脏病英文
- 涉密文件失泄密应急预案
- 智联招聘测评题库2024答案
- 农业社会化服务体系课件
评论
0/150
提交评论