Sentry android

软件出Bug是不可避免的,但要有办法追踪这些Bug。这样才能不断迭代完善软件。Android app bug追踪我用过友盟,Bugly,Sentry。综合对比,Sentry具有接入更方便,UI简洁专业,可以私有化部署的优点。

集成

集成SDK依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Top-level build file 
// ADD JCENTER REPOSITORY
dependencies {
classpath 'io.sentry:sentry-android-gradle-plugin:1.7.33'
}
repositories {
jcenter()
}


// modules build files
// ADD COMPATIBILITY OPTIONS TO BE COMPATIBLE WITH JAVA 1.8
apply plugin: 'io.sentry.android.gradle'
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}


// ADD SENTRY ANDROID AS A DEPENDENCY
dependencies {
implementation "io.sentry:sentry-android:2.0.2"
}

链接SDK到Sentry(自动)

1
2
3
<application>
<meta-data android:name="io.sentry.dsn" android:value="https://[email protected]/1704083" />
</application>

链接SDK到Sentry(手动)

1
2
3
4
5
6
7
8
// 一般在application里init
SentryAndroid.init(context) {
it.dsn = sentryUrl
it.environment = environment
it.release = releaseVersion
it.addInAppExclude("java.")
it.addInAppInclude("io.sentry")
}

小项目直接用自动配置就可以,手动配置可以控制dsn,并且在初始化时传一些自定义字段。

测试

1
2
3
4
5
6
7
8
9
10
public class MyActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
throw new Exception("This is a test.");
} catch (Exception e) {
Sentry.captureException(e);
}
}
}

这时候应该可以在Sentry dashboard上看到一个issue:

开发环境已经可以开始玩耍了。在项目上线后,App打包会进行混淆,这时堆栈信息也是混淆的,有两个办法还原:1,用sdk带的proguard和Release时产生mapping文件还原堆栈信息。2,用Sentry的自动上传功能上传mapping文件,自动反混淆。

设置自动上传

先在项目根目录下创建一个sentry.properties文件:

1
2
3
defaults.project=your-project
defaults.org=your-org
auth.token=YOUR_AUTH_TOKEN

在module的build文件里加上

1
2
3
4
5
6
7
8
9
10
11
12
13
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
}

lintOptions {}

Auth.token 可以在这里找到,没有的话创建一个。

设置NDK符号文件

在sentry-android 2.0之后是可以捕获Native crash的,不过信息是很不直观的:


这事需要我们上传一些符号文件来反混淆堆栈信息,命令如下:

1
2
3
// --url 是可选的,如果没有私有化部署不需要
sentry-cli --url https://sentry.xxxlabs.io/ login
sentry-cli upload-dif -o xxxlabs -p xxxlabs-android-dev /Users/hyjfine/Work/android/xxxlab-ffmpeg/app/build/intermediates/merged_native_libs/release

这时候可以看到JNI函数名称了:

安装sentry-cli

参考

https://sentry.io/for/android/

https://docs.sentry.io/platforms/android/migrate/