Integration of Huawei Crash Service in Attendance Tracker Android app (Kotlin) – Part 4
Tags: kotlin, crash, android, xml
Published: 2022-03-21 16:51:17


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dq3jyfnb13s4jxw4tlr5.jpeg)
**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](https://forums.developer.huawei.com/forumPortal/en/topic/0201810952098220013?fid=0101187876626530001)

[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](https://forums.developer.huawei.com/forumPortal/en/topic/0201817532838860004?fid=0101187876626530001)

[Beginner: Find the CRUD operations in Room Database in Attendance Tracker Android app (Kotlin) – Part 3](https://forums.developer.huawei.com/forumPortal/en/topic/0202827964929470059)

**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**

- First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a [Huawei ID](https://developer.huawei.com/consumer/en/doc/start/registration-and-verification-0000001053628148).
- Create a project in android studio, refer [Creating an Android Studio Project](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#1).
- Generate a **SHA-256 certificate fingerprint**.
- 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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/416wmx9kag8oudie5rhw.png)

Note: Project Name depends on the user created name.

- [Create an App in AppGallery Connect](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0).
- Download the **agconnect-services.json** file from **App information**, copy and paste in android **Project** under **app** directory, as follows.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71o60x4t8bunsr3tq3kn.png)

- Enter **SHA-256 certificate fingerprint** and click **Save** button, as follows.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd0898z2z7wqa2cz6a2i.png)

- Click **Manage APIs** tab and enable **HUAWEI Analytics**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgo1tre3bo3z84x2ycs9.jpg)

- Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer [Add Configuration](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#6).
`maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
`

- 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'`

- Now Sync the gradle.
- 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**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4t0xuylsqsyzscwehf3y.JPG)

**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**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48yv6lpssklde8bmc1pr.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6inrdhk3qin8alnc05cb.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7p3v6fdeofh6qj90vor.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hori9h9xdvmongyi6t9.jpg)

**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](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-crash-introduction-0000001055732708)
Crash Service – [Training Video](https://developer.huawei.com/consumer/en/training/course/video/101624680490355847)