Integrating HUAWEI Ads kit together with Google AdMob in single Application

RChancha
7 min readAug 7, 2020

What is Ad Kit

Ad kit service makes it easy for developers to raise money with high-quality advertising from mobile devices. It maximizes the value of every impression by combining global advertiser demand, innovative ad formats, and advanced app monetization technology.

Showing ads to app users help you to create a reliable revenue stream to help your company grow while concentrating on creating and improving quality apps. Advertisers can attract new consumers and users can discover related goods and services while enjoying free apps. So it is a win for developers, users, and advertisers.

In this article we will integrate banner ad and reward video ad provided by Google Admob and Huawei Ad kit in single application.

  • Banner Ad

Rectangular advertising on the top or bottom of the screen show. When users interact with the app, banner ads remain on the screen and it can be refreshed periodically for a period of time.

  • Reward Ad

Ads which reward users for watching short videos, interacting with ads and surveys that are playable. Perfect for free-to-play players to monetize.

Usecase

1. We will develop small application “Wheel of fortune”. User can spin wheel to earn coins.

2. We will show banner ad at the bottom of app

3. User will spin the wheel of fortune, if coins earned are 6 or less ,he can earn additional coin by watching reward ads.

4. If device supports HMS, we will show Huawei ads.

5. If device supports GMS, we will show Google AdMob ads.

Initial Setup

  • Huawei Ad kit

1) Create a project in android studio

2) Get the SHA Key. For getting the SHA key we can refer to this article.

3) Create an app in the Huawei app gallery connect.

4) Provide the SHA Key in App Information Section.

5) Provide storage location.

6) After completing all the above points we need to download the agconnect-services.json from App Information Section. Copy and paste the Json file in the app folder of the android project.

7) Enter the below maven url inside the repositories of buildscript and allprojects ( project build.gradle file )

maven { url 'http://developer.huawei.com/repo/' }

8) Enter the below maven url inside the repositories of buildscript and allprojects (project build.gradle file)

implementation 'com.huawei.hms:ads-lite:13.4.30.307'

9) Provide following permissions in manifest.

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

1. Create a Google AdMob account and register an app.

2. Open app-level gradle file, and add following dependencies.

dependencies {
implementation 'com.google.android.gms:play-services-ads:19.2.0'
}

3. Create APP ID in admob dashboard. Refer this to create APP ID.

4. Add APP ID in Android manifest.

<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>
</manifest>

Development for Huawei device supporting HMS

  • Integrating Huawei Banner AD
  1. Add bannerview in activity_main.xml
<RelativeLayout xmlns:hwads="http://schemas.android.com/apk/res-auto" >
<com.huawei.hms.ads.banner.BannerView
android:id="@+id/huawei_banner_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="gone"
hwads:adId="testw6vs28auh3"
hwads:bannerSize="BANNER_SIZE_360_57"/>

Note: We are using test ad ID. “testw6vs28auh3” is a dedicated test ad slot ID.

2. To show Banner Ad

if(Utility.isDeviceHuaweiManufacturer() && Utility.isHuaweiMobileServicesAvailable(this)) {
hwBannerView = findViewById(R.id.huawei_banner_view);
hwBannerView.setVisibility(View.VISIBLE);
// Create an ad request to load an ad.
AdParam adParam = new AdParam.Builder().build();
hwBannerView.loadAd(adParam);
hwBannerView.setAdListener(adListener);
} private AdListener adListener = new AdListener() {
@Override
public void onAdLoaded() {
// Called when an ad is loaded successfully.
Log.d(TAG , "onAdLoaded");
}
@Override
public void onAdFailed(int errorCode) {
// Called when an ad fails to be loaded.
Log.d(TAG , "onAdFailed");
}
@Override
public void onAdOpened() {
// Called when an ad is opened.
Log.d(TAG , "onAdOpened");
}
@Override
public void onAdClicked() {
// Called when a user taps an ad.
Log.d(TAG , "onAdClicked");
}
@Override
public void onAdLeave() {
// Called when a user has left the app.
Log.d(TAG , "onAdLeave");
}
@Override
public void onAdClosed() {
// Called when an ad is closed.
Log.d(TAG , "onAdClosed");
}
};
  • Integrating Huawei Reward Video AD
  1. To show Huawei reward Ads
if (Utility.isHuaweiMobileServicesAvailable(MainActivity.this) && Utility.isDeviceHuaweiManufacturer()) {
showHuaweiRewardAds();
}

