How to use Huawei Ads with Google AdMob mediation (Banner Ads)

Chia Leung Ho
5 min readOct 26, 2020

If you are a mobile app developer, your mobile app already has Google AdMob integrated and you would like to earn even more — you are at the right place.

Huawei alone has shipped 240.6 million smartphones in the year of 2019, that is almost 50 million more than Apple’s (source). Therefore, wouldn’t it make sense to earn even more with Huawei Ads?

The good news is, if you are already using Google AdMob, you could use its mediation feature to add Huawei Ads as one of your Ad sources to maximize your profit!

Okay, enough of my blah blahs, let’s jump right into work!

Summary:

  1. Set up and configure your Google AdMob account (skip this step if you know what I am talking about)
  2. Set up your Android app project with Google AdMob (skip this step if you already have an existing Android app project with Google AdMob integrated)
  3. Integrate Huawei Ads Kit and create a custom event adapter
  4. Create a mediation group and add our custom adapter as one of the Ad sources
  5. Extra — Sign up for Huawei Ads on Huawei AppGallery Connect console and create Ad Unit

Set up and configure your Google AdMob account

Head to https://apps.admob.com/v2/home and login with your Google account. Make sure you activate your Google AdMob account by entering your payment details.

Go to Apps > Add app, select Android platform and follow the set up wizard. Take note of the App ID, you’ll need it later!

Congrats!

Click the button NEXT: CREATE AD UNIT, select Banner, enter an Ad unit name and click CREATE AD UNIT

That’s it for now, we’ll head to the next step

Set up your Android app project with Google AdMob

Create a new Android app project (Empty Activity)

Add Google AdMob dependency to your module level build.gradle and click Sync Now to do a gradle sync

Add internet permission and app-id meta-data in your AndroidManifest.xml. Replace the meta-data value with the App ID you get from the previous step (after adding an App in Google AdMob console)

Now it’s time to finally do something fun! Go to res/layout/activity_main.xml, replace the Hello World TextView with a Google AdView. Replace the adUnitId value with the id you get after creating a banner ad unit

And in your MainActivity.kt, initialize Google AdMob and load the banner ad

You can try running the app on your emulator / phone at this point, however, you might not see anything if your Google AdMob account is new and not activated. Use a test banner ads ca-app-pub-3940256099942544/6300978111 instead (source)

With a test banner!

So now you have a very elegant App showing the test banner ads. To proceed to the next step, make sure you use your Ad Unit instead of the test banner!

Integrate Huawei Ads Kit and create a custom event adapter

Add Huawei’s repository in your project level build.gradle

Add Huawei Ads Kit dependency in your module level build.gradle

Add a new package and give it a name (eg: HuaweiCustomBannerEvent)

We need a total of 4 classes, let’s create them all at once and go through them one by one. Right click the newly created package, select New > Kotlin File/Class and create these class files:

  • HuaweiCustomBannerAdapter
  • HuaweiCustomBannerEventForwarder
  • HuaweiCustomBannerListener
  • HuaweiCustomBannerView

This is how your directory should look like at this point:

Now let’s work on the HuaweiCustomBannerListener. Inherit com.huawei.hms.ads.AdListener class and implement all the required abstract functions. (Click Ctrl+o to see all the Override Members)

Now, for HuaweiCustomBannerView, take context as the constructor and make this class a type of com.huawei.hms.ads.banner.BannerView. Make a function to load ad as follow

Next, for HuaweiCustomBannerEventForwarder, take CustomEventBannerListener and our HuaweiCustomBannerView as the constructor and inherit our HuaweiCustomBannerListener. Our custom event must notify mediation via our HuaweiCustomBannerListener interface when it loads or failed to load. Otherwise, the custom event times out, and ad mediation moves on to the next network.

Finally, our HuaweiCustomBannerAdapter. This class will get triggered when mediation decided that our Huawei Banner Ad should be displayed.

Are you still follwing? We are almost there. Now, the last step.

Create a mediation group and add our custom adapter as one of the Ad sources

Head to your Google AdMob console > Mediation > Create Mediation Group. Choose Banner as the Ad format and Android as the platform.

Enter the group name and add the banner ad unit we have created in the first step.

Under the Ad source section, you’ll see that the AdMob network is automatically added and set as the default Ad source. Go ahead and click Add Custom Event. Enter the label, a manual eCPM of any figure for now (eg: $5.00) and click Continue

Next, enter the full class path to our adapter class. For this case it’s com.yourpackage.name.HuaweiCustomBannerEvent.HuaweiCustomBannerAdapter. Enter the Huawei test banner ad id testw6vs28auh3 as the parameter. (For the sake of simplicity, we’ll only use test ads for now)

Remember to use your banner Ad Unit id in your activity_main.xml

Click Save and you are done! Run your app and you should see your Huawei Ads!

Yes, this is a test banner ad from Huawei. In order to serve real ads, you’ll have to sign up a Huawei AppGallery Connect account, create ads and replace the ad unit ID in the parameter field

For more information regarding Huawei Ads, refer to the official documentation here

For more information regarding Google AdMob mediation, refer to the official documentation here

Full source code : https://github.com/clho40/adapter-admob-huaweiads-banner

Thank you!

--

--