Android 应用接入 Firebase Crashlytics 进行崩溃分析上报

前言

所在公司的项目中有一款应用应客户要求,需要接入 Firebase Crashlytics,在此提前练手,也做个总结。本文以最新的 Gradle 7.5 为例,如果 Gradle 版本比较低,添加依赖那一章节可参考官方文档。

1. 登录 Firebase 控制台,创建项目及应用

  • 输入项目名称,点击继续
    image
  • 勾选 Google Analytics,点击继续
    image
  • 选择默认的 Google Analytics 账号,点击创建项目
    image
  • 等待创建完成
    image

2. 在控制台添加 Android 应用

  • 点击跳转页面的安卓机器人,跳转添加应用界面
    image

  • 输入要接入的应用包名和应用别名,点击注册应用按钮
    image

  • 点击下载配置文件按钮,并将其添加到项目的模块级别目录
    image

    类似如下这样:

    image

3. 打开项目,在 IDE 中添加依赖

  • 定位到项目级别目录,在build.gradle中添加如下两行:
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { // .... some others plugins id 'com.google.gms.google-services' version '4.3.15' apply false id 'com.google.firebase.crashlytics' version '2.9.2' apply false }
  • 定位到模块级别目录,在build.gradle中添加如下几行:
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
plugins { // .... some others plugins id 'com.google.gms.google-services' id 'com.google.firebase.crashlytics' } // .... some others closure dependencies { // .... some others dependencies implementation platform('com.google.firebase:firebase-bom:31.2.2') implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-crashlytics' }

4. 构造 Crash 代码,验证功能

  • 在应用的 Activity 中添加如下点击事件:
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn); btn.setOnClickListener(view -> { throw new RuntimeException("Test Crash"); }); } }
  • 在 Activity 对应的布局文件中添加按钮
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:text="test crash" android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
  • 在 AndroidMenifest.xml 添加联网权限
复制代码
  • 1
<uses-permission android:name="android.permission.INTERNET" />
  • 打包安装

最后点击按钮触发 Crash。多触发几次,即可在 Firebase 控制台看到报告分析。

image

posted @   wx2020  阅读(1687)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开