一:引入hibeaver(略,网上有资料,简单介绍)
1.classpath 'com.bryansharp:hibeaver:1.2.5'
二:使用
1.gradle中配置
apply plugin: 'hiBeaver' import com.bryansharp.gradle.hibeaver.utils.MethodLogAdapter import org.objectweb.asm.ClassVisitor import org.objectweb.asm.MethodVisitor import org.objectweb.asm.Opcodes import org.objectweb.asm.Label hiBeaver { //turn this on to make it print help content, default value is true showHelp = true //this flag will decide whether the log of the modifying process be printed or not, default value is false keepQuiet = false //this is a kit feature of the plugin, set it true to see the time consume of this build watchTimeConsume = false //this is the most important part // modifyMatchMaps = ['com.mns.MnsBroadcastReceiver':['methodName': 'onReceive']] modifyMatchMaps = [ //此处可以进行模糊匹配,!表示排除,!android*即表示排除掉android开头的全类名。 //|符号不完全表示或,而仅仅是匹配的分隔符。*表示任意长度(>0)的任意字符 'com.mns.MnsBroadcastReceiver' : [ //methodDesc设置为空代表对methodDesc不进行限制 //方法名也可以用模糊匹配 用javap -s 命令来查看类中方法的description ['methodName': 'onReceive', 'methodDesc': null, 'adapter': { ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions -> MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions); MethodVisitor adapter = new MethodLogAdapter(methodVisitor) { @Override void visitCode() { //下面这行代码 为要调用的方法,请酌情修改 super.visitCode(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ad/ad/abc/AbcCacheManager", "isReturnAbc", "()Ljava/lang/Object;"); Label myLable = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, myLable); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(myLable); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ad/ad/abc/AbcCacheManager", "isEnd", "()V"); } } return adapter; }] ] ] // modifyTasks = ["${rootDir.absolutePath}/libs/Mns_1.0.2.jar": modifyMatchMaps] } // modifyTasks = ["${rootDir.absolutePath}/libs/Mns_1.0.2.jar": modifyMatchMaps]打开这句话生成新的jar
文章出处:https://blog.csdn.net/pangzaifei/article/details/70213731 主要:
准备修改第三方jar的类
'com.mns.MnsBroadcastReceiver'想修改的方法onReceive()
['methodName': 'onReceive', 'methodDesc': null, 'adapter':
插入
super.visitCode(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ad/ad/abc/AbcCacheManager", "isReturnAbc", "()Ljava/lang/Object;"); Label myLable = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, myLable); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(myLable); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ad/ad/abc/AbcCacheManager", "isEnd", "()V");
你想往第三方jar包插入的代码:
"com/ad/ad/abc/AbcCacheManager",方法:isReturnAbc,参数:"()Ljava/lang/Object;"public static Object isReturnAbc(Object var1) { Context context = null; if (var1 instanceof Context) { context = (Context) var1; } else { return null; } String initBT = SharedPrefsUtils.getInitBT(context); LogHelper.d(LOG_TAG, "abc revicer start,initBT:" + initBT); if (!"0".equals(initBT)) { return new Object(); } LogHelper.d(LOG_TAG, "abc reciver continue"); return null; }
Android studio重新打包,生成新的jar
