Overview

Rewarded ads are full-screen video ads that users opt to view in exchange for in-app rewards.
This codelab will step you through the process of adding a rewarded ad and correctly displaying it in your app.

What You Will Create

In this codelab, you will build an empty app and integrate a rewarded ad into it.
Your app will:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To view the HMS Core (APK) version on a device, go to Settings > Apps > Apps and search for HMS Core.

Opening a Project in Android Studio

  1. Start Android Studio.
  2. Start a new project, select Empty Activity, and name the project RewardAdExample.
  3. Click Finish.

Configuring the HUAWEI Ads SDK Repository and Dependency Package

Configure the Maven repository address in the project-level build.gradle file.

buildscript { repositories { google() jcenter() maven { url 'https://developer.huawei.com/repo/' } } ... } allprojects { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }

Configure the dependency package in the app-level build.gradle file.

dependencies { ... implementation 'com.huawei.hms:ads-lite:13.4.32.300' ... }

Click Sync Now and wait until synchronization is complete.

Configuring Obfuscation Scripts

Before building the APK, configure obfuscation scripts to prevent the HUAWEI Ads SDK from being obfuscated. If obfuscation arises, the HUAWEI Ads SDK may not function properly.

  1. Open the obfuscation script file of your Android project.
  2. Add the following two lines of code to the app/proguard-rules.pro file to exclude the HUAWEI Ads SDK from obfuscation:
    -keep class com.huawei.openalliance.ad.** { *; } -keep class com.huawei.hms.ads.** { *; }

To obtain ads using the HUAWEI Ads SDK, you must first initialize the SDK in your app. This process must be completed during app launch. You do not need to repeat this process when the app is running.
Call HwAds.init() in the onCreate() method of the MainActivity class to initialize the HUAWEI Ads SDK.
MainActivity.java

import com.huawei.hms.ads.HwAds; ... public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the HUAWEI Ads SDK. HwAds.init(this); ... } ... }

Create RewardAd to load a rewarded ad.
MainActivity.java

private void createRewardAd(){ // "testx9dtjwj8hp" is test reward ad slot id rewardAd = new RewardAd(MainActivity.this, "testx9dtjwj8hp"); }

Create a button for watching a rewarded ad.
activity_main.xml

<Button android:id="@+id/show_video_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="104dp" android:layout_centerHorizontal="true" android:text="@string/watch_video_button_text" />

strings.xml

<resources> ... <string name="watch_video_button_text">watchVideo</string> ... </resources>

MainActiviry.java

private void loadWatchVideoButton(){ watchAdButton = findViewById(R.id.show_video_button); watchAdButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rewardAdShow(); } }); }

Create a view for displaying scores.
activity_main.xml

<TextView android:id="@+id/coin_count_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:layout_centerHorizontal="true" android:textAppearance="?android:attr/textAppearanceLarge" />

MainActivity.java

private void loadScoreView() { scoreView = findViewById(R.id.coin_count_text); scoreView.setText("Score:" + score); }

Call the loadAd() method to load a rewarded ad.
Create RewardAdLoadListener to listen to the ad loading status.
MainActivity.java

private void loadRewardAd() { if (rewardAd == null) { createRewardAd(); } RewardAdLoadListener rewardAdLoadListener = new RewardAdLoadListener() { @Override public void onRewardAdFailedToLoad(int errorCode) { Toast.makeText(MainActivity.this, "onRewardAdFailedToLoad errorCode is :" + errorCode, Toast.LENGTH_SHORT) .show(); } @Override public void onRewardedLoaded() { Toast.makeText(MainActivity.this, "onRewardedLoaded", Toast.LENGTH_SHORT).show(); } }; rewardAd.loadAd(new AdParam.Builder().build(), rewardAdLoadListener); }

Call the isLoaded() method to confirm that a rewarded ad has been loaded, and call the show() method of the RewardAd object to display the ad.
MainActivity.java

private void rewardAdShow() { if (rewardAd.isLoaded()) { rewardAd.show(MainActivity.this, new RewardAdStatusListener() { @Override public void onRewardAdClosed() { loadRewardAd(); } @Override public void onRewardAdFailedToShow(int errorCode) { Toast .makeText(MainActivity.this, "onRewardAdFailedToShow errorCode is :" + errorCode, Toast.LENGTH_SHORT) .show(); } @Override public void onRewardAdOpened() { Toast.makeText(MainActivity.this, "onRewardAdOpened", Toast.LENGTH_SHORT).show(); } @Override public void onRewarded(Reward reward) { Toast .makeText(MainActivity.this, "Watch video show finished, add " + reward.getAmount() + " scores", Toast.LENGTH_SHORT) .show(); // You are advised to grant a reward immediately and at the same time, check whether the reward takes effect on the server. // If no reward information is configured, grant a reward based on the actual scenario. addScore(reward.getAmount()); loadRewardAd(); } }); } }

The sample code is as follows:

private void addScore(int amount){ score += amount; scoreView.setText("Score:" + score); }

For details about the methods of RewardAdStatusListener, please refer to the API Reference.
After running the project, you will see the rewarded ad in your app.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following links:

You can click the button below to download the source code.

Download source code

Code copied