廢話不多說,直接上代碼
實現方式
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
}
位置如圖:
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
喵印~~~