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

What if I tell you is possible generate money even if your app can be downloaded for free?, would you like it? Well, if the answser is yes let me introduce you the Huawei Ads Kit. In this article I’m going to show you how to place ads in you app.

Note: The Ads service is only available for enterprise accounts, support for individual accounts will be added by the end of august 2020.

Supported Ad Types

Banner ads: A rectangular small add.

Native ads: Get ad data and adjust it to your own layout. Native ads are thinked to fit seamlessly into the surrounding content to match your app design.

Rewarded ads: Full-screen video ads that reward users for watching

Interstitial Ads: Full-screen ads displayed when a user starts, pauses, or exits an app, without disrupting the user's experience.

Splash Ads: Splash ads are displayed immediately after an app is launched, even before the home screen of the app is displayed.

Prerequisites

An app project in App Gallery Connect

An android project with the HMS core SDK integrated

Integrating the SDK

Add the following lines to your app level build.gradle file

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

Initializing the SDK

You can initialize the SDK by the app startup or when an activity is created. Add the next to your Application/Activity class:

override fun onCreate() {
super.onCreate()
HwAds.init(this)//Add this line
}

Adding a Banner Ad

Go to the view of the fragment/activity where you want to put your ad and add a BannerView

<com.huawei.hms.ads.banner.BannerView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
hwads:adId="testw6vs28auh3"
hwads:bannerSize="BANNER_SIZE_360_57"
android:layout_gravity="bottom"/>

Now go to your Fragment/Activity and load the ad

fun loadBannerAd(){
val bannerView=view?.banner
bannerView?.adId = getString(R.string.ad_id)
val adParam = AdParam.Builder().build()
bannerView?.adListener= MyAdListener(requireContext(),"Banner Ad")
bannerView?.loadAd(adParam)
}

Creating an event listener

The event listener allows you to be informed if the ad has been loaded, clicked, closed or if the load fails

package com.hms.example.dummyapplication.utils

import android.content.Context
import android.widget.Toast
import com.huawei.hms.ads.AdListener

class MyAdListener(private val context: Context,private val adTag:String): AdListener() {
override fun onAdClicked() {

}

override fun onAdClosed() {
Toast.makeText(context,"$adTag allows you enjoy the app for free", Toast.LENGTH_SHORT).show()
}

override fun onAdFailed(p0: Int) {
Toast.makeText(context,"Failed to load $adTag code: $p0", Toast.LENGTH_SHORT).show()
}

override fun onAdImpression() {

}

override fun onAdLeave() {

}

override fun onAdLoaded() {
Toast.makeText(context,"$adTag Loaded", Toast.LENGTH_SHORT).show()
}

override fun onAdOpened() {

}
}

Adding an splash ad

Create a new empty activity and add the Splash Ad view to the layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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=".activity.AdsActivity">

<com.huawei.hms.ads.splash.SplashView
android:id="@+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Go to your activity and load your ad

fun loadAd(){
//Create an intent to jump to the next acivity
val intent=Intent(this,NavDrawer::class.java)

//Add a listener to jump when the ad finished or if the load fails
val splashAdLoadListener: SplashAdLoadListener = object : SplashAdLoadListener() {
override fun onAdLoaded() {
// Called when an ad is loaded successfully.
}

override fun onAdFailedToLoad(errorCode: Int) {
// Called when an ad failed to be loaded. The app home screen is then displayed.
startActivity(intent)
}

override fun onAdDismissed() {
// Called when the display of an ad is complete. The app home screen is then displayed.
startActivity(intent)
}
}

val splashView = splash//find view by id is not needed in kotlin
val id=getString(R.string.ad_id2)
val orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
val adParam = AdParam.Builder().build()
splashView.load(id, orientation, adParam, splashAdLoadListener)
}

Finally go to your Android Manifest and changue the launcher activity

<activity android:name=".activity.AdsActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Testing

You must use the test ad id during the app development, this avoids invalid ad clicks during the test.

Test ID: "testw6vs28auh3"

An easy way is creating resourses for debug and release build types so you dont have to change it manually for release. You can make it creating different resource files (strings.xml) for each build type or modifying your build.gradle file like the next:

buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable false

resValue "string","ad_id","formal ad slot ID"
resValue "string","ad_id2","formal ad slot ID"
resValue "string","ad_id3","formal ad slot ID"
}
debug {
signingConfig signingConfigs.release
debuggable true
resValue "string","ad_id","testw6vs28auh3"
resValue "string","ad_id2","testw6vs28auh3"
resValue "string","ad_id3","testw6vs28auh3"
}
}

Conclusion

The ads kit allows you use the space in your app to monetize by displaying ads. You can use different types of ads according your necessities or app behavior, rewarded ads are very useful for games.

Reference

Development guide

Service Description

Check this and other demos: https://github.com/danms07/dummyapp