[Huawei Developers] Freemind R:
In this article, I will try to explain how you can easily implement Huawei Banner Ads (In Kotlin) to your project and monetize your application right away!

What is Banner Ad

Banner ads are rectangular images that can be placed to the top, middle, or bottom of your app’s screen. Banner ads refresh automatically at regular intervals. When a user taps a banner ad, the user is redirected to the advertiser’s page in most cases.

To implement banner ads, you need to implement Huawei Ads Kit dependency to your project which I explained below.

How to Implement Ads Kit

Things you need to have

1) A Huawei Developer Account (It needs to be Enterprise Level account for monetization)

2) A Huawei phone with HMS 4.0.0.300 or later

3) A computer with Android Studio , Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

Things Need To Be Done Before Implementation

First things first HMS Core needs to be implemented to the your project.

After HMS Core implementation, Ads Kit dependency needs to be added in app level “build.gradle” file

<pre class="brush:java;toolbar:false;">dependencies {
...
//Huawei Ads Kit Dependency
implementation 'com.huawei.hms:ads-lite:13.4.32.300'
...
} </pre><p style="white-space: normal;">
</p><p style="white-space: normal;">4) Configure obfuscation scripts to prevent HUAWEI Ads kit from being obfuscated. Add the following two lines of code to the <strong>app/proguard-rules.pro</strong></p><p style="white-space: normal;"><strong>
</strong></p><pre class="brush:java;toolbar:false">-keep class com.huawei.openalliance.ad.** { *; }
-keep class com.huawei.hms.ads.** { *; }
</pre><p style="white-space: normal;">
</p><p style="white-space: normal;">After Completing the steps above. All you need to do is initialize Huwei Ads by calling HwAds.init() method in your application. Then the application is ready to implement all kind of various ad types which I will show below.</p><pre class="brush:java;toolbar:false">override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
// Initialize the HUAWEI Ads SDK.
HwAds.init(this);
...
} </pre>

Implementing Banner Ads

To implement Banner Ad, BannerView object needs to be initialized either with XML or directly in code.

Adding Banner Ads Through XML

Create BannerView in your XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hwads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BannerAdsActivity">
...
<!--Adding BannerView Through XML-->
<com.huawei.hms.ads.banner.BannerView
android:id="@+id/hwBannerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
hwads:adId="testw6vs28auh3"
hwads:layout_constraintEnd_toEndOf="parent"
hwads:layout_constraintStart_toStartOf="parent"
hwads:layout_constraintTop_toTopOf="parent"
hwads:bannerSize="BANNER_SIZE_360_57"
/>
...
</androidx.constraintlayout.widget.ConstraintLayout>

After creating the your BannerView through XML file, call it from your class & load the add.

override fun onCreate(savedInstanceState: Bundle?) {
....
// Obtain BannerView based on the configuration in XML layout.
val adParam = AdParam.Builder().build();
hwBannerView.loadAd(adParam);
...
}

Run the application and the result will be like below.

Adding Banner Ads Programmatically Without XML

Lets create another Banner ad at the bottom of our view without using XML. All you need to do is creating & initializing banner ad , adding it to the specific layout, then loading the ad as below.

override fun onCreate(savedInstanceState: Bundle?) {
...
val adParam = AdParam.Builder().build();
val bannerView = BannerView(this);
// "testw6vs28auh3" is a dedicated test ad slot ID. To get real one, you need to have an enterprise level Huawei developer account.
bannerView.setAdId("testw6vs28auh3");
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_320_50);
val parentLayout = findViewById<ConstraintLayout>(R.id.clBannerMain);
val set = ConstraintSet();
// set view id, else getId() returns -1
bannerView.id = View.generateViewId()
parentLayout.addView(bannerView,0)
set.clone(parentLayout)
set.connect(bannerView.id, ConstraintSet.BOTTOM, parentLayout.id, ConstraintSet.BOTTOM, 0);
set.applyTo(parentLayout);
bannerView.loadAd(adParam)
...
}

When you run the code above you will see we have succesfully added a second banner ad to the bottom of the our view.

https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/023/106/056/0890090000023106056.20200827074704.78852832128128279354621703409783:50510914075520:2800:862740C17733B87A8CEAA65FD7B62221F9214A105D3414605B35C5EF96A0F1AE.png

Optional: Adding Ad Listener

After implementing banner ads to your project, if you want to proceed further, you can add Ad listener to listen ad events like AdLoaded is AdFailed is AdClicked etc. To do that, simply add an Ad Listener to your project like below

override fun onCreate(savedInstanceState: Bundle?) {
...
val adListener = object : AdListener() {
override fun onAdImpression() {
super.onAdImpression()
}
override fun onAdFailed(p0: Int) {
super.onAdFailed(p0)
}
override fun onAdLeave() {
super.onAdLeave()
}
override fun onAdClicked() {
super.onAdClicked()
}
override fun onAdClosed() {
super.onAdClosed()
}
override fun onAdOpened() {
super.onAdOpened()
}
override fun onAdLoaded() {
super.onAdLoaded()
}
}
//Add Ad Listener to which ad you want to listen.
bannerView.adListener = adListener
....
}

As you can see, it is really easy to implement banner ads to your android application with Huawei Ads Kit. Stay tuned for other ad types. You can find the github project below.

Resources

Disav0wed/Huawei_Banner_Ads_Demo：https://github.com/Disav0wed/Huawei_Banner_Ads_Demo

Official Huawei Documentation：https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/ads-sdk-guide-banner

Codelab：https://developer.huawei.com/consumer/en/codelab/HUAWEIAdsSDK-BannerAds/index.html#2