Integrating AppGallery Connect Crash in a Xamarin app for Android
Tags: android, xamarin, appgallery, huawei
Published: 2021-12-01 10:01:06

Today we are going to take a look at how we can integrate the AppGallery Connect crash service into your Xamarin app.

But why might you want to do this? The AppGallery Connect Crash service provides a powerful yet 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.

The service is completely free to use and can be a great addition to your project!

Below is a simple app example you can follow along with to get a good idea of how you might use this service in your own app!

## Preparing the Xamarin Environment and Configuring Your Project

Install the Xamarin environment.
You'll need to install Visual Studio first, and then select Mobile development with .NET in Visual Studio to install the [Xamarin environment](https://docs.microsoft.com/en-us/xamarin/cross-platform/get-started/requirements).

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

[Create a project](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-android-0000001058210705#section113292017144) in AppGallery Connect and enable [HUAWEI Analytics](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-enabling-0000001050745155) for your project. 

Install the service SDK. Right-click your project and choose **Manage NuGet Packages**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a96t8crfjgjrx7kyfl9k.png)
Search for **crash** on the Browse tab. Click **Xamarin.Android bindings for HMS Core - AgconnectCrash** in the search results and install it. 

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

Add the [JSON file](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-harmonyos-0000001184684961#section641530103413) to the **Assets** directory 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j1x7mpbxwd57jo6s14i.png)

Next, implement **LazyInputStream** to read the **agconnect-services.json** file.
For details, please refer to [Getting Started with Xamarin](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-xamarin-0000001081055220)

Finally set a package name in Android Manifest.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sj27utxz4ka1kryfcw7.png)
 
## Example Application

Lets start by creating a simple layout that has three buttons. 

{% gist https://gist.github.com/devwithzachary/c9302b160025cf2e9595c2a809a44ca4 %}

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21mv6vvl2ftgkc5p8roe.png)

In the **MainActivity.cs** file, call * *AGConnectCrash.Instance.TestIt** to trigger a crash, call **AGConnectCrash.Instance.SetUserId** to set a custom user ID, call **AGConnectCrash.Instance.SetCustomKey** to set the key and value for a custom key-value pair, call **AGConnectCrash.Instance.Log** to set the log level, and call **AGConnectCrash.Instance.RecordException** to record a non-fatal exception.

{% gist https://gist.github.com/devwithzachary/67fb0147e626a409d64ed90f961156e1 %}

## View the Crash Report

Tap **MakeCrash**, **CatchException**, and **CustomReport** in sequence, and check the report in AppGallery Connect.

Crash statistics:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxgpgctunt3qgx15i2d5.png)

Exceptions:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gubgadin79rx9sb8d5x.png)

Debugging:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zvibcyxt44s3k2skrbk.png)

View custom key-value pairs:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qguru8fochfwsu2v3kk.png)
 
View custom log level:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1gckwnce62a8k07fhaj.png)
 
View custom user IDs:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1bdpyqshrjtzuwhazeo.png)
 
For more information, please refer to:
[Crash document for Android](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/crash-xamarin-android-gettingstarted-0000001130566711)
[Codelab of Crash for Xamarin.Android](https://developer.huawei.com/consumer/en/codelabsPortal/carddetails/CrashService-Xamarin-Android)