Pygmy Collection Application Part 3 (Crash service)
Tags: android, java, hms, huawei
Published: 2022-02-21 12:43:00

## Introduction
In my last article, I have explained how to integrate account kit finance application. Have a look into [Pygmy collection application Part 1 (Account kit)](https://forums.developer.huawei.com/forumPortal/en/topic/0201684625183310089?fid=0101187876626530001). And Integration of Huawei Ads kit have look into [Intermediate: Pygmy Collection Application Part 2 (Ads Kit)](https://forums.developer.huawei.com/forumPortal/en/topic/0202690688578050115)

**What is Huawei Crash service?**
In this article, we will learn how to **integrate Crash services** of **AppGallery** in Pygmy collection finance application.
Huawei Crash is a realtime crash reporting tool that helps in **tracking**, **prioritizing**, and **fix** stability issues that compromise the quality of your app. Crashlytics also helps in troubleshooting and saves the debugging.

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

**Why do we need the crash service?**
Although apps have gone through rounds the tests before release considering the large user base diverse device models and complex network environment. It’s inevitable for apps to crash occasionally. Crashes compromise user experience, users may even uninstall app due to crashes and your app is not going to get good reviews.

You can’t get sufficient crash information from reviews to locate crashes, therefore you can’t resolve them shortly. This will severely harm your business. That’s why we need to use the crash services in our apps to be more efficient.

**How to integrate Crash Service**
1. Configure the application on the AGC.
2. Client application development process.

**1. Configure application on the AGC**
Follow the steps
Step 1: We need to [register as a developer](https://developer.huawei.com/consumer/en/doc/10104) account in AppGallery Connect. **If you are already a developer ignore this step.**
Step 2: Create an app by referring to [Creating a Project](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started#createproject) and Creating an App in the Project
Step 3: Set the data storage location based on the current location.
Step 4: Generating a Signing Certificate Fingerprint.
Step 5: Configuring the Signing Certificate Fingerprint.
Step 6: Download your **agconnect-services.json** file, paste it into the **app **root directory.
Step 7: Enable Crash services.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yqqmq12uqvl87v2wh2z.png)
 
**Client application development process**
Follow the steps.
Step 1: Create an Android application in the **Android studio** (Any IDE which is your favorite).
Step 2: Add the App level **Gradle dependencies**. Choose inside project Android > app > build.gradle.

```
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.agconnect:agconnect-crash:1.6.0.300'
```
// It is recommended that you integrate the APM SDK to further locate whether an app crash is caused by an app event or behavior such as ANR, launch, and network request.

```
implementation 'com.huawei.agconnect:agconnect-apms:x.x.x.xxx'
implementation 'com.huawei.hms:hianalytics:5.0.5.300'
```
Root level gradle dependencies.

```
maven { url 'https://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
```
Step 3: Add permission in **AndroidManifest.xml**

```
<application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
</application>
```
Step 4: Initialize Crash Service activity or application class
Step 5: Build Application
**Enable Crash Service**

```
AGConnectCrash.getInstance().enableCrashCollection(true);
```
**Test Crash service.**

```
AGConnectCrash.getInstance().testIt(context);
```
**Set User Id**

```
AGConnectCrash.getInstance().setUserId("12345");
```
**Set Log without log level**

```
AGConnectCrash.getInstance().log("set info log.");
```
**Set Log with Log Level**

```
AGConnectCrash.getInstance().log(Log.WARN, "set warn log.");
```
**Set custom Key pair value.**

```
AGConnectCrash.getInstance().setCustomKey("mobileNumber", "Phone number is empty");
// Add a key-value pair of the string type.
AGConnectCrash.getInstance().setCustomKey("UserName", "Basavaraj Navi");
// Add a key-value pair of the boolean type.
AGConnectCrash.getInstance().setCustomKey("isFirstTimeUser", false);
// Add a key-value pair of the double type.
AGConnectCrash.getInstance().setCustomKey("doubleKey", 1.1);
// Add a key-value pair of the float type.
AGConnectCrash.getInstance().setCustomKey("floatKey", 1.1f);
// Add a key-value pair of the integer type.
AGConnectCrash.getInstance().setCustomKey("intKey", 0);
// Add a key-value pair of the long type.
AGConnectCrash.getInstance().setCustomKey("longKey", 11L);
```

**Record Exception**

```
SimpleDateFormat format = new SimpleDateFormat(PygmyConstants.DOB_FORMAT);
try {
Date date = format.parse(dob);
customerEntity.setDateOfBirth(date);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
AGConnectCrash.getInstance().recordException(e);
}
```

## Result
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rxeuwthxh7vkap06mjn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnpjn69coawaridwursj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqyxqbgmcj5wy112yh9y.png)
   

## Tips and Tricks
Make sure you added **agconnect-service.json** file.
Add internet permission in AndroidManifest.xml.
You can also download the crash reports.
Conclusion
In this article, we have learnt what the Crash service is. And how to integrate the crash service. How to record log with log level and without log level. And also we have learn how to record exception.

## Reference
[Huawei Crash Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-crash-introduction-0000001055732708)