接着上一篇文章ANR的处理分析,这里来整理一下Matrix工具的使用。有不对的地方,请大家指出来
Matrix 是一款微信研发并日常使用的应用性能接入框架,支持iOS, macOS和Android。 Matrix 通过接入各种性能监控方案,对性能监控项的异常数据进行采集和分析,输出相应的问题分析、定位与优化建议,从而帮助开发者开发出更高质量的应用。
Matrix-android 当前监控范围包括:应用安装包大小,帧率变化,启动耗时,卡顿,慢方法,SQLite 操作优化,文件读写,内存泄漏等等。
在gradle.properties目录下配置Matrix版本
MATRIX_VERSION=2.1.0
由于我是最新稳定版的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'
}
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'
}
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
}
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();
}
}
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;
}
}
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());
}
}
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;");
}
}
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());
}
}
Matrixdemo: 微信性能监控框架Matrix的简单使用