Integration of Huawei Account Kit and Analytics Kit in Android App KnowMyBoard Part -1
Tags: huaweiaccountkit, huaweianalyticskit, java, android
Published: 2022-06-03 11:43:21

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

**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**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6db0kr359dyz9q55wxz.png)
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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et4z68l2bwrzroxf9i2r.png)
**Supported Devices**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qubej5yx7jvz9osl3xc3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaxygzp1w9qbhqte9u4g.png)
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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1l5yncqfdmne5vt0cva.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08n9rck2erst55jx94k0.png)
Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle.
`apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.hms:hwid:6.4.0.301'
implementation 'com.huawei.hms:hianalytics:6.4.1.302'`
Root level gradle dependencies
`maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.6.0.300'`   
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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvrnq5jfj35u6kiyif0y.png)
**Let's start coding**
**MainActivity.java   **

```
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);
    }
}

```
**LoginViewModel.java**

```
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);

    }
}

```
**Result**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18ongsjtghe1o2mpxna7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmptpctepxapnim1afwh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kco55l1q0u1qv2rimnbs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b97zh57vk554rzck1tw0.png)
**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](https://developer.huawei.com/consumer/en/training/course/video/101620801350862038)

Analytics Kit- [Training Video ](https://developer.huawei.com/consumer/en/training/course/video/101585551680860917)   
[Checkout in forum](https://forums.developer.huawei.com/forumPortal/en/topic/0202883397761280020)