[Huawei Developers] Freemind R:
Overview

Recently, I was asked to develop a pet store app that can filter out fake users during user registration and sign-in, so as to minimize the negative impact of fake users on the app operations. At first I was stumped but I quickly recalled the UserDetect function of HUAWEI Safety Detect which I had heard about at the Huawei Developer Conference 2020. So I integrated the function into the pet store app I was developing, and it turned out to be very effective. UserDetect helps improve the detection rate for fake users, as well as prevent malicious posting, credential stuffing attacks, and bonus hunting. An added bonus is that the service comes free of charge.

Now, I will show you how I integrate this function.

Demo and Sample Code on the HUAWEI Developers Website

You can download UserDetect sample code for both Java and Kotlin from the HUAWEI Developers website by clicking on the following link. In addition to UserDetect, sample code for four other functions is also provided. You can then run the demo by changing the package name according to tips on the website.

You can also take a look at the sample code I wrote for my pet store app:

Sample code-PetStore

1. Preparations

1.1 Installing Android Studio

Before you get started, ensure that you have installed Android Studio. You can download it from Android Studio.

1.2 Configuring App Information in AppGallery Connect

Before developing an app, you need to configure app information in AppGallery Connect. For details, please refer to Preparations.

1.3 Configuring the Huawei Maven Repository Address

Open the build.gradle file in the root directory of your Android Studio project.

Add the AppGallery Connect plug-in and the Maven repository address.

· Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.

· Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.

· If the agconnect-services.json file has been added to the app, go to buildscript> dependencies and add the AppGallery Connect plug-in configuration.

<p style="line-height: 1.5em;">buildscript {
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
// Add the AppGallery Connect plug-in configuration.
classpath 'com.huawei.agconnect:agcp:1.4.2.300'
}
}

allprojects {
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
}
</p>

Note that the Maven repository address cannot be accessed from a browser as it can only be configured in the IDE. If there are multiple Maven repositories, add the Maven repository address of Huawei as the last one.

1.4 Adding Build Dependencies

Open the build.gradle file in the app directory of your project.

Add the following information under apply plugin: 'com.android.application' in the file header:

<p style="line-height: 1.5em;">apply plugin: 'com.huawei.agconnect'
</p>

Add the build dependency in the dependencies section.

<p style="line-height: 1.5em;">dependencies {
implementation 'com.huawei.hms:safetydetect:5.0.5.301'
}
</p>

1.5 Configuring Obfuscation Scripts

If you are using AndResGuard, add its trustlist to the build.gradle file in the app directory of your project. For details about the code, please refer to Configuring Obfuscation Scripts on the HUAWEI Developers website.

2. Code Development

2.1 Creating a SafetyDetectClient Instance

<p style="line-height: 1.5em;">// Pass your own activity or context as the parameter.
SafetyDetectClient client = SafetyDetect.getClient(MainActivity.this);
</p>

2.2 Initializing UserDetect

You need to call the initUserDetect API to complete initialization. In my pet store app, the sample code for calling the initialization API in the onResume method of the LoginAct.java class is as follows:

<p style="line-height: 1.5em;">@Override
protected void onResume() {
super.onResume();
// Initialize the UserDetect API.
SafetyDetect.getClient(this).initUserDetect();
}
</p>

For more, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0202460793663820014