Overview

The Crash service of AppGallery Connect is a lightweight crash analysis service, in which Huawei provides a Crash SDK that can be quickly integrated into your app without the need for coding. The SDK integrated into your app can automatically collect crash data and report the data to AppGallery Connect when your app crashes, helping you understand the version quality of your app, quickly locate the causes of crashes, and evaluate the impact scope of crashes.

What You Will Create

In this codelab, you will create an app integrated with the Crash service, use custom user IDs, logs, and key-value pairs to define your crash report, and intentionally create a crash event for your app to report. In this way, you will understand crash report details.

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

To integrate the Crash service of AppGallery Connect, you must complete the following preparations:

  1. Create an app in AppGallery Connect.
  2. Create an Android Studio project.
  3. Add the app package name.
  4. Add the AppGallery Connect plugin and Maven repository.
  5. Synchronize the project.
For details, please refer to Integration Preparations.
  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card, and select your app to enable the Crash service from the app drop-down list on the top.
  3. Go to Quality > Crash. The Crash page is displayed.

Adding the AppGallery Connect Configuration File of Your App

  1. In AppGallery Connect, click My projects.
  2. Click your project card, and select your app to integrate the Crash service from the app drop-down list on the top.
  3. Go to Project settings > General information and click agconnect-services.json under App information to download it.
  4. Copy the agconnect-services.json file to your app's module directory.

Adding Build Dependencies

  1. Open the app-level build.gradle file and add the following code to integrate the Crash SDK and Analytics SDK:
    dependencies { // Configure the following address: implementation 'com.huawei.agconnect:agconnect-crash:1.6.2.300' implementation 'com.huawei.hms:hianalytics:6.3.2.300' }
  2. Click Sync Now to synchronize the configuration.

In this codelab, you can create a layout page in your Android Studio project and design the UI according to the following figure, with two buttons required. The makeCrash and CustomReport buttons can be tapped to trigger a crash event and a custom report, respectively.

Sample code:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="300dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btn_crash" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="makeCrash" /> <Button android:id="@+id/btn_CustomReport" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="CustomReport" /> </LinearLayout> </LinearLayout>
  1. Tap makeCrash to trigger the AGConnectCrash.testIt method to create a crash.
    Button btn_crash = findViewById(R.id.btn_crash); btn_crash.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AGConnectCrash.getInstance().testIt(MainActivity.this); } });
  2. Tap CustomReport to trigger AGConnectCrash.setUserId to set a user ID, AGConnectCrash.log to customize logs, and AGConnectCrash.setCustomKey to customize key-value pairs.
    Button btn_CustomReport = findViewById(R.id.btn_CustomReport); btn_CustomReport.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AGConnectCrash.getInstance().setUserId("testuser"); AGConnectCrash.getInstance().log(Log.DEBUG,"set debug log."); AGConnectCrash.getInstance().log(Log.INFO,"set info log."); AGConnectCrash.getInstance().log(Log.WARN,"set warning log."); AGConnectCrash.getInstance().log(Log.ERROR,"set error log."); AGConnectCrash.getInstance().setCustomKey("stringKey", "Hello world"); AGConnectCrash.getInstance().setCustomKey("booleanKey", false); AGConnectCrash.getInstance().setCustomKey("doubleKey", 1.1); AGConnectCrash.getInstance().setCustomKey("floatKey", 1.1f); AGConnectCrash.getInstance().setCustomKey("intKey", 0); AGConnectCrash.getInstance().setCustomKey("longKey", 11L); } });
  3. Package and run your app. Tap CustomReport to generate a custom crash report, and then tap makeCrash to report the crash report.
  1. In AppGallery Connect, click My projects, click your project card, and select your app from the drop-down list on the top.
  2. On the Crash service page, click the Statistics tab to view the crash statistics of your app.
  3. Click the Search by user tab and check the crash report by user ID.
  4. Click a crash to view its details. On the details page that is displayed, you can click the Logs tab to check generated custom logs.
  5. Click the Status tab to view information about custom key-value pairs.

Congratulations! You have successfully built your first app that integrates the Crash service, and learned how to customize and view a crash report in AppGallery Connect. Alternatively, more forms of crash reports are waiting for you to try in AppGallery Connect. Welcome to create them there.

API Reference.

Sample code.

Code copied