上述就是android开发分享Android解决所有双击优化的问题的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
背景
产品想对多次快速点击做一下优化,想要的效果就是双击不会打开多次
但是从开发角度来说,我可以用kotlin的拓展方法来调整这个,但是之前的历史债务可能会把我让我有点手足无措,同时java代码也会有问题。有没有什么方法可以让开发可以投机取巧呢,我想到了去年项目里写到的插桩埋点的方式,是不是我只要在编译的时候编织插入字节码就可以解决这个问题了。
transform简介
在打包流程中,我们知道生成.class文件后,利用dx工具生成.dex文件,而利用transform api可以在生成.class文件后修改.class文件,从而修改源码。我们将transform注册到appextension中,在java compile task执行后会执行tramsform类型的task。
具体开发
初始化
首先先创建一个groovy的module,然后初始化一个gradle插件。
声明一个gradle-plugins 这个基础
这个博客内有基础的流程操作
构建transform
class doubletabtransform extends transform { project project doubletabtransform(project project) { this.project = project } @override string getname() { return "doubletabtransform" } @override set<qualifiedcontent.contenttype> getinputtypes() { return transformmanager.content_jars } @override set<? super qualifiedcontent.scope> getscopes() { return transformmanager.scope_full_project } @override boolean isincremental() { return false } @override void transform(transforminvocation transforminvocation) throws transformexception, interruptedexception, ioexception { final doubletapdelegate injecthelper = new doubletapdelegate() basetransform basetransform = new basetransform(transforminvocation, new transformcallback() { @override byte[] processjarclass(string classname, byte[] classbytes, basetransform transform) { if (classutils.checkclassname(classname)) { return injecthelper.transformbyte(classbytes) } else { return null } } @override file processclass(file dir, file classfile, file tempdir, basetransform transform) { string absolutepath = classfile.absolutepath.replace(dir.absolutepath + file.separator, "") string classname = classutils.path3classname(absolutepath) if (classutils.checkclassname(classname)) { return injecthelper.begintransform(classname, classfile, transform.context.gettemporarydir()) } else { return null } } }) basetransform.starttransform() } }
上述代码对transform 以及classvisitor代码进行了一次抽象封装,方便后续如果有类似的插入逻辑可以快速接入开发。
主要的逻辑代码是对jar包以及.class文件进行扫描,当文件符合修改标准的情况下会回调文件修改的方法,然后基于asm的classvisitor 对文件进行访问操作。
classvisitor机制
这个可以看下网上的资料,我这边就不多过于简述了, 简单的说就是构造了一个类访问器,然后顺序的读取类的所以属性,方法,以及方法的每一行。
class classfiltervisitor extends classvisitor { private string[] interfaces boolean visitedstaticblock = false private string owner classfiltervisitor(classvisitor classvisitor) { super(opcodes.asm5, classvisitor) } @override void visit(int version, int access, string name, string signature, string supername, string[] interfaces) { super.visit(version, access, name, signature, supername, interfaces) this.interfaces = interfaces if (interfaces != null && interfaces.length > 0) { for (map.entry<string, methodcell> entry : methodhelper.sinterfacemethods.entryset()) { methodcell cell = entry.value if (cell != null && interfaces.contains(cell.parent)) { visitedstaticblock = true this.owner = name cv.visitfield(opcodes.acc_private + opcodes.acc_final, "doubletap", "lcom/xxx/doubleclickplugin/sample/test/doubletapcheck;", signature, null) } } } } @override fieldvisitor visitfield(int access, string name, string descriptor, string signature, object value) { return super.visitfield(access, name, descriptor, signature, value) } @override methodvisitor visitmethod(int access, string name, string desc, string signature, string[] exceptions) { if (interfaces != null && interfaces.length > 0) { try { if (visitedstaticblock && name == "<init>") { methodvisitor methodvisitor = cv.visitmethod(access, name, desc, signature, exceptions) return new initblockvisitor(methodvisitor, owner) } methodcell cell = methodhelper.sinterfacemethods.get(name + desc) if (cell != null && interfaces.contains(cell.parent)) { methodvisitor methodvisitor = cv.visitmethod(access, name, desc, signature, exceptions) checkvisitor mv = new checkvisitor(methodvisitor, owner) return mv } } catch (exception e) { e.printstacktrace() } } return super.visitmethod(access, name, desc, signature, exceptions) } }
修改前的类
public class testjavaclicklistener implements view.onclicklistener { @override public void onclick(view v) { log.i("onclick", "1"); log.i("onclick", "2"); log.i("onclick", "3"); log.i("onclick", "4"); } }
修改后的类
public class testjavaclicklistener implements onclicklistener { private final doubletapcheck doubletap = new doubletapcheck(); public testjavaclicklistener() { } public void onclick(view var1) { if (this.doubletap.isnotdoubletap()) { log.i("onclick", "1"); log.i("onclick", "2"); log.i("onclick", "3"); log.i("onclick", "4"); } } }
这个就是项目内的类访问器,其中visit方法代表类被访问了,会返回这个类继承的接口等等基础参数。我在这个方法插入了一个引用的索引,简单的说声明了一个 doubletapcheck doubletap;然后就是classvistior的visitmethod,这个是我们主要要调整的地方,其中一个关键点是我们需要修改两个地方,一个类的初始化,另外一个onclick方法。
其中init方法我们回去给doubletap 完成初始化操作,下面我们来讲下initblockvisitor。
public class initblockvisitor extends methodvisitor { private string owner; initblockvisitor(methodvisitor mv, string owner) { super(opcodes.asm5, mv); this.owner = owner; } @override public void visitinsn(int opcode) { if ((opcode >= opcodes.ireturn && opcode <= opcodes.return) || opcode == opcodes.athrow) { mv.visitvarinsn(opcodes.aload, 0); mv.visittypeinsn(opcodes.new, "com/xxxx/doubleclickplugin/sample/test/doubletapcheck"); mv.visitinsn(opcodes.dup); mv.visitmethodinsn(opcodes.invokespecial, "com/xxx/doubleclickplugin/sample/test/doubletapcheck", "<init>", "()v", false); mv.visitfieldinsn(opcodes.putfield, owner, "doubletap", "lcom/xxx/doubleclickplugin/sample/test/doubletapcheck;"); } super.visitinsn(opcode); } }
上述代码只完成了一件事情,就是在init 之后执行new doubletapcheck();这个操作。这边我使用了asm的一个idea的插件 asm bytecode viewer ,借助这个类你可以简单的把你想插入的代码的字节码都观察出来,之后再去用asm插入你想要的代码。
最后我们修改了onclick方法
public class checkvisitor extends methodvisitor { private string owner; checkvisitor(methodvisitor mv, string owner) { super(opcodes.asm5, mv); this.owner = owner; } @override public void visitcode() { mv.visitvarinsn(opcodes.aload, 0); mv.visitfieldinsn(opcodes.getfield, owner, "doubletap", "lcom/xxx/doubleclickplugin/sample/test/doubletapcheck;"); mv.visitmethodinsn(opcodes.invokevirtual, "com/xxx/doubleclickplugin/sample/test/doubletapcheck", "isnotdoubletap", "()z", false); label label = new label(); mv.visitjumpinsn(opcodes.ifne, label); mv.visitinsn(opcodes.return); mv.visitlabel(label); super.visitcode(); } }
这个代码就比较少了,他只做了一件事情,就是在onclick方法的最前面插入doubletap.isnotdoubletap()的逻辑判断。
条件语句与label分析
下面是一个onclicklistener 的插桩字节码,我们会通过分析这个类了解label的用法
public class com/xxx/doubleclickplugin/sample/testjavaclicklistener implements android/view/view$onclicklistener { // access flags 0x609 public static abstract innerclass android/view/view$onclicklistener android/view/view onclicklistener // access flags 0x12 private final lcom/xxx/doubleclickplugin/sample/test/doubletapcheck; doubletap // access flags 0x1 public <init>()v aload 0 invokespecial java/lang/object.<init> ()v aload 0 new com/xxx/doubleclickplugin/sample/test/doubletapcheck dup invokespecial com/xxx/doubleclickplugin/sample/test/doubletapcheck.<init> ()v putfield com/xxx/doubleclickplugin/sample/testjavaclicklistener.doubletap : lcom/xxx/doubleclickplugin/sample/test/doubletapcheck; return maxstack = 3 maxlocals = 1 // access flags 0x1 public onclick(landroid/view/view;)v aload 0 getfield com/xxx/doubleclickplugin/sample/testjavaclicklistener.doubletap : lcom/xxx/doubleclickplugin/sample/test/doubletapcheck; invokevirtual com/xxx/doubleclickplugin/sample/test/doubletapcheck.isnotdoubletap ()z ifne l0 return l0 ldc "onclick" ldc "1" invokestatic android/util/log.i (ljava/lang/string;ljava/lang/string;)i pop ldc "onclick" ldc "2" invokestatic android/util/log.i (ljava/lang/string;ljava/lang/string;)i pop ldc "onclick" ldc "3" invokestatic android/util/log.i (ljava/lang/string;ljava/lang/string;)i pop ldc "onclick" ldc "4" invokestatic android/util/log.i (ljava/lang/string;ljava/lang/string;)i pop return maxstack = 2 maxlocals = 2 }
我们从第24行开始观察起。首先我们获取了0位置就是view,然后我们获取了doubletap 的实例,调用了doubletab.isnotdoubletap 的方法。27行是关键,这里判断的isnotdoubletap的结果然后跳转到下面的方法块。其中最后有个l0,我一开始也不能理解这个是什么意思,最后用javap解析了字节码之后发现其实这个l0其实映射到的是下面的方法块的l0,而在真实的字节码中,这个就是对应的行数。而这个地方就是我们使用的label标签,那么label标签顾名思义,就是标记一个方法块的行数。就是两个label之间的代码行数。
github链接
以上就是android解决所有双击优化的问题的详细内容,更多关于android双击优化的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/1105550.html