Coding-free Integration of AppGallery Connect Crash into a Unity App
This screenshot-packed tutorial has all the directions you need to integrate App Gallery Connect Crash into a Unity application.
Join the DZone community and get the full member experience.
Join For FreePreparations
Reference:
https://docs.unity.cn/cn/Packages-cn/com.unity.hms@1.3/manual/crash.html
1. Download Unity Hub.
2. Configure the Android environment in Unity Editor.
Choose Edit > Preferences > External Tools > Andriod and configure the JDK and SDK paths. You can keep the default paths.
Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select items for Android as required.
3. Import the HuaweiService package.
Download the package from the Unity Assets Store.
In Unity Editor, choose Assets > Import package.
Select the required package and click Import. If you’re not sure which one to use, you can select all of them.
Completing Configurations in AppGallery Connect
1. Sign in to AppGallery Connect, and click the created app.
2. Go to My projects > Quality > Crash and click Enable now to enable the Crash service.
3. Go to Project settings > General information and download the latest agconnect-services.json file.
4. Save the file to the Assets\Plugins\Android directory of your Unity project.
Configuring the Project Information in Unity Editor
1. In Unity Editor, choose Edit > Project Settings > Player > Other Settings, and set the package name to match the package name you set in AppGallery Connect.
2. Add the following code to the project-level baseProjectTemplate.gradle file in the Assets\Plugins\Android directory:
allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0'
classpath 'com.huawei.agconnect:agcp:1.4.2.300'
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
3. Add the following code to the app-level LauncherTemplate.gradle file in the Assets\Plugins\Android directory:
xxxxxxxxxx
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation 'com.huawei.hms:hianalytics:5.0.5.300'
implementation 'com.huawei.agconnect:agconnect-crash:1.4.2.300'
}
Integrating the Crash Service
1. In Unity Editor, choose GameObject > UI > Button and create a button. Click the button, click Add Component on the right to create a script file called testcrash
, and then add the following methods:
xxxxxxxxxx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiService;
using HuaweiService.crash;
public class testcrash : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void CrashCollectON()
{
AGConnectCrash.getInstance().enableCrashCollection(true);
}
public void CrashCollectOFF()
{
AGConnectCrash.getInstance().enableCrashCollection(false);
}
public void SetTestIt()
{
Application.ForceCrash(0);
}
public void SetUserId()
{
AGConnectCrash.getInstance().setUserId("TestUser");
}
public void SetCustomKey()
{
AGConnectCrash.getInstance().setCustomKey("stringKey", "Hello world");
AGConnectCrash.getInstance().setCustomKey("booleanKey", false);
AGConnectCrash.getInstance().setCustomKey("doubleKey", 1.1);
AGConnectCrash.getInstance().setCustomKey("floatKey", 1.1f);
AGConnectCrash.getInstance().setCustomKey("intKey", 0);
AGConnectCrash.getInstance().setCustomKey("longKey", 11L);
}
public void SetDefaultlog()
{
AGConnectCrash.getInstance().log("set default log");
}
public void Setlog()
{
AGConnectCrash.getInstance().log(3, "set debug log");
AGConnectCrash.getInstance().log(4, "set info log");
AGConnectCrash.getInstance().log(5, "set warning log");
AGConnectCrash.getInstance().log(6, "set error log");
}
}
Keep creating other buttons and associating corresponding methods with them:
CrashCollectON
: Enables crash data reporting.
CrashCollectOFF
: Disables crash data reporting.
SetTestIt
: Creates a crash.
SetCustomKey
: Sets a custom key-value pair.
SetDefaultlog
: Sets the default log level.
Setlog
: Sets a custom log level.
SetUser
: Sets a custom user ID.
2. Package the APK file and launch the app. Check whether the buttons have been successfully created.
Testing the API and Viewing the Crash Report
I have written about this part before. Please check out my previous post for details.
Generating and Uploading a Mapping File
Manual upload
1. Open the obfuscation script file for your Unity project.
2. Create the proguard-unity.txt and proguard-user.txt files and add the following code to prevent obfuscation:
xxxxxxxxxx
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public com.crashlytics.** { *; }
3. When the APK package is generated, the corresponding mapping file is automatically generated accordingly. You can then upload the mapping file to AppGallery Connect.
Auto Upload
1. Enable the automatic upload of the mapping file.
Add the following code to the app-level LauncherTemplate.gradle file in the Assets\Plugins\Android directory:
xxxxxxxxxx
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
agcp{
mappingUpload = true
debug = true
appVersion = '0.1'
}
dependencies {
implementation 'com.huawei.hms:hianalytics:5.0.5.300'
implementation 'com.huawei.agconnect:agconnect-crash:1.4.2.300'
}
2. Export your Unity project and open it in Android Studio.
3. Generate a mapping file, which is automatically uploaded.
Run the gradle clean assembleRelease
command or use Android Studio to install the app. The mapping file will be successfully generated.
The following code indicates that the upload is successful.

Restoring the Obfuscated Report
The following figure is a crash report when the obfuscation mapping file has not been uploaded.

This figure shows what the report looks like after being restored.

For more details, please refer to Crash sample code:
https://github.com/AppGalleryConnect/agc-demos/tree/main/Android/agc-crash-demo-java
Opinions expressed by DZone contributors are their own.
Comments