HUAWEI Ads Kit provides the open advertising identifier (OAID) for you to deliver personalized ads and monetize traffic on Huawei devices.
You can provide personalized marketing activities or commercial ads for users based on OAIDs to improve the conversion effect.
In the codelab, you will create an app to access HUAWEI Ads. Your app will obtain an OAID and setting of Disable personalized ads.
In this codelab, you will learn how to integrate the OAID capabilities of HUAWEI Ads Kit.
A Huawei mobile phone or tablet.
HMS Core (APK) 2.6.2 or later.
To view the HMS Core (APK) version on a device, go to Settings > Apps > Apps and search for HMS Core.
Download the OAID-Initial project.
Use Android Studio to open the OAID-Initial project.
1. Integrate the SDK.
Step 1: Configure the Maven repository address for the HUAWEI Ads SDK.
Open the build.gradle
file in the root directory of your Android Studio project. Go to allprojects > repositories
and configure the Maven repository address for the SDK.
allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
Step 2: Configure build dependencies.
Open the build.gradle
file in the app
directory. Configure build dependencies, and replace {version} with the latest version of HUAWEI Ads SDK. For details, please refer to Version Change History.
dependencies {
implementation 'com.huawei.hms:ads-identifier:{version}'
}
Step 3: Synchronize the modified files.
Step 4: (Mandatory) Configure the obfuscation scripts.
app/proguard-rules.pro
-keep class com.huawei.hms.ads.** { *; }
-keep interface com.huawei.hms.ads.** { *; }
2. Call the static method getAdvertisingIdInfo
to obtain the OAID.
Create a subthread and call the static method AdvertisingIdClient.getAdvertisingIdInfo(mContext)
to obtain the OAID information. Do not call this method in a main thread. The sample code is as follows:OaidSdkUtil.java
public static void getOaid(Context context, OaidCallback callback) {
if (null == context || null == callback) {
Log.e(TAG, "invalid input param");
return;
}
try {
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
if (null != info) {
callback.onSuccuss(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 following information is displayed. You can click OAID Sample to view the result in the right figure.
Well done. You have successfully completed this codelab and learned how to:
For more information, please click the following link:
You can click the button below to download the source code.