Introduction
In this article, we will cover how to verify Phone Number and Anonymous Account Login using Huawei Auth Service in Unity Project using Official Plugin (Huawei HMS Core App Services). AppGallery Connect provides a cloud-based Auth Service and SDKs to help you quickly build a secure and reliable user authentication system for your apps to verify user identity.
The AppGallery Connect Auth service supports multiple authentication methods and is seamlessly integrated with other server less services to secure user data based on simple rules that you have defined.
Key FunctionsUsing the AppGallery Auth Service SDK, you can integrate one or more of the following authentication methods into your app for achieving easy and efficient user registration and sign-in.
Self-owned account: Your self-owned account is used to support the access of your existing authentication system, so that your existing users can access other server less services in a secure manner.
Anonymous account: Anonymous accounts can be used to access your apps as visitors. The Auth service can assign user IDs to your app visitors, so that they can access other server less services in a secure manner. A visitor can be registered as a formal user and retain the original user ID to ensure service continuity.
Third-party accounts: AppGallery Connect allows user identity to be verified by third-party authentication services. The AppGallery Auth Service SDK supports the following accounts for user identity verification:
1. HUAWEI account
2. HUAWEI Game Service account
3. Phone number
4. Email account
5. WeChat account
6. Weibo account
Development Overview
You need to install Unity software and I assume that you have prior knowledge about the unity and C#.
Hardware Requirements
A computer (desktop or laptop) running Windows 10.
A Huawei phone (with the USB cable), which is used for debugging.
Software Requirements
Java JDK installation package.
Unity software installed.
Visual Studio/Code installed.
HMS Core (APK) 4.X or later.
Follows the steps.
1. Create Unity Project.
Open Unity Hub.
Click NEW, select 3D, Project Name and Location.
Click CREATE, as follows
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
2. Click Asset Store, search Huawei HMS Core App Services and click Import, as follows.
3. Once import is successful, verify directory in Assets > Huawei HMS Core App Services path, as follows.
4. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.
5. Generate a SHA-256 certificate fingerprint.
To generating SHA-256 certificate fingerprint use below command
keytool -list -v -keystore D:\Unity\projects_unity\file_name.keystore -alias alias_name
6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.
7. Choose Project Settings > Player and update package name.
8. Open LauncherTemplate.gradle and add below line.
apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.agconnect:agconnect-auth:1.4.2.301'
implementation 'com.huawei.hms:base:5.2.0.300'
implementation 'com.huawei.hms:hwid:5.2.0.300'
9. Open AndroidManifest file and add below permissions.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
10. Open "baseProjectTemplate.gradle" and add lines, as follows.
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
maven {url 'https://developer.huawei.com/repo/'}
11. Open "mainTemplate.gradle" and add lines like shown below.
implementation 'com.huawei.agconnect:agconnect-auth:1.4.2.301'
implementation 'com.huawei.hms:base:5.2.0.300'
implementation 'com.huawei.hms:hwid:5.2.0.300'
12. Create MainActivity.java class inside Plugin > Android folder.
13. Follow the steps, as shown in image:
a. Assign Ads script to Canvas.
b. Select Button and add onclick event
c. Assign all button to button handler as per your requirements.
14. Onclick Button Handler you find your script HMSAuthService (As per your script name) and attach method as per below screen shot.
15. To build apk and run in device, choose File > Build Settings > Build for apk or Build and Run for run on connected device.
Result
1. Click on Anonymous Login, send OTP and Verify button you can see below results.
2. Find the details in AppGallery Connect, as follows.
Tips and Tricks
Always use the latest version of the library.
Add agconnect-services.json file without fail.
Add SHA-256 fingerprint without fail.
Make sure dependencies added in build files.
Make sure that you enabled the auth service in AG-Console.
Make sure that you enabled the Authentication mode in Auth Service.
Conclusion
In this article, we have learnt integration of Huawei Auth Service-AGC anonymous account login and mobile number verification through OTP in Unity Game development. Auth Service provides secure and reliable user authentication system to your application.
Thanks for reading the article, please do like and comment your queries or suggestions.
References
Unity Auth Service Manual
Auth Service
Checkout in forum
Related
More articles like this, you can visit HUAWEI Developer Forum
HUAWEI Analytics Kit 5.0 version has been updated, and it supports iOS SDK now, enabling one-stop, unified analysis of cross-platform Android and iOS users. It looks into user-centric analysis and helps you understand user behavior on the Android and iOS platforms. Today I want to share with you how to add Analytics Kit to your iOS App. The version I integrated is 5.0.0.300.
The general steps we will walk through include:
Configuring App Information in AppGallery Connect
Integrating the HMS Core SDK
Accessing HUAWEI Analytics Kit
Configuring App Information in AppGallery Connect
Follow the steps below to configure App Information in AppGallery Connect for your iOS application. If you have both an Android and an iOS version of your app, you can set them within the same project, this will allow you to finish the data analysis conveniently.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Getting Started:
1. Have your Bundle ID ready for your iOS app (ask your developers for this).
2. Go to AppGallery Connect and create a new project.
Once you’ve created a new project, you’ll see a page, and the side navigation bar shows the AGC's various function menus.
3. Click Add app, and create an iOS application.
Here the App package ID is the Bundle ID.
Click OK, and then you will get the configuration file agconnect-services.plist.
Click Manage APIs, and here we’ve already had the Analytics API enabled.
Back to the side navigation bar, and go to the HUAWEI Analytics tab, you will see all kinds of functions, including User analysis, Behavior analysis, etc. Select any menu to Enable Analytics service.
On the Project access settings page, set the data storage location, time zone, currency, user data storage time, and calendar week, and click Finish.
Integrating the HMS Core SDK (in Cocoapods Mode)
I prefer the Cocoapods Mode. You can also integrate the HMS Core SDK into the Xcode Project Manually.
1. Add the AppGallery Connect configuration file of the app to your XCode project.
a. Click Download agconnect-services.plist to obtain your iOS config file (agconnect-services.plist).
b. Move your agconnect-services.plist file into the root of your Xcode project.
2. Edit the Podfile file
a. Create a Podfile if you don't already have one:
$ cd your-project-directory
$ pod init
b. To your Podfile, add the pods that you want to use in your app: Add pod 'HiAnalytics', that is, the dependency for pod.
Example Podfile file:
Code:
# Uncomment the next line to define a global platform for your project
# platform :iOS, '9.0'
target 'HiAnalyticsSwiftDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for HiAnalyticsSwiftDemo
pod 'HiAnalytics'
target 'HiAnalyticsSwiftDemoUITests' do
# Pods for testing
end
end
c. Install the pods, then open your .xcworkspace file to see the project in Xcode:
$ pod install
$ open your-project.xcworkspace
Accessing HUAWEI Analytics Kit
1. Initialize Analytics SDK in your app
The final step is to add initialization code to your application. Initialize AppDelegate through the config interface.
Objective C sample code: Perform initialization in AppDelegate.m.
Code:
#import "AppDelegate.h"
#import <HiAnalytics/HiAnalytics.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
...
- (BOOL)Application:(UIApplication *)Application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after Application launch.
[HiAnalytics config];//Initialization
return YES;
}
...
@end
Swift code example: Perform initialization in AppDelegate.swift.
Code:
import UIKit
import HiAnalytics
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func Application(_ Application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after Application launch.
HiAnalytics.config();//Initialization
return true
}
...
}
2. Record defined events using the onEvent API.
For definitions of the events, please refer to Event Description.
3. Call APIs of HUAWEI Analytics Kit to implement the corresponding functions. For details, please refer to API Reference.
During the development, you can use DebugView to view the event records in real time, observe the results, and adjust the event tracing scheme.
Enabling/Disabling the Debug Mode
1.To enable the debug mode:
Go to Product > Scheme > Edit Scheme from the Xcode menu. On the Arguments page, click + to add the -HADebugEnabled parameter. After the parameter is added, click Close to save the setting.
Code:
1. –HADebugEnabled
2.To disable the debug mode
Code:
1. -HADebugDisabled
After the data is successfully reported, you can go to HUAWEI App Debugging to view the data, as shown in the following figure.
Q&A:
1. I have integrated the iOS SDK of HUAWEI Analytics Kit into my app but why no log events are collected when the app is running?
When this problem occurs, it is probable that the run log parameters have not been set. You can set the log level to any of the following values: -HALogLevelDebug, -HALogLevelInfo, -HALogLevelWarn, and -HALogLevelError.
For example, to set the log level to -HALogLevelDebug, perform the following steps:
Choose Product > Scheme > Edit Scheme from the Xcode menu.
On the Arguments page, click + to add the -HALogLevelDebug parameter.
After the parameter is added, click Close to save the setting.
Face problems during development? Take it easy. Go to Stack Overflow and raise your questions. Huawei experts will get back to you as soon as possible.
https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
The Crash service of AppGallery Connect provides a simple, coding-free solution for app crashes. It is found that Cocos Creator also supports the Crash service provided by AppGallery Connect. Let’s see how it works.If you want to quickly experience this service, see the demo on GitHub.
1. Creating an App and Enabling the Crash Service
1.1 Setting a Cocos App ID
Cocos app ID is the unique identifier of a game app. Before using services, you need to set a Cocos app ID for your game project.
If you do not have a Cocos-based game, click Dashboard to create one in the Cocos Account Center, as shown in the following figure.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
In the Cocos Account Center, click Create a game, complete your game information, and obtain the generated app ID.
Return to Cocos Creator, click Set Cocos AppID, and choose the game to associate it.
1.2 Enabling HUAWEI Analytics and Crash in Cocos Creator
In the Service panel, enable HUAWEI Analytics and Crash.
You can also view the enabled services (highlighted) in the Cocos Account Center, as shown in the following figure.
1.3 Enabling HUAWEI Analytics and Crash in AppGallery Connect
You also need to enable the services in AppGallery Connect. The steps are as follows:
a) Sign in to AppGallery Connect, create a project, and add an app to it. For details about how to create an app in AppGallery Connect, check the official documentation.
b) In AppGallery Connect, click My projects, select a project, go to Quality > Crash, and click Enable now to enable both HUAWEI Analytics and Crash.
c) Go to Project settings and download the latest agconnect-services.json file.
d) Save the downloaded agconnect-services.json file to the settings directory in your Cocos project.
The file is also displayed in Cocos Creator, as shown in the following figure.
2. Building and Installing an App
2.1 Releasing your app in AppGallery Connect
a) In Cocos Creator, go to Project > Build…, choose HUAWEI AppGallery Connect, and set a package name. The package name must be the same as that configured in AppGallery Connect. Otherwise, a compilation error will be reported.
b) Click Build, and then Compile. You can find the corresponding APK file in the \build\jsb-link\publish\android directory of the project. You can run the adb command after a USB cable has been connected to install the package on your phone for testing.
c) You can also upload the APK file to AppGallery Connect by clicking Upload next to Compile. Select a sign-in type, fill in the relevant information, and click Confirm.
2.2 Releasing your app to an Android platform
a) In the Build… panel, choose Android for Platform, connect a phone to your computer, and click Play to install the APK file to your phone.
b) You can also open the project in the \build\jsb-link\frameworks\runtime-src\proj.android-studio directory in Android Studio.
3. Testing the Crash Service
Usually, app crashes are not common. You can manually trigger a crash and test the Crash service by calling the AGConnectCrash.testIt method of the Crash SDK and then checking whether the crash data is reported in AppGallery Connect.
In this example, a test button CrashTest is created. You can click the button to trigger a crash. The API is defined as follows:
testIt(): void
Sample code:
huawei.agc.crash.CrashService.testIt();
4. Viewing a Crash Report
4.1 Viewing crash statistics
a) Package and run your app. Tap CrashTest to trigger a crash. Exit the app and launch it again.
b) In AppGallery Connect, click My projects.
c) Find your project from the project list and click your app on the project card.
d) Go to Quality > Crash. On the Crash page, click the Statistics tab and select Last hour to view crash statistics of your app.
4.2 Viewing crash stack details
Click the crash name to go to the crash details page. Click the Stack tab. The possible causes of the crash are displayed, helping you analyze the crash.
4.3 Obtaining a readable crash report
a) When ProGuard or DexGuard overrides class names, fields, and methods in code with unreadable code during building, you can obtain a de-obfuscation crash report from the obfuscation mapping file.
b) Find the obfuscation mapping file in the following directory in Android Studio and download the file.
c) On the Mapping file tab page in AppGallery Connect, click Upload.
d) It is found that the data is restored.
Summary
1. It is simple to integrate the Crash SDK into a Cocos-based app. You just need to create an app and enable HUAWEI Analytics and Crash.
2. If it is your first time integrating the SDK, you can use a demo app for testing.
3. In addition to Crash, some other Huawei services have started to support Cocos-based apps.
For more details, please check:
1. Cocos official documentation:
https://docs.cocos.com/creator/manual/en/cocos-service/agc-crash.html
2. HUAWEI AppGallery Connect Crash documentation:
https://developer.huawei.com/consum...Gallery-connect-Guides/agc-crash-introduction
3. HUAWEI AppGallery Connect Crash codelab:
https://github.com/AppGalleryConnect/agc-crash-codelab-java
4. Cocos Creator Integrates with AppGallery Connect APM — Releasing a Cocos-based App:
https://forums.developer.huawei.com/forumPortal/en/topic/0202382127496450426?fid=0101187876626530001
if you have any question, please refer to stackoverflow.
When an app is used, such problems may occur: slow app launch, Application Not Responding (ANR), app crash, and network loading failure. These are the major issues that affect user experience.
To meet increasing demands of diagnosing performance problems, more and more app performance monitoring services have emerged in the market. HUAWEI AppGallery Connect provides full-process quality services in app development, testing, release, and analysis.If you want to quickly experience this service, see the demo on GitHub.
1. HUAWEI AppGallery Connect APM
App Performance Management (APM) is one of the quality services provided by AppGallery Connect. This service can monitor app performance at the minute level, and is totally free of charge. It does its job by:
Collecting data about app launches, app screen rendering, network requests, and foreground/background activities automatically.
Monitoring ANR problems and recording relevant device information and log information when they occur.
Providing app performance data analysis reports for app optimizations.
Supporting custom traces to monitor app performance data in specific scenarios.
AppGallery Connect APM has the following edges over other app performance monitoring platforms:
Easy integration: You can integrate APM for app performance analysis without any coding.
Comprehensive metrics: APM illustrates an app’s performance in a myriad of dimensions such as app launches, ANR, screen rendering, and network requests, and also supports custom traces, indicators, and dimensions to provide a tailored report for your specific needs.
2. Integrating AppGallery Connect APM
As described in Huawei document, you can easily complete the integration of the service by adding the required plug-in and SDK configurations to your code without any coding. There are just a few simple steps:
i. Create an app in AppGallery Connect and enable APM.
ii. Download and add the JSON file.
iii. Import the HuaweiService package to Unity Editor.
iv. Integrate the APM plug-in and the APM SDK.
Then, you can package and run your app, and view its performance data later in AppGallery Connect.
2.1 Creating an App in AppGallery Connect and Enabling APM
If you need to enable APM for an existing app, make sure that the app package name in the APK file is the same as that configured in AppGallery Connect when the app is created. Access AppGallery Connect, create an app, and enable APM. Ensure that your app package name is the same as that configured in the APK file.
Then select an app under My projects, go to Quality > APM, and click Enable.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
2.2 Configuring the Android Environment in Unity Editor and Downloading the JSON File
1. Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select items for Android as required.
2. In Unity Editor, choose Edit > Project Settings > Player > Other Settings, and set the package name to the one you set in AppGallery Connect.
2.3 Importing the HuaweiServices Package
1. Download the package here.
2. Choose Assets > Import package.
3. Select the required package, and click Import.
4. In AppGallery Connect, click My projects, and find your project from the list. Go to Project settings > General information, download the agconnect-services.json file, and save the file to the Assets\Plugins\Android directory of your Unity project.
2.4 Integrating the APM SDK into Your Unity Project
1. Add the following code to the project-level baseProjectTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.4.2.301'
classpath ''com.huawei.agconnect:agconnect-apms-plugin:1.4.1.302'
**BUILD_SCRIPT_DEPS**
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
2. Add the following code to the app-level LauncherTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
apply plugin: 'com.huawei.agconnect.apms'
dependencies {
implementation project(':unityLibrary')
implementation 'com.huawei.agconnect:agconnect-apms:1.4.1.303'
}
3. Configure the manifest file.
3. Packaging and Testing
Package the APK file and launch the app. Check whether the corresponding APMS logs are generated.
If so, you have integrated the APM service successfully.
For details, visit: https://forums.developer.huawei.com/forumPortal/en/topic/0203396636080560050?fid=0101188387844930001
4. Summary
In only 4 steps, you can integrate the HUAWEI AppGallery Connect APM SDK without coding, to implement comprehensive app performance monitoring.
The APM analysis report provides detailed device, log, and performance information recorded when an issue occurs. This real-time report drives app operations based on data and provides abundant information for app optimizations. App R&D and operations personnel no longer need to spend much time on locating and reproducing performance problems.
For more details, please check:
AppGallery Connect APM development guide:
https://developer.huawei.com/consum...pGallery-connect-Guides/agc-apms-introduction
Unity documentation about APM:
https://docs.unity.cn/cn/Packages-cn/[email protected]/manual/apm.html
Coding-free Integration of AppGallery Connect APM on Android Platform:
https://forums.developer.huawei.com/forumPortal/en/topic/0203396636080560050?fid=0101188387844930001
HUAWEI AppGallery Connect APM codelab for Android:
https://developer.huawei.com/consumer/en/codelab/AppGalleryConnectAPM/index.html#0
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
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.
Java:
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.
Java:
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.
Java:
<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.
Java:
AGConnectCrash.getInstance().enableCrashCollection(true)
2. Test the below method in your code.
Java:
AGConnectCrash.getInstance().testIt([email protected])
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.
Java:
AGConnectCrash.getInstance().setUserId("123456789")
4. Recording Custom Logs: we can record custom logs, Recorded logs will be reported together with the crash data.
Java:
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.
Java:
AGConnectCrash.getInstance().setCustomKey("keyInt", 1)
In the Home.kt activity to find the business logic for crash service.
Java:
class Home : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
img_profile.setOnClickListener {
val intent = Intent([email protected], EmployeesList::class.java)
startActivity(intent)
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show()
// Test
AGConnectCrash.getInstance().testIt([email protected])
}
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
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Introduction
In this article, I will explain how to integrate Huawei Crash Kit in a News Application using React Native. This kit provides a crash analysis service where we can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time. It is very easy to integrate.
Requirements
1. JDK version: 1.7 or later
2. Android Studio version: 3.X or later
3. minSdkVersion: 21 or later
4. targetSdkVersion: 31 (recommended)
5. compileSdkVersion: 29 (recommended)
6. Gradle: 4.1 or later (recommended)
7. Must have a Huawei Developer Account
8. Must have a Huawei phone with HMS 4.0.0.300 or later and running EMUI 4.0 or later.
9. React Native environment with Android Studio, Node Js and Visual Studio code.
Preparation
In order to develop the HMS react native apps, following steps are mandatory:
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. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in SHA256 CODE
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, as follows.
8. Create a react native project. Use the following command
Code:
react-native init project name
9. Enabling the Crash Service
(i). Sign in to AppGallery Connect and click My projects.
(ii). Find your project from the project list and click the app for which you need to enable the Crash service on the project card.
(iii). Choose Quality > Crash. The Crash page is displayed.
Note: To use the Crash service, you need to enable HUAWEI Analytics first. If you have not enabled HUAWEI Analytics, click Enable now, or navigate to HUAWEI Analytics to enable the service.
Integration
1. Configure android level build.gradle.
Add to buildscript/repositories and allprojects/repositories
Code:
maven {url 'http://developer.huawei.com/repo/'}
2. Adding permissions
Add below permissions to Android.manifest file.
XML:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
3. Download the Plugin using NPM.
Open project directory path in command prompt and run this command.
Code:
npm i @react-native-agconnect/crash
4. Integrate the Crash SDK.
Add the following code to the app-level build.gradle file (usually in the app directory):
(i).
Code:
implementation 'com.huawei.agconnect:agconnect-crash:1.6.4.300'
(ii). 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.
Code:
implementation 'com.huawei.agconnect:agconnect-apms:1.5.2.307'
Sync Gradle.
Development
To successfully crash service, we would need to:
Initialize the AGConnectCrash instance.
JavaScript:
AGConnectCrash.getInstance()
Collect or report crash data.
JavaScript:
AGConnectCrash.enableCrashCollection(Boolean)
For instance, in my News Application, if Food field is clicked, it will not fetch any Data and an error (Namely NoDataExpection extending RuntimeException) would be thrown which will cause app to crash.
We just have to add this code, so that When app encounters crash, Crash service reports the crash on dashboard in AppGallery connect.
JavaScript:
AGConnectCrash.getInstance().enableCrashCollection(true)
If you want to test crash kit you can follow simple steps:
AGCCrash.testIt() is used to test the crash. We can view the crash data in AppGallery Connect to check whether the crash service is running properly.
JavaScript:
AGCCrash.testIt()
2. Set custom user ID.
We can use custom id for identifying the users uniquely.
JavaScript:
AGCCrash.setUserId("123");
3. Add Custom Key – Value Pair.
AGCCrash.setCustomKey is used to add the crash record status.
JavaScript:
AGCCrash.setCustomKey("key", "value");
Testing
Run the below command to build the project.
Code:
React-native run-android
Run the application (Generating the Signed Apk):
Open project directory path in command prompt. Navigate to android directory and run the below command for signing the apk.
Code:
gradlew assembleRelease
Result/ Crash Report
To monitor crash:
1. Sign in to AppGallery Connect and select my project.
2. Choose the app.
3. Select Quality > Crash on left panel of the screen.
4. View details about crash stack information.
5. View custom key-value pairs.
6. We can also View custom log levels and custom user IDs.
To enable Crash service notifications
1. Sign in AppGallery and Select User and Permissions.
2. Select User > Personal Information.
3. Select the check box under Email for Crash notification under notification.
Tips and Tricks
1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
2. agconnect-services.json is not required for integrating the hms-ads-sdk.
3. Make sure you have added SHA-256 fingerprint without fail.
4. Make sure all the dependencies are added properly.
Huawei Crash services work on non-Huawei device.
5. AGConnectCrash.getInstance().testIt(mContext) triggers app crash. Make sure to comment or remove it before releasing your app.
6. Crash Service takes around 1 to 3 minutes to post the crash logs on App Gallery connect dashboard/console.
7. Crash SDK collects App and system data.
System data :
AAID, Android ID (obtained when AAID is empty), system type, system version, ROM version, device brand, system language, device model, whether the device is rooted, screen orientation, screen height, screen width, available memory space, available disk space, and network connection status.
App data:
APK name, app version, crashed stack, and thread stack.
8. The Crash SDK collects data locally and reports data to the collection server through HTTPS after encrypting the data.
9. For project cleaning navigate to android directory and run the below command.
Code:
gradlew clean
Conclusion
In this article, we learnt about how to integrate Crash Kit in React native project. It is very simple and quick way to detect, locate and resolve app crashes.
Reference
Crash Kit: Documentation
Crash Kit: Training Video