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.
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.
A device running Android 4.4 or a later version
To integrate AppGallery Connect APM, you must complete the following preparations:
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'
}
}
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 {
//..
}
If you are using Android Studio, you need to integrate the APM SDK into your Android Studio project before development.
dependencies {
// Add the APM SDK library dependency.
implementation 'com.huawei.agconnect:agconnect-apms:1.5.2.307'
}
-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
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.
Button triggeranrBtn = findViewById(R.id.anr_test);
triggeranrBtn.setOnClickListener(v -> {
Log.d("apmsAndroidDemo", "trigger ANR");
anrTestEnable = true;
});
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);
}
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.