Android开发经验谈Android开发Android技术知识

Android Sentry接入

2019-01-18  本文已影响13人  蓝库知识

废话不多说,直接上代码

实现方式

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

官方材料

喵印~~~

上一篇下一篇

猜你喜欢

热点阅读