[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

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 helps 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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723090858.12314514709002024112914716937413:50510803015924:2800:0E3B1D2E6EB0EA73B19FEB3604BDFA7FAEEEE3B3A5D9ADF833CEE4BADF10BAD9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723090858.12314514709002024112914716937413:50510803015924:2800:0E3B1D2E6EB0EA73B19FEB3604BDFA7FAEEEE3B3A5D9ADF833CEE4BADF10BAD9.png[/IMG]

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723091030.90290197463438857405729098448591:50510803015924:2800:F51E6136C949D1676C11EAD9F608A2BD5FF2450444C420196E340CAB479A2676.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723091030.90290197463438857405729098448591:50510803015924:2800:F51E6136C949D1676C11EAD9F608A2BD5FF2450444C420196E340CAB479A2676.png[/IMG]

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113049.31100981585503871183385266685252:50510803015924:2800:436E5F6124CA5B0C05CFFF3012BA70F9E4A3911D4602804325B6154C4292EB5E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113049.31100981585503871183385266685252:50510803015924:2800:436E5F6124CA5B0C05CFFF3012BA70F9E4A3911D4602804325B6154C4292EB5E.jpg[/IMG]​

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112140.19488181853115282199003074755790:50510803015924:2800:EFACB6C94C1CC90F7C60907BE459993A11382948146981E0E2D7B52170AED014.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112140.19488181853115282199003074755790:50510803015924:2800:EFACB6C94C1CC90F7C60907BE459993A11382948146981E0E2D7B52170AED014.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112219.85243906162508921397108203678531:50510803015924:2800:6084BEDE541558FB0BB9743BE79AA7E893826F642BDCC9B223CB7019E0CD0B73.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112219.85243906162508921397108203678531:50510803015924:2800:6084BEDE541558FB0BB9743BE79AA7E893826F642BDCC9B223CB7019E0CD0B73.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112301.57399891113624237052474425728453:50510803015924:2800:27CA8010F786514FDDC70363F8D5838CF9F92AD0F0C56A95A49604DCE9C59639.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723112301.57399891113624237052474425728453:50510803015924:2800:27CA8010F786514FDDC70363F8D5838CF9F92AD0F0C56A95A49604DCE9C59639.png[/IMG]​

Development for Non Huawei device supporting GMS

Integrating AdMob Banner AD

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 the banner AdView in activity_main.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.
}
}

);
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113049.31100981585503871183385266685252:50510803015924:2800:436E5F6124CA5B0C05CFFF3012BA70F9E4A3911D4602804325B6154C4292EB5E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113049.31100981585503871183385266685252:50510803015924:2800:436E5F6124CA5B0C05CFFF3012BA70F9E4A3911D4602804325B6154C4292EB5E.jpg[/IMG]​

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.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113538.36458104567374533450958222085321:50510803015924:2800:E1CD307E29EAF1FA26DAC07DE7ACA6998F8E69A268EA98F12199E84B99CE52EE.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113538.36458104567374533450958222085321:50510803015924:2800:E1CD307E29EAF1FA26DAC07DE7ACA6998F8E69A268EA98F12199E84B99CE52EE.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113642.39918255513844790977346258278342:50510803015924:2800:B66B247592B66F2442E8BDB60BD00070764B3B12491AA1E3ED5E09E8AA36D987.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113642.39918255513844790977346258278342:50510803015924:2800:B66B247592B66F2442E8BDB60BD00070764B3B12491AA1E3ED5E09E8AA36D987.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113728.61234974383465934948808232988684:50510803015924:2800:61C6F11A6322B19BAA24FDDD14AF423423F2F9891FA566D8F59A2EC5136615EF.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200723113728.61234974383465934948808232988684:50510803015924:2800:61C6F11A6322B19BAA24FDDD14AF423423F2F9891FA566D8F59A2EC5136615EF.jpg[/IMG]​

To check if device has HMS or GMS installed

public class Utility {

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:

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

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