Overview

Ads Kit provides the open advertising identifier (OAID) for you to deliver personalized ads and monetize traffic on Huawei devices.

OAID Capabilities

Based on OAIDs, you can serve personalized marketing campaigns and commercial ads to users, thereby improving conversions. The process is as follows:

  1. You can call APIs provided by Ads Kit to obtain a user's OAID and setting of Disable personalized ads.
  2. If the user toggles off Disable personalized ads, you can serve personalized marketing campaigns and commercial ads to the user based on the audiences to which the user's OAID belongs.
  3. If the user clicks an ad displayed in your app, your app downloads the advertised app from HUAWEI AppGallery and installs it on the user's device.
  4. Your app carries the OAID in impression, click, download, and other events to track and analyze the effect of marketing campaigns or commercial ads.

What You Will Create

In this codelab, you will create an app for traffic monetization via Ads Kit. Your app will be able to obtain users' OAIDs and settings of Disable personalized ads.

What You Will Learn

In this codelab, you will learn how to integrate the OAID capabilities of Ads Kit.

Hardware Requirements

A Huawei phone, tablet, or smart display running EMUI 3.0 or later

Software Requirements

Required Knowledge

Android app development basics

Click here to view and download the sample code of the OAID-Initial project.
Use Android Studio to open the OAID-Initial project.

Integrating the HUAWEI Ads SDK

  1. Configure the Maven repository address.

    The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.

    Gradle plugin earlier than 7.0

    Gradle plugin 7.0

    Gradle plugin 7.1 or later

    • Gradle plugin earlier than 7.0
      i.  Open your project-level build.gradle file.


      ii.  Add the Maven repository.

      • Configure the Maven repository address under buildscript > repositories.
      • Configure the Maven repository address under allprojects > repositories.
      buildscript { repositories { google() jcenter() // Configure the Maven repository address for the HUAWEI Ads SDK. maven {url 'https://developer.huawei.com/repo/'} } } allprojects { repositories { google() jcenter() // Configure the Maven repository address for the HUAWEI Ads SDK. maven {url 'https://developer.huawei.com/repo/'} } }
    • Gradle plugin 7.0
      i.  Open your project-level build.gradle file.


      ii.  Add the Maven repository.

        Configure the Maven repository address under buildscript > repositories.

      buildscript { repositories { google() jcenter() // Configure the Maven repository address for the HUAWEI Ads SDK. maven {url 'https://developer.huawei.com/repo/'} } }

      iii.  Open the project-level settings.gradle file and configure the Maven repository address.

      dependencyResolutionManagement { ... repositories { google() jcenter() // Configure the Maven repository address for the HUAWEI Ads SDK. maven {url 'https://developer.huawei.com/repo/'} } }
    • Gradle plugin 7.1 or later
      Open the project-level settings.gradle file and configure the Maven repository address.

      pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() // Configure the Maven repository address for the HUAWEI Ads SDK. maven { url 'https://developer.huawei.com/repo/' } } } dependencyResolutionManagement { ... repositories { google() mavenCentral() // Configure the Maven repository address for the HUAWEI Ads SDK. maven { url 'https://developer.huawei.com/repo/' } } }
  2. Configure build dependencies.
    Add build dependencies to the build.gradle file in the app directory of your project, and replace {version} with the actual version of the HUAWEI Ads SDK. For details, please refer to Version Change History.
    dependencies { implementation 'com.huawei.hms:ads-identifier:{version}' }
  3. Synchronize the modified files.
  4. Configure obfuscation scripts in the app/proguard-rules.pro file.
    -keep class com.huawei.hms.ads.** { *; } -keep interface com.huawei.hms.ads.** { *; }

Calling Static Method getAdvertisingIdInfo to Obtain OAID Information

Create a subthread and call the static method AdvertisingIdClient.getAdvertisingIdInfo(mContext) to obtain the OAID information. Do not call this method in the main thread. The sample code is as follows:

public static void getOaid(Context context, OaidCallback callback) { if (null == context || null == callback) { Log.e(TAG, "invalid input param"); return; } try { // Obtain the OAID information. Do not call this method in the main thread. AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context); if (null != info) { callback.onSuccess(info.getId(), info.isLimitAdTrackingEnabled()); } else { callback.onFail("oaid is null"); } } catch (IOException e) { Log.e(TAG, "getAdvertisingIdInfo IOException"); callback.onFail("getAdvertisingIdInfo IOException"); } }

Compile the OAID-Initial project to generate an APK file and install the APK file on a device. After the app is launched, the page shown in the figure on the left is displayed. You can tap OAID Sample to view the result in the figure on the right.

Well done. You have successfully completed this codelab and learned how to integrate the OAID capabilities of Ads Kit.

For more information, please refer to related documents.
You can also click here and download the source code.

Code copied