private void showHuaweiRewardAds() {
// Test ad slot ID
final String AD_ID = "testx9dtjwj8hp";
final RewardAd rewardAd;
rewardAd = new RewardAd(this, AD_ID);
RewardAdLoadListener listener= new RewardAdLoadListener() {
@Override
public void onRewardedLoaded() {
// Rewarded ad loaded successfully.
if (rewardAd.isLoaded()) {
rewardAd.show(MainActivity.this, new RewardAdStatusListener() {
@Override
public void onRewardAdOpened() {
// Rewarded ad opened.
Log.d(TAG , "rewardAd loaded");
}
@Override
public void onRewardAdFailedToShow(int errorCode) {
// Failed to display the rewarded ad.
Log.d(TAG , "rewardAd failed error : " + errorCode);
}
@Override
public void onRewardAdClosed() {
// Rewarded ad closed.
}
@Override
public void onRewarded(Reward reward){

showRewardAdAlertDialog( "You have been rewarded " +
String.valueOf(reward.getAmount()) +" "+ reward.getName() + " for watching Ad");
}
});
}
}
@Override
public void onRewardAdFailedToLoad(int errorCode) {
// Failed to load the rewarded ad.
}
};
rewardAd.loadAd(new AdParam.Builder().build(), listener);

}

2. The reward object is passed in the onRewarded method. Call the reward.getAmount() method to obtain the reward amount and the reward.getName() method to obtain the name of the reward.

public void onRewarded(Reward reward){
// Reward earned by the user.
//
TODO: Reward the user.
// Toast.makeText(MainActivity.this , reward.getName() +", "+ reward.getAmount() , Toast.LENGTH_LONG).show();
showRewardAdAlertDialog( "You have been rewarded " +
String.valueOf(reward.getAmount()) +" "+ reward.getName() + " for watching Ad");
}
Reward Ad on Huawei device supporting HMS

Development for Non Huawei device supporting GMS

  • Integrating AdMob Banner AD
  1. Before loading ads, we have to initialize the Mobile Ads SDK by calling MobileAds.initialize() which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at app launch.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

2. Add Banner ad view in main_activity.xml

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/google_banner_adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

Note: Here we are using test adUnitId. When building and testing your apps, make sure you use test ads rather than live, production ads

3. To load the banner Ad view

if(Utility.isGooglePlayServicesAvailable(this)) {
mGoogleAdView = findViewById(R.id.google_banner_adView);
mGoogleAdView.setVisibility(View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder().build();
mGoogleAdView.loadAd(adRequest);
mGoogleAdView.setAdListener(new com.google.android.gms.ads.AdListener()
{
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.d(TAG, "onAdLoaded");
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Log.d(TAG, "onAdFailedToLoad " + errorCode);
}

@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}

@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}

@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}

@Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
}

);
}
  • Integrating AdMob Reward Video AD
  1. To show Admob Reward Video ad.
if (Utility.isGooglePlayServicesAvailable(MainActivity.this)) {
showGoogleRewardAds();
}
private void showGoogleRewardAds() {
RewardedVideoAd mRewardedVideoAd;
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {

}

@Override
public void onRewarded(RewardItem rewardItem) {
showRewardAdAlertDialog("You have been rewarded " + rewardItem.getAmount() + " "+ rewardItem.getType() + " for watching ad");
}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {

}
});
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}

“ca-app-pub-3940256099942544/5224354917” is test Ad id for Admob reward AD.

To check if device has HMS or GMS installed

public class Utils {

public static boolean isSignedIn = false;

public static boolean isGooglePlayServicesAvailable(Activity activity) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
if(status != ConnectionResult.SUCCESS) {
if(googleApiAvailability.isUserResolvableError(status)) {
googleApiAvailability.getErrorDialog(activity, status, 2404).show();
}
return false;
}
return true;
}

public static boolean isHuaweiMobileServicesAvailable(Context context) {
if (HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context) == ConnectionResult.SUCCESS){
return true;
}
return false;
}

public static boolean isDeviceHuaweiManufacturer () {

String manufacturer = Build.MANUFACTURER;
Log.d("Device : " , manufacturer);
if (manufacturer.toLowerCase().contains("huawei")) {
return true;
}
return false;

}
}

Links:

1. https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/publisher-service-introduction-0000001050064960-V5

2. https://developers.google.com/admob/android/quick-start

--

--