前言 在日常业务测试中经常会发现页面跳转卡顿、滑动卡顿等卡顿问题,但是我们发现卡顿问题,有时候反馈给开发不一定得到很好的解决,因为开发需要看日志,而每次如果都去抓log,就会显得麻烦,而且需要搜索。会非常麻烦,如何解决了,成为老大难的问题了。 本文介绍如何使用BlockCanary测试app卡顿并且做到log日志上报. BlockCanary介绍 BlockCanary是一个Android平台的一个非侵入式的性能监控组件,应用只需要实现一个抽象类,提供一些该组件需要的上下文环境,就可以在平时使用应用的时候检测主线程上的各种卡慢问题,并通过组件提供的各种信息分析出原因并进行修复 原理: 使用教程 app目录的build . gradle 中 , 增加如下代码 implementation "com.github.markzhai:blockcanary-android:1.5.0" 如何使用 一般是在Application中的onCreate中注册 , BlockCanary.install(this,new AppBlockCanaryContext()).start(); 这里的AppBlockCanaryContext是继承了BlockCanaryContext 重写了里面的几个方法,具体代码如下: package com.example.studayapp; import android.content.Context; import com.github.moduth.blockcanary.BlockCanaryContext; import com.github.moduth.blockcanary.internal.BlockInfo; import java.io.File; import java.util.LinkedList; import java.util.List; /** * @Description * @auther VTester * @create 2020-10-11 10:08 */ public class AppBlockCanaryContext extends BlockCanaryContext { public String provideQualifier() { //获取应用版本信息 return "unknown"; } /** * Implement in your project. * * @return user id */ public String provideUid() { //获取用户uid return "uid"; } /** * Network type * * @return {@link String} like 2G, 3G, 4G, wifi, etc. */ public String provideNetworkType() { //获取网络类型 return "unknown"; } /** * Config monitor duration, after this time BlockCanary will stop, use * with {@code BlockCanary}"s isMonitorDurationEnd * * @return monitor last duration (in hour) */ public int provideMonitorDuration() { //设置监控时长 return -1; } /** * Config block threshold (in millis), dispatch over this duration is regarded as a BLOCK. You may set it * from performance of device. * * @return threshold in mills */ public int provideBlockThreshold() { //设置监控卡顿阀值 return 1000; } /** * Thread stack dump interval, use when block happens, BlockCanary will dump on main thread * stack according to current sample cycle. ** Because the implementation mechanism of Looper, real dump interval would be longer than * the period specified here (especially when cpu is busier). * * * @return dump interval (in millis) */ public int provideDumpInterval() { return provideBlockThreshold(); } /** * Path to save log, like "/blockcanary/", will save to sdcard if can. * * @return path of log files */ public String providePath() { //设置log保存地址 return "/blockcanary/"; } /** * If need notification to notice block. * * @return true if need, else if not need. */ public boolean displayNotification() { //设置是否在桌面展示, return false; } /** * Implement in your project, bundle files into a zip file. * * @param src files before compress * @param dest files compressed * @return true if compression is successful */ public boolean zip(File[] src, File dest) { return false; } /** * Implement in your project, bundled log files. * * @param zippedFile zipped file */ public void upload(File zippedFile) { throw new UnsupportedOperationException(); } /** * Packages that developer concern, by default it uses process name, * put high priority one in pre-order. * * @return null if simply concern only package with process name. */ public List
concernPackages() { return null; } /** * Filter stack without any in concern package, used with @{code concernPackages}. * * @return true if filter, false it not. */ public boolean filterNonConcernStack() { return false; } /** * Provide white list, entry in white list will not be shown in ui list. * * @return return null if you don"t need white-list filter. */ public List provideWhiteList() { LinkedList whiteList = new LinkedList<>(); whiteList.add("org.chromium"); return whiteList; } /** * Whether to delete files whose stack is in white list, used with white-list. * * @return true if delete, false it not. */ public boolean deleteFilesInWhiteList() { return true; } /** * Block interceptor, developer may provide their own actions. */ public void onBlock(Context context, BlockInfo blockInfo) { } } 需要在项目的配置权限 根据上面的配置代码并打包app,进行手工测试,如果主线程卡顿超过1000ms,会产生卡顿日志。 在我们执行中,日志的位置 遇到卡顿就会将日志记录 我们看下日志的详细的信息 卡顿文件包含几点: 发生时间 版本 imei cpu型号 内存 卡顿堆栈 这是我们的日志的文件,这样是存储在手机里,我们还可以实现一个上传功能,去上传我们的发现的日志。