[Huawei Developers] HMS Community:
Introduction

In this article, we will learn how to integrate the Huawei Account kit and Analytics kit in KnowMyBoard application. This is the first part of the application where, we are using Huawei Account kit for user login and Analytics kit for analysis and custom events for application usage and behaviour. You may expect second part soon, where we are using Huawei Location kit and ML kit.

Huawei Account kit provides simple, secure, and quick sign-in and authorization functions. User is not required to enter accounts details and wait for authorization. user can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their Huawei IDs.

Supported Devices

Huawei Analytics kit is a one-stop user behaviour analysis platform for products such as mobile apps, web apps, quick apps, quick games, and mini-programs. It offers scenario-specific data collection, management, analysis, and usage, helping enterprises achieve effective user acquisition, product optimization, precise operations, and business growth.

It is a one-stop digital, intelligent data analysis platform tailored to meet the needs of enterprises cross-departmental and cross-role personnel.

Supported Devices

Development Overview

You need to install Android Studio IDE and I assume that you have prior knowledge about the Android application development in java.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

Android phone (with the USB cable), which is used for debugging.

Software Requirements

Java JDK 1.7 or later.

Android studio software

HMS Core (APK) 4.X or later.

Integration process

Step 1: Create android project.

Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle.

[/B][/B]
apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.hms:hwid:6.4.0.301'
implementation 'com.huawei.hms:hianalytics:6.4.1.302'
[B][B]

Root level gradle dependencies

[/B][/B][/B]
maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
[B][B][B]

Step 3: Add the below permissions in Android Manifest file.

<uses-permission android:name="android.permission.INTERNET" />

Step 4: Create project and configure app in AppGallery.

Step 5: Download and add agconnect-services.json file in app directory of project.

Let's start coding

MainActivity.java

[/B]
public class MainActivity extends AppCompatActivity {
LoginViewModel loginViewModel;
ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.setActivity(this);
// Enable SDK log recording.
HiAnalyticsTools.enableLog();
MyApplication.getAnalyticsInstance().setUserProfile("UserName","Test123");
loginViewModel = new LoginViewModel(getApplication());
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLoginViewModel(loginViewModel);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// Process the authorization result to obtain the authorization code from AuthAccount.
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 8888) {
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
if (authAccountTask.isSuccessful()) {
// The sign-in is successful, and the user's ID information and authorization code are obtained.
AuthAccount authAccount = authAccountTask.getResult();
UserData userData = new UserData();
userData.setAccessToken(authAccount.getAccessToken());
userData.setCountryCode(authAccount.getCountryCode());
userData.setDisplayName(authAccount.getDisplayName());
userData.setEmail(authAccount.getEmail());
userData.setFamilyName(authAccount.getFamilyName());
userData.setGivenName(authAccount.getGivenName());
userData.setIdToken(authAccount.getIdToken());
userData.setOpenId(authAccount.getOpenId());
userData.setUid(authAccount.getUid());
userData.setPhotoUriString(authAccount.getAvatarUriString());
userData.setUnionId(authAccount.getUnionId());
updateMessage("Welcome " + userData.getDisplayName());

} else {
// The sign-in failed.
Log.e("TAG", "sign in failed:" + ((ApiException) authAccountTask.getException()).getStatusCode());
updateMessage(getResources().getString(R.string.msg_signin_failed));
}
}
}

public void updateMessage(String msg) {
binding.txtMessage.setText(msg);
}
}

[B]

LoginViewModel.java

[/B][/B]
public class LoginViewModel extends AndroidViewModel {

private MyApplication app;
AccountAuthService service;
public String message = "Login required";

public LoginViewModel(@NonNull Application application) {
super(application);
app = (MyApplication) application;
}
public void loginClicked() {
AccountAuthParams authParams = new
AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();
service = AccountAuthManager.getService(MyApplication.getActivity(), authParams);
MyApplication.getActivity().startActivityForResult(service.getSignInIntent(), 8888);

}
}

[B][B]

Result

Tricks and Tips

Makes sure that agconnect-services.json file added.

Make sure required dependencies are added

Make sure that service is enabled in AGC

Makes sure Analytics service is enable in AGC

Conclusion

In this article, we have learnt how to integrate Huawei Account kit, and Analytics kit Android application KnowMyBoard part-1. Account kit helps users to login quickly and conveniently sign in to apps with their Huawei IDs after granting initial access permission. Analytics kit helps in app analysis and user behaviour.

Thank you so much for reading. I hope this article helps you to understand the integration of Huawei Account kit and Analytics kit in Android application KnowMyBoard.

Reference

Account Kit- Training Video

Analytics Kit- Training Video

Checkout in forum