[Huawei Developers] HMS Community:
Introduction

In this article, we can learn how to integrate Huawei Crash service of AppGallery Connect in Attendance Tracker application.

So, I will provide the series of articles on this Attendance Tracker App, in upcoming articles I will integrate other Huawei Kits.

If you are new to this application, follow my previous articles

Beginner: Integration of Huawei Account Kit of Obtaining Icon Resources feature in Attendance Tracker Android app (Kotlin) - Part 1

Beginner: Find Log in via SMS and Forgot password? Features using Huawei Account Kit, and Ads Kit in Attendance Tracker Android app (Kotlin) – Part 2

Beginner: Find the CRUD operations in Room Database in Attendance Tracker Android app (Kotlin) – Part 3

What is Crash service?

The AppGallery Connect Crash service provides a powerful lightweight solution to app crash problems. With the service, you can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time, without the need to write any code.

Huawei Crash is a realtime crash reporting tool that helps in tracking, prioritizing, and fix stability issues that compromise the quality of your app.

To ensure stable running of your app and prevent user experience deterioration caused by crashes, you are advised to monitor the running status of your app on each device, which is the key. The Crash service provides real-time reports, revealing any crash of your app on any device. In addition, the Crash service can intelligently aggregate crashes, providing context data when a crash occurs, such as environment information and stack, for you to prioritize the crash easily for rapid resolution.

Requirements

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.0.300 or later.

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.

4. Minimum API Level 24 is required.

5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Note: Project Name depends on the user created name.

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

7. Enter SHA-256 certificate fingerprint and click Save button, as follows.

Note: Above steps from Step 1 to 7 is common for all Huawei Kits.

8. Click Manage APIs tab and enable HUAWEI Analytics.

9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'

10. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Analytics
implementation 'com.huawei.hms:hianalytics:6.4.0.300'
// Huawei Crash Service
implementation 'com.huawei.agconnect:agconnect-crash:1.6.0.300'

11. Now Sync the gradle.

12. Add the required permission to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Configuration in AppGallery Connect

Find the Crash using AppGallery connect dashboard.

Choose My Projects > Quality > Crash, and click Enable now.

Let us move to development

I have created a project on Android studio with empty activity let us start coding.

1. Initialize an AGConnectCrash before other methods and Enable crash.

AGConnectCrash.getInstance().enableCrashCollection(true)

2. Test the below method in your code.

AGConnectCrash.getInstance().testIt(this@Home)

3. Customizing User IDs: Analyzing crashes by a user to resolve crashes using User ID. The user can be uniquely identified after a crash occurs.

AGConnectCrash.getInstance().setUserId("123456789")

4. Recording Custom Logs: we can record custom logs, Recorded logs will be reported together with the crash data.

AGConnectCrash.getInstance().log("catch exception")

5. Adding a Custom key-value pair: we can add a custom key-value pair to record the status. The recorded status will be reported together with the crash data.

AGConnectCrash.getInstance().setCustomKey("keyInt", 1)

In the Home.kt activity to find the business logic for crash service.

class Home : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

img_profile.setOnClickListener {
val intent = Intent(this@Home, EmployeesList::class.java)
startActivity(intent)
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show()
// Test
AGConnectCrash.getInstance().testIt(this@Home)
}

AGConnectCrash.getInstance().enableCrashCollection(true)
// Record Custom Status
AGConnectCrash.getInstance().setCustomKey("keyInt", 1)
// Set the user ID
AGConnectCrash.getInstance().setUserId("123456789")

try {
throw NullPointerException()
} catch (ex: NullPointerException) {
AGConnectCrash.getInstance().log("catch exception")
// Record non-fatal exception
AGConnectCrash.getInstance().recordException(ex)
}

}

}

Result

Tips and Tricks

1. Make sure you are already registered as Huawei developer.

2. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.

3. Make sure you have added the agconnect-services.json file to app folder.

4. Make sure you have added SHA-256 fingerprint without fail.

5. Make sure all the dependencies are added properly.

Conclusion

In this article, we have learnt the integrate of Huawei Crash service of AppGallery Connect in Attendance Tracker application. Crash service provides a powerful lightweight solution to app crash problems. With the service, you can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time, without the need to write any code.

I hope you have read this article. If you found it is helpful, please provide likes and comments.

Reference

Crash Service – Documentation

Crash Service – Training Video