目录

This guide describes how to integrate the HUAWEI Ads SDK into your app. It is the first step toward displaying ads and earning revenue. Once the SDK is integrated, you can choose an ad format (such as native or rewarded ads) and follow the required steps to implement it.

1. Prerequisites

2. Importing the HUAWEI Ads SDK

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

  1. buildscript {
  2.    repositories {
  3.        google()
  4.        jcenter()
  5.        maven { url 'http://developer.huawei.com/repo/' }
  6.    }
  7.    ...
  8. }
  9. allprojects {
  10.    repositories {
  11.        google()
  12.        jcenter()
  13.        maven { url 'http://developer.huawei.com/repo/' }
  14.    }
  15. }

Add build dependencies to the app-level build.gradle file.

Replace {version} with the actual SDK version number, for example:

  1. dependencies {
  2.    implementation 'com.huawei.hms:ads-lite:13.4.30.301'
  3. }

Click Sync Now and wait until synchronization is complete.

Before starting your development in the Eclipse Integrated Development Environment (IDE), download the HUAWEI Ads SDK and integrate it into your app project.

Downloading the SDK

Step1 Download and decompress adssdk-eclipse-{version}.zip. Eight Eclipse Library projects are generated, as listed in the following table.

Project

Description

Remarks

ads-base

HMS Ads SDK basic function package.

The project is mandatory.

ads-banner

Provides the banner ad capability.

This project and the ads-base and ads-native projects are required only if you want to integrate the banner ad capability.

ads-consent

Provides the capability of collecting consent from users.

This project and the ads-base project are required only if you want to integrate the capability of collecting consent from users.

ads-lang

Provides the multi-language capability.

This project and the ads-base project are required only if you want to integrate the multi-language capability.

ads-native

Provides the native ad capability.

This project and the ads-base project are required only if you want to integrate the native ad capability.

ads-reward

Provides the rewarded ad capability.

This project and the ads-base project are required only if you want to integrate the rewarded ad capability.

ads-interstitial

Provides the interstitial ad capability.

This project and the ads-base project are required only if you want to integrate the interstitial ad capability.

ads-splash

Provides the splash ad capability.

This project and the ads-base project are required only if you want to integrate the splash ad capability.

Step2 Import all SDK projects to your workspace.

a. Go to File > Import > Android > Existing Android Code Into Workspace and click Next.

b. Click Browse and navigate to the directory where your projects are located. Select the projects to be imported under Projects and click Finish. By default, all projects are selected. You can change as needed.

Caution:

The projects to be imported must be located in the same drive as the workspace, for example, D:\. If they are not in the same drive, select Copy projects into workspace to copy the project file to the workspace.

Adding Project Dependencies

Step1 Right-click your app project and choose Properties from the shortcut menu.

Step2 Choose Android from the navigation tree, click Add, select the imported projects, and click OK.

Step3 Set the ads-banner and ads-native projects as dependencies on the ads-base project, as shown in the following figure.

Modifying the AndroidManifest.xml File

If the value of minSdkVersion in the AndroidManifest.xml file of your app project is less than 19, you are advised to change the value to 19 or delete the following configuration from the AndroidManifest.xml file of each SDK project to avoid compilation errors.

  1. <uses-sdk
  2.    android:minSdkVersion="19"
  3.    android:targetSdkVersion="29" />
Caution:
Ad requesting may fail if your app project is installed on phones with Android API level 18 or earlier.

As the XML file in the Eclipse IDE does not support the dollar sign ($), replace ${applicationId} in the AndroidManifest.xml file of the ads-base project with your app package name.

  1. <application>
  2.    ...
  3.    <activity android:name="com.huawei.openalliance.ad.activity.PPSLauncherActivity"
  4.        android:theme="@android:style/Theme.Translucent.NoTitleBar"
  5.        android:exported="true">
  6.        <intent-filter>
  7.            <action android:name="android.intent.action.VIEW"/>
  8.            <category android:name="android.intent.category.DEFAULT"/>
  9.            <category android:name="android.intent.category.BROWSABLE"/>
  10.            <data android:scheme="hwpps" android:host="${applicationId}"/>
  11.        </intent-filter>
  12.    </activity>
  13.    ...
  14. </application>

Modifying the project.properties File

Configure the following information in the project.properties file of your app project:

  1. target=android-29
  2. manifestmerger.enabled=true
Note:
ADT must be above 17.

3. Configuring Network Permissions

To allow HTTP network requests on mobile phones with targetSdkVersion 28 or later, you are advised to perform the following configuration in the AndroidManifest.xml file:

  1. <application
  2.    ...
  3.    android:usesCleartextTraffic="true"
  4.    >
  5.    ...
  6. </application>

4. 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.

The configuration file in Android Studio is proguard-rules.pro, and that in Eclipse is proguard-project.txt.

  1. Open the obfuscation script file of your project.

  2. Add configurations to exclude the HUAWEI Ads SDK from obfuscation.

  1. -keep class com.huawei.openalliance.ad.** { *; }
  2. -keep class com.huawei.hms.ads.** { *; }

5. Initializing the SDK

You are advised to call the HwAds.init() API to initialize the HUAWEI Ads SDK upon app launch. You need to implement the AdSampleApplication class.

The sample code is as follows:

Add your app name to AndroidManifest.xml.

  1. <application
  2.    android:name=".AdSampleApplication"
  3.    ...
  4.    >
  5.    ...
  6. </application>

Implement the AdSampleApplication class and initialize the SDK.

  1. ...
  2. import com.huawei.hms.ads.HwAds;
  3. ...
  4. public class AdSampleApplication extends Application {
  5.    @Override
  6.    public void onCreate() {
  7.        super.onCreate();
  8.        HwAds.init(this);
  9.    }
  10.    ...
  11. }

Alternatively, you can initialize the SDK in an Activity.

The sample code is as follows:

  1. ...
  2. import com.huawei.hms.ads.HwAds;
  3. ...
  4. public class MainActivity extends Activity {
  5.    private BannerView bannerView;
  6.    @Override
  7.    protected void onCreate(Bundle savedInstanceState) {
  8.        super.onCreate(savedInstanceState);
  9.        setContentView(R.layout.activity_main);
  10.        // Initialize the HUAWEI Ads SDK.
  11.        HwAds.init(this);
  12.        ...
  13.    }
  14. }