废话不多说,直接上代码
实现方式
1.配置gradle
api 'io.sentry:sentry-android:1.7.16'
2.工具类
public class SentryUtils {
//初始化sentry
public static void init(Context context) {
String sentryDsn =“你的dsn”;
Sentry.init(sentryDsn, new AndroidSentryClientFactory(context));
}
//主动发送Throwable消息
public static void sendSentryExcepiton(Throwable throwable) {
Sentry.capture(throwable);
}
//主动发送Event消息
public static void sendSentryExcepiton(Event throwable) {
Sentry.capture(throwable);
}
//主动发送EventBuilder消息
public static void sendSentryExcepiton(EventBuilder throwable) {
Sentry.capture(throwable);
}
public static void sendSentryExcepiton(String logger, Throwable throwable) {
SentryUtils.sendSentryExcepiton(new EventBuilder().withMessage("try catch msg").withLevel(Event.Level.WARNING).withLogger(logger).withSentryInterface(new ExceptionInterface(throwable)));
}
}
配置ProGuard
1>在gradle.properties文件中加入
android.enableR8=false
2>在app/build.gradle
apply plugin: 'io.sentry.android.gradle'
和
sentry {
// Disables or enables the automatic configuration of proguard
// for Sentry. This injects a default config for proguard so
// you don't need to do it manually.
autoProguardConfig true
// Enables or disables the automatic upload of mapping files
// during a build. If you disable this you'll need to manually
// upload the mapping files with sentry-cli when you do a release.
autoUpload true
}
位置如图:
app/build.gradle
3>在project/build.gradle中加入
buildscript {
dependencies {
classpath 'io.sentry:sentry-android-gradle-plugin:1.7.16'
}
}
4>新建一个sentry.properties文件添加,具体数据跟后台要
defaults.project=your-project
defaults.org=your-org
auth.token=YOUR_AUTH_TOKEN
注意事项
1.sentry初始化完成后就可以收到界面崩溃的消息,但是你也可以手动发送错误消息,详看工具类
2.ProGuard的配置主要是处理混淆的,我这里用的是自动集成的方式,不用每次都手动上传mapping,但是每个版本只能自动上传一次mapping
添加自定义参数
思路很简单,就是找到官方在哪里添加的,直接也在那里添加就好了
方法:
在初始化过程中可以看到AndroidSentryClientFactory这个类
AndroidSentryClientFactory:
@Override
public SentryClient createSentryClient(Dsn dsn) {
。。。。。。
SentryClient sentryClient = super.createSentryClient(dsn);
sentryClient.addBuilderHelper(new AndroidEventBuilderHelper(ctx));
return sentryClient;
}
注意AndroidEventBuilderHelper这个类,打开有惊喜
private Map<String, Map<String, Object>> getContexts() {
Map<String, Map<String, Object>> contexts = new HashMap<>();
Map<String, Object> deviceMap = new HashMap<>();
Map<String, Object> osMap = new HashMap<>();
Map<String, Object> appMap = new HashMap<>();
contexts.put("os", osMap);
contexts.put("device", deviceMap);
contexts.put("app", appMap);
// Device
deviceMap.put("manufacturer", Build.MANUFACTURER);
deviceMap.put("brand", Build.BRAND);
deviceMap.put("model", Build.MODEL);
deviceMap.put("family", getFamily());
deviceMap.put("model_id", Build.ID);
deviceMap.put("battery_level", getBatteryLevel(ctx));
deviceMap.put("orientation", getOrientation(ctx));
deviceMap.put("simulator", IS_EMULATOR);
deviceMap.put("arch", Build.CPU_ABI);
deviceMap.put("storage_size", getTotalInternalStorage());
deviceMap.put("free_storage", getUnusedInternalStorage());
deviceMap.put("external_storage_size", getTotalExternalStorage());
deviceMap.put("external_free_storage", getUnusedExternalStorage());
deviceMap.put("charging", isCharging(ctx));
deviceMap.put("online", isConnected(ctx));
DisplayMetrics displayMetrics = getDisplayMetrics(ctx);
if (displayMetrics != null) {
int largestSide = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
int smallestSide = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
String resolution = Integer.toString(largestSide) + "x" + Integer.toString(smallestSide);
deviceMap.put("screen_resolution", resolution);
deviceMap.put("screen_density", displayMetrics.density);
deviceMap.put("screen_dpi", displayMetrics.densityDpi);
}
ActivityManager.MemoryInfo memInfo = getMemInfo(ctx);
if (memInfo != null) {
deviceMap.put("free_memory", memInfo.availMem);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
deviceMap.put("memory_size", memInfo.totalMem);
}
deviceMap.put("low_memory", memInfo.lowMemory);
}
// Operating System
osMap.put("name", "Android");
osMap.put("version", Build.VERSION.RELEASE);
osMap.put("build", Build.DISPLAY);
osMap.put("kernel_version", KERNEL_VERSION);
osMap.put("rooted", isRooted());
// App
PackageInfo packageInfo = getPackageInfo(ctx);
if (packageInfo != null) {
appMap.put("app_version", packageInfo.versionName);
appMap.put("app_build", packageInfo.versionCode);
appMap.put("app_identifier", packageInfo.packageName);
}
appMap.put("app_name", getApplicationName(ctx));
appMap.put("app_start_time", stringifyDate(new Date()));
return contexts;
}
是不是所有sentry显示的参数,直接在这里添加你想要的参数就好了。
重写以上两个类,完后后在初始化的时候直接用自定义的就可以了!
喵印~~~