Matrix工具抓取ANR

接着上一篇文章ANR的处理分析,这里来整理一下Matrix工具的使用。有不对的地方,请大家指出来

1.Matrix 简介:

Matrix 是一款微信研发并日常使用的应用性能接入框架,支持iOS, macOS和Android。 Matrix 通过接入各种性能监控方案,对性能监控项的异常数据进行采集和分析,输出相应的问题分析、定位与优化建议,从而帮助开发者开发出更高质量的应用。

2.使用说明:

​ Matrix-android 当前监控范围包括:应用安装包大小,帧率变化,启动耗时,卡顿,慢方法,SQLite 操作优化,文件读写,内存泄漏等等。

2.1配置Matrix版本:

在gradle.properties目录下配置Matrix版本

MATRIX_VERSION=2.1.0

2.2 AGP8.0.2导入依赖

由于我是最新稳定版的Studio,AGP版本为8.0以上,所以这里有两种配置方式,后面讲解AGP8.0新的依赖配置方式和改变,这里就直接上代码.

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation group: "com.tencent.matrix", name: "matrix-android-lib", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-android-commons", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-trace-canary", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-io-canary", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-memory-canary", version: MATRIX_VERSION, changing: true
    implementation 'com.blankj:utilcodex:1.31.1'
}

 2.3 AGP8.0以下导入依赖

apply plugin: 'com.android.application'
apply plugin: 'com.tencent.matrix-plugin'

    matrix {
        trace {
            enable = true    //if you don't want to use trace canary, set false
            baseMethodMapFile = "${project.buildDir}/matrix_output/Debug.methodmap"
            blackListFile = "${project.projectDir}/matrixTrace/blackMethodList.txt"
        }
    }

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation group: "com.tencent.matrix", name: "matrix-android-lib", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-android-commons", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-trace-canary", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-io-canary", version: MATRIX_VERSION, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-memory-canary", version: MATRIX_VERSION, changing: true
    implementation 'com.blankj:utilcodex:1.31.1'
}

2.4 项目的build.gradle配置

buildscript {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'https://repo1.maven.org/maven2/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath ("com.tencent.matrix:matrix-gradle-plugin:${MATRIX_VERSION}") { changing = true }
    }
}
plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
}

3.MatrixUtils工具类

package com.example.matrixdemo.utils;

import android.app.Application;

import com.blankj.utilcode.util.LogUtils;
import com.example.matrixdemo.impl.MatrixDynamicConfigImpl;
import com.example.matrixdemo.plugin.MatrixPluginListener;
import com.tencent.matrix.Matrix;
import com.tencent.matrix.iocanary.IOCanaryPlugin;
import com.tencent.matrix.iocanary.config.IOConfig;
import com.tencent.matrix.memory.canary.MemoryCanaryPlugin;
import com.tencent.matrix.trace.TracePlugin;
import com.tencent.matrix.trace.config.TraceConfig;


public class MatrixUtils {
    private static volatile MatrixUtils INSTANCE;
    private static final String TAG = "MatrixLog";

    private MatrixUtils() {
    }

    public static MatrixUtils getInstance() {
        if (INSTANCE == null) {
            synchronized (MatrixUtils.class) {
                if (INSTANCE == null) {
                    INSTANCE = new MatrixUtils();
                }
            }
        }
        return INSTANCE;
    }

