Expert: Integration of HMS Core Kits in MVVM and RxAndroid based Android App Part-2

Manoj Kumar
4 min readOct 8, 2021

Overview

In this article, I will create a Movie Show android application in which I will integrate HMS Core kits such as Huawei ID, Analytics, Huawei Ads and much more.

In this article I will integrate Analytics and Ads kit.

In this series of article I will cover all the kits with real life usages in Movie Show application. This is the part-2 article of this series.

Part-1: https://forums.developer.huawei.com/forumPortal/en/topic/0202684555189490078?fid=0101187876626530001

Huawei Analytics Service Introduction

HUAWEI Analytics Kit predefines rich analytics models to help you clearly understand user behavior and gain in-depth insights into users, products, and content. As such, you can carry out data-driven operations and make strategic decisions about app marketing and product optimization.

Analytics Kit implements the following functions using data collected from apps:

  1. Provides data collection and reporting APIs for collection and reporting of custom events.
  2. Sets up to 25 user attributes.
  3. Supports automatic event collection and session calculation as well as predefined event IDs and parameters.

Huawei Banner Ads

Banner ads are rectangular images that occupy a spot at the top, middle, or bottom within an app’s layout. Banner ads refresh automatically at intervals. When a user taps a banner ad, the user is redirected to the advertiser’s page in most cases.

Prerequisite

  1. Huawei Phone EMUI 3.0 or later.
  2. Non-Huawei phones Android 4.4 or later (API level 19 or higher).
  3. HMS Core APK 4.0.0.300 or later
  4. Android Studio
  5. AppGallery Account

App Gallery Integration process

  1. Sign In and Create or Choose a project on AppGallery Connect portal.
  2. Navigate to Project settings and download the configuration file.​​​​
  3. Navigate to General Information, and then provide Data Storage location.
  4. Navigate to Manage APIs > Huawei Analytics > Checked.
  5. Navigate to Huawei Analytics > Project Overview > Finish.​​​​​​​​​​​​​​​​​​

App Development

  1. Create A New Project.​​​
  2. Configure Project Gradle.

3. Configure App Gradle.

//HMS Kits
api 'com.huawei.hms:dynamicability:1.0.11.302'
implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300'
implementation 'com.huawei.hms:hwid:5.3.0.302'
implementation 'com.huawei.hms:hianalytics:5.0.1.301'
implementation 'com.huawei.agconnect:agconnect-crash:1.4.1.300'
implementation 'com.huawei.hms:ads-lite:13.4.30.307'

4. Configure AndroidManifest.xml.

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

5. Create Activity class with XML UI.

Analytics.java:

package com.hms.manoj;import android.content.Context;
import android.os.Bundle;
import com.huawei.hms.analytics.HiAnalytics;
import com.huawei.hms.analytics.HiAnalyticsInstance;
import com.huawei.hms.analytics.HiAnalyticsTools;
class Analystics { private HiAnalyticsInstance instance;
private Context context;
private static Analystics analystics;
private Analystics(Context context){
this.context=context;
HiAnalyticsTools.enableLog();
instance = HiAnalytics.getInstance(context);
}
public static Analystics getInstance(Context context){
if (analystics==null){
analystics=new Analystics(context);
}
return analystics;
}
public void setEvent(String tag, Bundle bundle){
instance.onEvent(tag, bundle);
}
public void setUserProfile(String tag, String attribute){
instance.setUserProfile(tag,attribute);
}
}

LoginActivity.java:

package com.hms.manoj.aab;import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;import com.hms.manoj.Analystics;
import com.hms.manoj.aab.ui.ShowsActivity;
import com.hms.movieshow.R;
import com.huawei.agconnect.crash.AGConnectCrash;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.ads.AdParam;
import com.huawei.hms.ads.HwAds;
import com.huawei.hms.ads.banner.BannerView;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.hwid.HuaweiIdAuthManager;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParams;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParamsHelper;
import com.huawei.hms.support.hwid.result.AuthHuaweiId;
import com.huawei.hms.support.hwid.service.HuaweiIdAuthService;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private static final int REQUEST_SIGN_IN_LOGIN = 1002;
private static String TAG = LoginActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button view = findViewById(R.id.btn_sign);
view.setOnClickListener(this);
initAds();
AGConnectCrash.getInstance().enableCrashCollection(true);
//Crash application
AGConnectCrash.getInstance().testIt(this);
}
private BannerView hwBannerView; private void initAds() {
HwAds.init(this);
hwBannerView = findViewById(R.id.huawei_banner_view);
hwBannerView.setVisibility(View.VISIBLE);
AdParam adParam = new AdParam.Builder().build();
hwBannerView.loadAd(adParam);
hwBannerView.setAdListener(adListener);
} private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), REQUEST_SIGN_IN_LOGIN);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_sign) {
signIn();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());
Bundle bundle = new Bundle();
bundle.putString(TAG,huaweiAccount.getDisplayName() + " signIn success ");
Analystics.getInstance(this).setEvent("login",bundle);
Intent intent = new Intent(this, ShowsActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();
} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}
}
}

activity_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Movie Show"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="34sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_sign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@color/colorPrimary"
android:text="Login With Huawei Id"
android:textColor="@color/colorAccent"
android:textStyle="bold" />
<com.huawei.hms.ads.banner.BannerView
android:id="@+id/huawei_banner_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
hwads:adId="testw6vs28auh3"
hwads:bannerSize="BANNER_SIZE_360_57"/>
</LinearLayout> </ScrollView></RelativeLayout>

App Build Result

Tips and Tricks

Check whether the Logcat log data on the device is reported successfully. If the log contains resultCode: 200, the data is reported successfully. If a report failure occurs, possible causes are as follows:

Cause 1: The tools:node parameter is set to replace (that is, tools:node=replace). As a result, some configurations of your app may conflict with those of the AppGallery Connect SDK, and therefore an error will occur when the AppGallery Connect API is called to obtain the token.

Cause 2: The configuration for excluding Analytics Kit from obfuscation is not added before you build the APK. As a result, an exception occurs during the running of the APK.

Conclusion

In this article, we have learned how to integrate Huawei Analytics and Ads in Android application. After completely read this article user can easily implement ads and analytics in the android based application.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

HMS Docs:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050745149

--

--