切换菜单
搜索
个人笔记云
首页
java
spring
springmvc
python
使用教程
笔记管理
搜索
登录/注册
好物分享
退出
搜索
android 性能分析(优化)-利用AOP技术,字节码方法插桩,实现 android 耗时统计
2021-10-24
922
**参考视频教程:** [**Top团队大牛带你玩转Android性能分析与优化 **](http://www.notescloud.top/goods/detail/1409) 在android开发的过程中,不管是开发SDK还是开发App,我们时常有这么一个需求,性能优化,或者卡顿优化,通常进行这项工作的时候,我们可能会凭着主观的一些观点,去定位排查某一部分代码,常用的手段就是在可疑代码前后加时间戳相减来手动统计,或者在运行的过程中,通过看android 自带的性能分析工具来进行优化。但这种方式难免会有疏漏,而且每段代码的执行过程也是不容易追踪的。 今天就来给大家介绍一个,利用AOP技术,字节码编译期方法插桩的方式,统计android耗时的工具。 github地址:[https://github.com/miqt/PluginTools](https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fmiqt%2FPluginTools) 这是一个android 方法耗时统计打印插件,可以根据包名,类名,方法正则表达式等,指定插入代码进行统计,在做性能调优的时候非常方便。能清晰的观察每个方法的耗时情况。另外还可以自定义统计实现类,增加额外的内容。 [\[图片上传失败...(image-93a384-1584524975350)\]](https://links.jianshu.com/go?to=https%3A%2F%2Fwww.apache.org%2Flicenses%2FLICENSE-2.0)[\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JP6etkK5-1584524741926)(https://api.bintray.com/packages/miqingtang/maven/pluginSrc/images/download.svg)\]](https://links.jianshu.com/go?to=https%3A%2F%2Fbintray.com%2Fmiqingtang%2Fmaven%2FpluginSrc) 效果展示: ----- 统计项目中所有activity的方法耗时情况: ``` 2020-03-18 16:09:11.540 23078-23078/? D/TimePrint: ╔====================================================================================== ║[Thread]:main ║[Method]:androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() type:void ║[Time]:84 ╚====================================================================================== 2020-03-18 16:09:11.575 23078-23078/? I/TimePrint: ╔====================================================================================== ║[Thread]:main ║[Method]:androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) type:void ║[Time]:118 ╚====================================================================================== 2020-03-18 16:09:11.575 23078-23078/? I/TimePrint: ╔====================================================================================== ║[Thread]:main ║[Method]:androidx.appcompat.app.AppCompatActivity.setContentView(int) type:void ║[Time]:118 ╚====================================================================================== 2020-03-18 16:09:11.575 23078-23078/? I/TimePrint: ╔====================================================================================== ║[Thread]:main ║[Method]:com.miqt.plugindemo.MainActivity.onCreate(android.os.Bundle) type:void ║[Time]:142 ╚====================================================================================== ``` 可以看出,这样的话方法名,运行线程,和耗时情况就都一目了然啦。 也支持注解指定固定的方法 @PrintTime: ``` @PrintTime public static String getStr() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "hello"; } ``` 使用方法: ----- 项目根目录:build.gradle 添加以下代码 ``` dependencies { classpath 'me.miqt.plugin.tools:pluginSrc:x.x.x' } ``` 对应 module 中启用插件,可以是`application`也可以是`library` ``` apply plugin: 'miqt.plugin.tools' methodtimer { enable = true //是否启用 //项目中的class true:全部插桩 false:只有注解插桩 all = true //指定插桩那些外部引用的jar,默认空,表示只对项目中的class插桩 jarRegexs = [".*androidx.*"] //指定插桩那些类文件,默认空 classRegexs = [".*view.*"] //所有包含 on 的方法,所有构造方法 methodRegexs = [".*on.*", ".*init.*"] //编译时是否打印log log = true //是否用插桩后的jar包替换项目中的jar包,慎用 replaceJar = false //是否生成详细mapping文件 mapping = true //自定义方法统计实现类,不指定默认使用自带实现方式 //impl = "com.miqt.plugindemo.MyTimeP" } ``` 添加类库依赖: ``` dependencies { implementation 'me.miqt.plugin.tools:pluginlib:x.x.x' } ```
教程分类
热门视频教程
热门文章
热门书籍推荐