What Is the Crash Service of AppGallery Connect?

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 that can integrate the Crash service and manually trigger a crash to test the Crash service.
You can also customize your crash report to suit your service by configuring custom user IDs, recording custom logs, and defining custom key-value pairs.

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:

For details, please refer to Configuring App Information in AppGallery Connect.

Enabling HUAWEI Analytics

The Crash service uses capabilities of HUAWEI Analytics when reporting crash events. Therefore, you must enable HUAWEI Analytics before integrating the Crash SDK. For details, please refer to Service Enabling.

Integrating the Crash SDK

If you are using Android Studio, you need to integrate the Crash SDK into your Android Studio project before development.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project from the project list and click the app for integration on the project card.
  3. Go to Project Settings > General information, and click agconnect-services.json under App information to download the configuration file.
  4. Copy the agconnect-services.json file to the app's root directory of your Android Studio project.
  5. Open the app-level build.gradle file and add the following code to integrate HUAWEI Analytics Kit and the Crash SDK:
    dependencies { // Configure the following address: implementation 'com.huawei.hms:hianalytics:' implementation 'com.huawei.agconnect:agconnect-crash:' }
  6. Click Sync Now to synchronize the configuration.

You can create a page in your Android Studio project and design the UI according to the following figure, with three buttons required: One button is used to trigger crashes, one button is used to report exceptions, and the left one is used to trigger the generation of a custom report.

In this codelab, you can test the Crash service by calling the API of the Crash SDK to trigger a crash and then checking whether the crash data is reported in AppGallery Connect. The procedure is as follows:

  1. Add three buttons, namely makeCrash, CustomReport, and makeException, to the activity layout file of your app.
    <?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_exception" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="makeException" /> <Button android:id="@+id/CustomReport" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="CustomReport" /> </LinearLayout> </LinearLayout>
  2. Add the code for calling the AGConnectCrash.testIt method to trigger a crash when the makeCrash button is tapped.
    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); } });
  3. Click makeException to call AGConnectCrash. The recordException method will generate a non-fatal exception event.
    Button btn_exception = findViewById(R.id.btn_exception); btn_exception.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try{ throw new Exception(); }catch (Exception e){ AGConnectCrash.getInstance().recordException(e); } } });
  4. Add the code for calling AGConnectCrash.setUserId to define a custom user ID, calling AGConnectCrash.log to record custom logs, and calling AGConnectCrash.setCustomKey to define a custom key-value pair when the CustomReport button is tapped.
    findViewById(R.id.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); } });
  5. Package and run the app. Then tap CustomReport to generate a custom crash report, tap makeException to trigger a non-fatal exception event, tap makeCrash to trigger a crash, and re-launch the app.
  1. Sign in to AppGallery Connect and click My projects.
  2. Click Add filter and select Exception under User attribute > Event type. This filter is not selected upon your first access by default. Click the Statistics tab and select Last hour to view crash and exception statistics of your app. You can trigger multiple crashes or exceptions and view the real-time statistics in AppGallery Connect. The recorded exceptions will be reported upon the next app launch.
  3. If crash and exception data was reported, click the Problems tab, select Last hour, and find the crash and exception of your app. For example, in the following figure, java.lang.NullPointerException is the crash triggered during the test, and java.lang.Exception is the exception triggered during the test.
  4. Click the crash name to go to the crash details page. Click the Stack tab. The possible causes of the crash are displayed, helping you analyze the crash.
  1. Click the Search by user tab and check the crash report by user ID.
  2. 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.
  3. Click the Status tab to view information by custom key-value pair.

You have successfully created an app integrated with the Crash service of AppGallery Connect and learned how to view and analyze crashes and custom crash reports in AppGallery Connect. You can trigger different types of crashes and view the crashes in AppGallery Connect.

For details about APIs related to the Crash service of AppGallery Connect, please refer to API Reference.
To download the sample code, please click the button below:

Download source code

Code copied