    public void initPlugin(Application application, String splashActivity) {
        Matrix.Builder builder = new Matrix.Builder(application); // build matrix
        builder.pluginListener(new MatrixPluginListener(application)); // add general pluginListener
        MatrixDynamicConfigImpl matrixDynamicConfig = new MatrixDynamicConfigImpl(); // dynamic config
        boolean fpsEnable = matrixDynamicConfig.isFPSEnable();
        boolean traceEnable = matrixDynamicConfig.isTraceEnable();
        //Trace plugin
        TraceConfig traceConfig = new TraceConfig.Builder()
                .dynamicConfig(matrixDynamicConfig)
                .enableFPS(fpsEnable)//帧率
                .enableEvilMethodTrace(traceEnable)//慢方法
                .enableAnrTrace(traceEnable)//anr
                .enableStartup(traceEnable)//启动速度
                .splashActivities(splashActivity)//首页
                //debug模式
                .isDebug(true)
                //dev环境
                .isDevEnv(false)
                .build();

        TracePlugin tracePlugin = new TracePlugin(traceConfig);
        builder.plugin(tracePlugin);

        MemoryCanaryPlugin memoryCanaryPlugin = new MemoryCanaryPlugin();
        builder.plugin(memoryCanaryPlugin);

        // io plugin
        IOCanaryPlugin ioCanaryPlugin = new IOCanaryPlugin(new IOConfig.Builder()
                .dynamicConfig(matrixDynamicConfig)
                .build());
        builder.plugin(ioCanaryPlugin);

        //init matrix
        Matrix.init(builder.build());
        tracePlugin.start();
    }
}

4.自定义Matrix动态配置接口

package com.example.matrixdemo.impl;

import com.tencent.mrs.plugin.IDynamicConfig;

public class MatrixDynamicConfigImpl implements IDynamicConfig {
    public MatrixDynamicConfigImpl() {}

    public boolean isFPSEnable() { return true;}
    public boolean isTraceEnable() { return true; }
    public boolean isMatrixEnable() { return true; }
    public boolean isDumpHprof() {  return false;}

    @Override
    public String get(String key, String defStr) {
        return defStr;
    }

    @Override
    public int get(String key, int defInt) {
        return defInt;
    }

    @Override
    public long get(String key, long defLong) {
        return defLong;
    }

    @Override
    public boolean get(String key, boolean defBool) {
        return defBool;
    }

    @Override
    public float get(String key, float defFloat) {
        return defFloat;
    }
}

5.自定义插件事件监听

package com.example.matrixdemo.plugin;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.tencent.matrix.plugin.DefaultPluginListener;
import com.tencent.matrix.report.Issue;
import com.tencent.matrix.util.MatrixLog;

public class MatrixPluginListener extends DefaultPluginListener {
    public static final String TAG = "MatrixPluginListener";

    public MatrixPluginListener(Context context) {
        super(context);
    }

    @Override
    public void onReportIssue(Issue issue) {
        super.onReportIssue(issue);
        //todo 处理性能监控数据
        MatrixLog.e(TAG, issue.toString());
        LogUtils.e(TAG, issue.toString());
    }
}

6.Matrix初始化

package com.example.matrixdemo.app;

import android.app.Application;

import com.example.matrixdemo.utils.MatrixUtils;


public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initMatrix();
    }

    private void initMatrix() {
        MatrixUtils.getInstance().initPlugin(this,"com.example.matrixdemo.MainActivity;");
    }
}

7.简单使用

private void testThreadAnr() {
    try {
        int number = 0;
        while (number++ < 5) {
            LogUtils.e(TAG, "主线程睡眠导致的ANR:次数" + number + "/5");
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                LogUtils.e(TAG, "异常信息为:" + e.getMessage());
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
        LogUtils.e(TAG, "异常信息为:" + e.getMessage());
    }
}

Matrix会生成如下log

2024-01-29 11:33:27.328 6278-6317/com.goyu.newdfhmi E/Matrix.AnrTracer: happens lag : java.lang.Thread.sleep(Native Method)
    java.lang.Thread.sleep(Thread.java:440)
    java.lang.Thread.sleep(Thread.java:356)
    com.goyu.newdfhmi.MainActivity.testThreadAnr(MainActivity.java:120)
    com.goyu.newdfhmi.MainActivity.onResume(MainActivity.java:104)
    android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
    android.app.Activity.performResume(Activity.java:7962)
    android.app.ActivityThread.performResumeActivity(ActivityThread.java:4196)
    android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4238)
    android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
    android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
    android.os.Handler.dispatchMessage(Handler.java:107)
    android.os.Looper.loop(Looper.java:214)
    android.app.ActivityThread.main(ActivityThread.java:7397)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
    , scene : com.goyu.newdfhmi.MainActivity 

 

8.源码地址

Matrixdemo: 微信性能监控框架Matrix的简单使用

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值