Overview

HUAWEI Ads supports developers in monetizing traffic on Huawei devices, facilitates developers to trace advertisement personalization, and provides advertising identifier capabilities.

Open Capabilities of OAID

Developers can provide personalized marketing activities or commercial advertisements for users based on the OAID to improve the conversion effect. The process is as follows:

  1. The developer app can call the HUAWEI Ads API to obtain the OAID and the status of the Disable Ads Personalization switch.
  2. When the developer app requests advertising, if users do not enable Disable Ads Personalization, developers can carry out personalized marketing activities and provide commercial advertisement recommendations based on the segment groups constructed by the OAID.
  3. Advertisements are displayed in the developer app. Users can download and install the advertised apps.
  4. The developer app carries OAID in events such as exposure, clicks, and downloads to trace and analyze the effect of marketing activities or commercial advertisements.

Hardware Requirement

A Huawei mobile phone or tablet.

Software Requirement

HMS 2.6.2 or later.
How to check the HMS version: Go to Settings > Apps & notifications > Apps > Huawei Mobile Services on the device.

Download and decompress the CodeLab_AdsOAID_Demo.zip file.

Download

Use Android Studio to open the CodeLab_AdsOAID_Demo project, compile and generate the APK file, download the file to the device, and view the running result. After the app running is
over, the following information is displayed. You can select a call mode and click OAID Sample to view the result as shown in the figure on the right below.

Connection through SDK

You can connect to the open capabilities through the SDK provided by HUAWEI Ads. This connection method is recommended due to its simplicity.

  1. Integrate SDKs
    Step 1 Configure the address of the maven warehouse of the HUAWEI Ads SDK.
    Open the Android Studio project-level build.gradle file and configure the maven warehouse address of the SDK by going to allprojects > repositories.
  2. /HuaweiAdsDemo/build.gradle

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

    Step 2 Configure dependencies.
    Open the build.gradle file in the subproject app. Add the SDK dependency in dependencies.

    /HuaweiAdsDemo/app/build.gradle

    dependencies { implementation 'com.huawei.hms:hms-ads-identifier:3.4.26.303' }

    Step 3 Synchronize the modified files.

    Step 4 (Mandatory) Configure the obfuscation script.
    /HuaweiAdsDemo/app/proguard-rules.pro

    -keep class com.huawei.hms.ads.** { *; } -keep interface com.huawei.hms.ads.** { *; }
  3. Call the getAdvertisingIdInfo static method to obtain OAID information.
    Create a subthread and call the static
    AdvertisingIdClient.getAdvertisingIdInfo(mContext) method to obtain the OAID information. Do not call this method in the main thread to obtain the OAID. A sample code is shown below:
    /HuaweiAdsDemo/app/src/main/java/com/huawei/pps/hms/test/oaid/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"); } }

Connection through AIDL

This method does not require the integration of any SDKs. However, you need to be familiar
with the Android AIDL service.

  1. Add an AIDL API file.
    Add an AIDL API file in the project. The package name is com.uodis.opendevice.aidl and includes the following content:
    /HuaweiAdsDemo/app/src/main/aidl/com/uodis/opendevice/aidl/OpenDeviceIdentifierService.aidl
    package com.uodis.opendevice.aidl; /* * Important: Do not modify the method sequence of the AIDL file! */ interface OpenDeviceIdentifierService { String getOaid(); boolean isOaidTrackLimited(); }
  2. Create a class to implement the ServiceConnection API.
    Step 1 Implement the onServiceConnected method of ServiceConnection.
    Step 2 Call the OpenDeviceIdentifierService.Stub.asInterface method to obtain OpenDeviceIdentifierService.
    Step 3 Call the getOaid and isOaidTrackLimited method to obtain the OAID and the status of the Disable Ads Personalization switch.
  3. /HuaweiAdsDemo/app/src/main/java/com/huawei/pps/hms/test/oaid/OaidAidlUtil.java

    private final class IdentifierServiceConnection implements ServiceConnection { private IdentifierServiceConnection() { } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.i(TAG, "onServiceConnected"); mService = OpenDeviceIdentifierService.Stub.asInterface(iBinder); if (null != mService) { try { if (null != mCallback) { mCallback.onSuccuss(mService.getOaid(), mService.isOaidTrackLimited()); } } catch (RemoteException e) { Log.e(TAG, "getChannelInfo RemoteException"); if (null != mCallback) { mCallback.onFail(e.getMessage()); } } catch (Exception e) { Log.e(TAG, "getChannelInfo Excepition"); if (null != mCallback) { mCallback.onFail(e.getMessage()); } } finally { unbindService(); } } } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i(TAG, "onServiceDisconnected"); mService = null; } }
  4. Connect to the AIDL service for OAID.
    Step 1 Create an IdentifierServiceConnection instance.
    Step 2 Create an Intent with the Action being com.uodis.opendevice.OPENIDS_SERVICE.
    Step 3 Set the name of the Intent package to com.huawei.hwid.
    Step 4 Call the AIDL service of the bindService for IdentifierService.
  5. /HuaweiAdsDemo/app/src/main/java/com/huawei/pps/hms/test/oaid/OaidAidlUtil.java

    mServiceConnection = new IdentifierServiceConnection(); Intent intent = new Intent(SERVICE_ACTION); intent.setPackage(SERVICE_PACKAGE_NAME); boolean result = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); Log.i(TAG, "bindService result: " + result);
  6. Disconnect the AIDL service for OAID.
    After you finish using the AIDL API, you should proactively disconnect the service.
  7. /HuaweiAdsDemo/app/src/main/java/com/huawei/pps/hms/test/oaid/OaidAidlUtil.java

    if (null != mServiceConnection) { mContext.unbindService(mServiceConnection); }

Compiling, Loading, and Debugging

Once you complete the above service code, you can compile the code.
If an error occurs during compiling, modify the code based on the error information.
After the compiling is complete, the APK file is generated and installed.

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

You can read the links as follow for more information.

documentation.

Code copied