Overview

App Performance Management (APM) of AppGallery Connect provides minute-level app performance monitoring capabilities. With the APM SDK, you can quickly integrate APM without coding. APM allows you to view and analyze app performance data collected by APM in AppGallery Connect to comprehensively understand the performance features of your app, helping you quickly and accurately rectify possible performance problems and continuously improve user experience.

What You Will Create

In this codelab, you will build an app that integrates APM. In addition, you can manually trigger ANR events to test whether APM can monitor these events and learn how to view and analyze ANR reports.

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

A device running Android 4.4 or a later version

To integrate AppGallery Connect APM, you must complete the following preparations:

For details, please refer to Integration Preparations.
  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card and select the app for which you want to enable APM from the app drop-down list on the top.
  3. Go to Quality > APM. If APM is not enabled, click Enable.

Enabling APM

  1. Add the AppGallery Connect plugin of 1.6.2.300 or a later version to the project-level build.gradle file in Android Studio:
  2. buildscript { repositories { // Add the Maven repository. maven { url 'https://developer.huawei.com/repo/' } } dependencies { // To benefit from the latest APM features, update your Android gradle plugin. // Dependency to version 3.5.3 or later. classpath 'com.android.tools.build:gradle:3.5.3' // Add the AppGallery Connect plugin dependence and update its version to 1.6.2.300. classpath 'com.huawei.agconnect:agcp:1.6.2.300' } }
  3. Enable APM in the app-level build.gradle file in Android Studio:
    apply plugin: 'com.android.application' // Apply the AppGallery Connect plugin. apply plugin: 'com.huawei.agconnect' // Set enableAPMS to true, indicating that the APMS plugin has taken effect. agcp { enableAPMS true } android { //.. }

Integrating the APM SDK

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

  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card and select an app from the app drop-down list on the top.
  3. Go to Project settings > General information and download agconnect-services.json under App information.
  4. Copy the agconnect-services.json file to your app's module directory.
  5. Open the app-level build.gradle file and add the following code to integrate the APM SDK:
    dependencies { // Add the APM SDK library dependency. implementation 'com.huawei.agconnect:agconnect-apms:1.5.2.307' }
  6. Find the app-level proguard-rules.pro file and exclude the following items from obfuscation:
    -keep class com.huawei.agconnect.apms.**{*;} -dontwarn com.huawei.agconnect.apms.** -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;} -keep interface com.huawei.hms.analytics.type.HAEventType{*;} -keep interface com.huawei.hms.analytics.type.HAParamType{*;} -keepattributes Exceptions, Signature, InnerClasses, LineNumberTable
  7. 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 one Trigger ANR button to trigger an ANR event.

For details about the layout, please refer to the following code in the activity_main.xml file:

<?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/anr_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Trigger ANR" /> </LinearLayout> </LinearLayout>

You can manually trigger an ANR event and check whether the ANR data is normal on the performance management page of APM.

  1. Add the following tap event in the onCreate method, and click Trigger ANR.
    Button triggeranrBtn = findViewById(R.id.anr_test); triggeranrBtn.setOnClickListener(v -> { Log.d("apmsAndroidDemo", "trigger ANR"); anrTestEnable = true; });
  2. To simulate an ANR error, add the following code to the outer layer of the onCreate method in the MainActivity.java file.
    private boolean anrTestEnable = false; @Override public boolean dispatchKeyEvent(KeyEvent event) { if (anrTestEnable) { try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } } return super.dispatchKeyEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (anrTestEnable) { try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } } return super.dispatchTouchEvent(ev); }
  3. Tap the Trigger ANR button for several times until the pop-up indicating ANR is displayed. Then, close the app and open it again.

Checking the Overview

  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card and select an app from the app drop-down list on the top.
  3. Go to Quality > APM.
  4. On the Overview tab page, you can check the issues and indicator overview in the past 30 days for your app. Here, an ANR error has been reported.

Viewing ANR Data

  1. Click the ANR analysis tab. The ANR report is displayed. This page displays the ANR trend information, including top 5 problems in last 24 hours (sorted by affected-user rate), number of ANR occurrences, number of affected users, and affected-user rate distribution in different dimensions such as time, device model, OS version, and app version, as well as the list of ANR errors categorized by type.
  2. Click View details of a type of problems from the problem list. The ANR analysis details page of this type of problems is displayed, containing metrics such as Affected user rate/ANR rate trend over time, Affected users/Occurrences trend over time, and affected-user count distribution by device model, app version, and OS version.
  3. Click View details next to a record in the Records list to view the details of the record.

You have successfully created an app integrated with the APM service of AppGallery Connect and learned how to trigger an ANR event and how to view and analyze ANR data in AppGallery Connect.

API Reference

Sample code

Code copied