Android简单集成sentry-android:4.3.0做异常监控

我是在集团已经搭建好私有化Sentry服务的情况下做客户端SDK集成,根据Android | Sentry Documentation官方文档配置两个build.gradle。

// 在项目的build.gradle的repositories节点下加入mavenCentral仓库的引用
repositories {
    mavenCentral()
}

// 在app的build.gradle中配置使用jdk1.8
android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

// 在app的build.gradle中加入sentry-android的依赖
dependencies {
    implementation 'io.sentry:sentry-android:4.3.0'
}

然后在AndroidManifest.xml的application节点下加入DSN配置。

<application>
  <meta-data android:name="io.sentry.dsn" android:value="项目的DSN" />
</application>

项目DSN在管理后台的【设置-项目】页面选择你的项目,进入项目的设置页面后,选择Client Keys(DSN)就可以看到DSN值。

按照官方文档的说明,此时已经可以正常上传崩溃日志了,但不知道是不是私有化的问题,我这里无论如何修改,使用自动初始化、手动初始化等都无法上报异常,且Log显示Sentry组件无法访问外部存储,这个提示误导了我,花了半天时间在找这块的原因,后来放弃挣扎,继续完成ProGuard配置后,居然就能正常上报了······

Sentry关于ProGuard的配置,官方文档只说了大概,有些细节还需要注意。首先在项目的build.gradle中加入sentry-android-gradle-plugin的配置。

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        // https://github.com/getsentry/sentry-android-gradle-plugin/releases
        classpath 'io.sentry:sentry-android-gradle-plugin:1.7.36'
    }
}

然后在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.

    // Only enable it if you are using sentry-android <= v1.7,
    // sentry-android >= v2.0 already does it automatically.
    autoProguardConfig false

    // 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

    // Disables or enables the automatic configuration of Native Symbols
    // for Sentry. This executes sentry-cli automatically so
    // you don't need to do it manually.
    // Default is disabled.
    uploadNativeSymbols false

    // Does or doesn't include the source code of native code for Sentry.
    // This executes sentry-cli with the --include-sources param. automatically so
    // you don't need to do it manually.
    // Default is disabled.
    includeNativeSources false
}

配置好两个gradle后,还需要在项目根目录新建sentry.properties,内容为

defaults.project=项目名
defaults.org=组织名,是组织id,不是显示的名称
defaults.url=这里官方没说,因为只有私有化服务需要配置,值就是私有化服务的ip\host:port
auth.token=这个token是管理后台的个人账号的设置页里的API下的Auth Tokens中,如果没有就新建一个,配置就用默认的即可

至此,已经可以正常使用异常上报了,关于手动初始化和配置,可以查看官方文档说明,大致就是AndroidManifest.xml中的application节点下加入

<meta-data android:name="io.sentry.auto-init" android:value="false" />

然后就可以在自定义Application类的onCreate中手动配置初始化

SentryAndroid.init(this) {
            it.setDebug(true)
            it.cacheDirPath = externalCacheDir?.absolutePath ?: cacheDir.absolutePath // 默认就是cacheDir,即data\data\包名\cache
            it.environment = "test" // 环境标识,如生产环境、测试环境,随便自定义字符串
            it.beforeSend = SentryOptions.BeforeSendCallback { event, hint ->
                // BeforeSendCallback主要就是上传前的拦截器,比如设置debug数据不上报等,具体看需求
                return@BeforeSendCallback if (event.level == SentryLevel.DEBUG) null else event
            }
        }

还有很多自定义配置,可以根据项目需求去探索。

PS:如果服务器用的http协议,而设备是9.0及以上版本,则需要在application标签配置允许http请求。

PPS:DSN是服务器配置生成的,如果服务端配了内网地址和端口,客户端这边需要注意将ip:port那一截改成外网地址和端口,或者域名。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容