Huawei Ads Kit Flutter Plugin

Table of Contents

Introduction

This plugin enables communication between Huawei Ads SDK and Flutter platform. Huawei Ads Kit Plugin for Flutter provides the following 2 services:

  • Publisher Service: HUAWEI Ads Publisher Service utilizes Huawei's vast user base and extensive data capabilities to deliver targeted, high quality ad content to users. With this service, your app will be able to generate revenues while bringing your users content which is relevant to them.
  • Identifier Service: HUAWEI Ads Kit provides the open advertising identifier (OAID) for advertisers to deliver personalized ads and attribute conversions.

Installation Guide

  • Before you get started, you must register as a HUAWEI developer and complete identity verification on the HUAWEI Developer website. For details, please refer to Register a HUAWEI ID.

  • Create an app by referring to Creating an AppGallery Connect Project and Adding an App to the Project.

  • On your Flutter project directory find and open your pubspec.yaml file and add library to dependencies to download the package from pub.dev.

      dependencies:
          huawei_ads: {library version}
    

    or

    If you downloaded the package from the HUAWEI Developer website, specify the library path on your local device.

      dependencies:
          huawei_ads:
              # Replace {library path} with actual library path of Huawei Ads Kit Plugin for Flutter.
              path: {library path}
    
  • Run

      flutter pub get
    

    or

    From Android Studio/IntelliJ: Click the Packages get action ribbon which appears on the top right corner when you open the pubspec.yaml file.

    From VS Code: Click Get Packages located in right side of the action ribbon at the top of pubspec.yaml.

  • Add a corresponding import statement in the Dart code and start the app.

API Reference

This section does not cover all of the API. To read more, please visit HUAWEI Developer website.

Publisher Service - Ads

AdListener

SignatureDescription
void AdListener(AdEvent event, {int errorCode})Ad status listener.

AdEvent

Enum Values

NameDescription
clickedAd is tapped.
closedAd is closed.
failedAd request failed.
impressionAn impression is recorded for an ad.
leaveAd leaves an app.
loadedAd is loaded successfully.
openedAd is opened.
dislikedAd is disliked.

AdParam

Properties

NameTypeDescription
genderintUser gender.
countryCodeintCode of the country/region to which an app belongs.
requestOptionsRequestOptionsChild-directed setting, setting directed to users under the age of consent, and ad content rating.

AdSize

Constructors

SignatureDescription
AdSize({int width, int height})Ad size constructor.

Properties

NameTypeDescription
widthfinal intAd height.
heightfinal intAd width.

HwAds

Methods

Return TypeMethodDescription
Future<void>init()Initializes the HUAWEI Ads SDK.
Future<void>initWithAppCode (String appCode)Initializes the HUAWEI Ads SDK.
Future<String>getSdkVersionObtains the version number of the HUAWEI Ads SDK.
Future<RequestOptions>getRequestOptionsObtains the global request configuration.
Future<void>setRequestOptions(RequestOptions options)Provides the global ad request configuration.

RequestOptions

Constructors

SignatureDescription
RequestOptions({ String adContentClassification, String tagForUnderAgeOfPromise, int tagForChildProtection, int nonPersonalizedAd, String appCountry, String appLang })Constructor for global ad request options.

Properties

NameTypeDescription
adContentClassificationStringAd content rating.
tagForUnderAgeOfPromiseintThe setting directed to users under the age of consent.
tagForChildProtectionintChild-directed setting.
nonPersonalizedAdintWhether to request non-personalized ads.
appCountryStringThe country code corresponding to the language in which an ad needs to be returned for an app.
appLangStringThe language in which an ad needs to be returned for an app.

VideoConfiguration

Constructors

SignatureDescription
VideoConfiguration({ int audioFocusType, bool customizeOperationRequested, bool startMuted })Video configuration constructor.

Properties

NameTypeDescription
audioFocusTypeintVideo playback scenario where the audio focus needs to be obtained.
customizeOperationRequestedboolWhether a custom video control is used for a video ad.
startMutedboolWhether a video is initially muted.

Publisher Service - Banner

BannerAdSize

Constructors

SignatureDescription
const BannerAdSize({ int width, int height }) : super(width: width, height: height)Banner ad size constructor. Extends ad size.

Constants

NameTypeDescription
s360x57BannerAdSizeBanner ad size: 360 x 57 dp
s360x144BannerAdSizeBanner ad size: 360 x 144 dp
s320x50BannerAdSizeBanner ad size: 320 x 50 dp
s468x60BannerAdSizeBanner ad size: 468 x 60 dp
s320x100BannerAdSizeBanner ad size: 320 x 100 dp
s728x90BannerAdSizeBanner ad size: 728 x 90 dp
s300x250BannerAdSizeBanner ad size: 300 x 250 dp
s160x600BannerAdSizeBanner ad size: 160 x 600 dp
sInvalidBannerAdSizeInvalid size. No ad can be requested using this size.
sDynamicBannerAdSizeDynamic banner ad size. The width of the parent layout and the height of the ad content are used.
sSmartBannerAdSizeDynamic banner ad size. The screen width and an adaptive height are used.

Methods

Return TypeMethodDescription
Future<int>getWidthPxObtains the ad width, in pixels. If it fails to be obtained, –1 is returned.
Future<int>getHeightPxObtains the ad height, in pixels. If it fails to be obtained, –1 is returned.
boolisAutoHeightSizeChecks whether an adaptive height is used.
boolisDynamicSizeChecks whether a dynamic size is used.
boolisFullWidthSizeChecks whether a full-screen width is used.
Future <BannerAdSize>getCurrentDirectionBannerSizeCreates a banner ad size based on the current device orientation and a custom width.
Future <BannerAdSize>getLandscapeBannerSizeCreates a banner ad size based on a custom width in landscape orientation.
Future <BannerAdSize>getPortraitBannerSizeCreates a banner ad size based on a custom width in portrait orientation.

BannerAd

Constructors

SignatureDescription
BannerAd({ String adSlotId, BannerAdSize size, int bannerRefreshTime, AdListener listener, AdParam adParam })Banner ad constructor.

Properties

NameTypeDescription
adSlotIdStringAd slot ID.
sizeBannerAdSizeSize of a banner ad.
bannerRefreshTimeintRotation interval for banner ads.
adParamAdParamAd request.
listenerAdListenerAd listener for an ad.

Methods

Return TypeMethodDescription
voidsetAdListener(AdListener listener)Sets an ad listener for an ad.
AdListenergetAdListenerObtains an ad listener.
Future<bool>loadAd()Loads an ad.
Future<bool>show({ Gravity gravity, double offset })Displays an ad.
Future<bool>isLoading()Checks whether ads are being loaded.
Future<bool>pause()Pauses any additional processing related to an ad.
Future<bool>resume()Resumes an ad after the pause() method is called last time.
Future<bool>destroy()Destroys an ad.

Gravity

Enum Values

NameDescription
bottomAd is displayed at the bottom of the screen.
centerAd is displayed at the center of the screen.
topAd is displayed on the top of the screen.

Publisher Service - Interstitial

InterstitialAd

Constructors

SignatureDescription
InterstitialAd({ String adSlotId, AdParam adParam, AdListener listener, RewardAdListener rewardAdListener })Interstitial ad constructor.

Properties

NameTypeDescription
adSlotIdStringAd slot ID.
adParamAdParamAd request.

Methods

Return TypeMethodDescription
voidsetAdListener(AdListener listener)Sets an ad listener for an ad.
AdSizegetAdListenerObtains an ad listener.
voidsetRewardAdListener(AdListener listener)Sets a rewarded ad listener for an interstitial ad.
AdSizegetRewardAdListenerObtains a rewarded ad listener from an interstitial ad.
Future<bool>loadAd()Loads an ad.
Future<bool>isLoading()Checks whether ads are being loaded.
Future<bool>isLoaded()Checks whether ad loading is complete.
Future<bool>show()Displays an ad.
Future<bool>destroy()Destroys an ad.

Publisher Service - Reward

Reward

Properties

NameTypeDescription
namefinal StringName of a reward item.
amountfinal intNumber of reward items.

RewardAd

Constructors

SignatureDescription
RewardAd({ String userId, String data, RewardAdListener listener })Rewarded ad constructor.

Properties

NameTypeDescription
userIdStringUser ID.
dataStringCustom data.
rewardVerifyConfigServer-side verification parameters.

Methods

Return TypeMethodDescription
RewardAdListenergetRewardAdListenerObtains a rewarded ad loading listener.
voidsetRewardListener(RewardAdListener listener)Sets a rewarded ad listener.
Future<bool>loadAd({ String adSlotId, AdParam adParam })Requests a rewarded ad.
Future<bool>isLoaded()Checks whether a rewarded ad is successfully loaded.
Future<bool>show()Displays a rewarded ad.
Future<bool>pause()Pauses a rewarded ad.
Future<bool>resume()Resumes a rewarded ad.
Future<bool>destroy()Destroys a rewarded ad.

RewardAdListener

SignatureDescription
void RewardAdListener(RewardAdEvent event, {Reward reward, int errorCode})Ad status listener.

RewardAdEvent

Enum Values

NameDescription
loadedRewarded ad is successfully loaded.
failedToLoadRewarded ad fails to be loaded.
openedRewarded ad is opened.
leftAppUser exits an app.
closedRewarded ad is closed
rewardedReward item when rewarded ad playback is completed.
startedRewarded ad starts playing.
completedRewarded ad playback is completed.
failedToShowRewarded ad fails to be displayed.

RewardVerifyConfig

Constructors

SignatureDescription
RewardVerifyConfig({ String userId, String data })Constructor for a RewardVerifyConfig object.

Properties

NameTypeDescription
userIdStringUser ID.
dataStringCustom data.

Publisher Service - Splash

SplashAd

Constructors

SignatureDescription
SplashAd({ SplashAdType adType, SplashAdDisplayListener displayListener, SplashAdLoadListener loadListener, String ownerText, String footerText, String logoResId, String logoBgResId, String mediaNameResId, String sloganResId, String wideSloganResId, int audioFocusType })Splash ad constructor.

Properties

NameTypeDescription
adTypeSplashAdTypeSplash ad layout type.
ownerTextStringAd owner text.
footerTextStringAd footer text.
displayListenerSplashAdDisplayListenerListener for the splash ad display or click event.
loadListenerSplashAdLoadListenerSplash ad loading listener.
logoResIdStringApp logo resource ID.
logoBgResIdStringApp background logo resource ID.
mediaNameResIdStringApp text resource ID.
sloganResIdStringDefault app launch image in portrait mode, which is displayed before a splash ad is displayed.
wideSloganResIdStringDefault app launch image in landscape mode, which is displayed before a splash ad is displayed.
audioFocusTypeintAudio focus preemption policy for a video splash ad.

Methods

Return TypeMethodDescription
Future<bool>preloadAd({ String adSlotId, int orientation, AdParam adParam })Preloads a splash ad.
Future<bool>loadAd({ String adSlotId, int orientation, AdParam adParam, double topMargin})Loads and displays a splash ad.
Future<bool>isLoading()Checks whether a splash ad is being loaded.
Future<bool>isLoaded()Checks whether a splash ad has been loaded.
Future<bool>pause()Suspends a splash ad.
Future<bool>resume()Resumes a splash ad.
Future<bool>destroy()Destroys a splash ad.

SplashAdLoadListener

SignatureDescription
void SplashAdLoadListener(SplashAdLoadEvent event, {int errorCode})Splash ad loading listener.

SplashAdDisplayListener

SignatureDescription
void SplashAdDisplayListener(SplashAdDisplayEvent event)Splash ad display listener.

SplashAdLoadEvent

Enum Values

NameDescription
loadedSplash ad is loaded successfully.
dismissedSplash ad disappears.
failedToLoadSplash ad fails to be loaded.

SplashAdDisplayEvent

Enum Values

NameDescription
showedSplash ad is displayed.
clickSplash ad is clicked.

Publisher Service - Native

NativeAd

Properties

NameTypeDescription
adSlotIdfinal StringAd slot ID.
stylesfinal NativeStylesStyle options for the views present in a native ad.
typefinal NativeAdTypeNative ad layout type.
controllerfinal NativeAdControllerController object that grants control over native ads.

NativeAdController

Methods

Return TypeMethodDescription
voidsetAdListener(AdListener listener)Sets an ad action listener.
voidsetDislikeAdListener(DislikeAdListener listener)Sets an ad closing listener.
Future<VideoOperator>getVideoOperator()Obtains the video controller of an ad.
voidgotoWhyThisAdPage()Goes to the page explaining why an ad is displayed.
Future<bool>isLoading()Checks whether ads are being loaded.
voidsetAllowCustomClick()Enables custom tap gestures.
Future<bool>isCustomClickAllowed()Checks whether custom tap gestures are enabled.
Future<String>getAdSource()Obtains an ad source.
Future<String>getDescription()Obtains the description of an ad.
Future<String>getCallToAction()Obtains the text to be displayed on a button, for example, View Details or Install.
Future<String>getTitle()Obtains the title of an ad.
Future<bool>dislikeAd(DislikeAdReason reason)Does not display the current ad. After this method is called, the current ad is closed.
Future<bool>isCustomDislikeThisAdEnabled()Checks whether custom tap gestures are enabled.
Future<bool>triggerClick(Bundle bundle)Reports a tap.
Future<bool>recordClickEvent()Reports a custom tap gesture.
Future<bool>recordImpressionEvent(Bundle bundle)Reports an ad impression.
Future<List<DislikeAdReason>>getDislikeAdReasons()Obtains the choice of not displaying the current ad.
voiddestroy()Destroys an ad object.

NativeAdConfiguration

Properties

NameTypeDescription
adSizeAdSizeRequested ad size.
choicesPositionintPosition of an ad choice icon.
mediaDirectionintDirection of an ad image.
mediaAspectintAspect ratio of an ad image.
requestCustomDislikeAdboolWhether to customize the function of not displaying the ad.
requestMultiImagesboolWhether multiple ad images are requested.
returnUrlsForImagesboolWhether the SDK is allowed to download native ad images.
videoConfigurationVideoConfigurationInformation about a native video.

NativeStyles

Properties

NameTypeDescription
showMediaboolWhether to show the media content of the native ad.
mediaImageScaleTypeStringScale type of the native ad image.

Methods

Return TypeMethodDescription
voidvoid setFlag({ bool isVisible, double fontSize, FontWeight fontWeight, Color color, Color bgColor })Sets the style for the ad flag text view.
voidvoid setTitle({ bool isVisible, double fontSize, FontWeight fontWeight, Color color, Color bgColor })Sets the style for the ad title text view.
voidvoid setSource({ bool isVisible, double fontSize, FontWeight fontWeight, Color color, Color bgColor })Sets the style for the ad owner text view.
voidvoid setDescription({ bool isVisible, double fontSize, FontWeight fontWeight, Color color, Color bgColor })Sets the style for the ad description text view.
voidvoid setCallToAction({ bool isVisible, double fontSize, FontWeight fontWeight, Color color, Color bgColor })Sets the style for the ad action button.

NativeFontWeight

Enum Values

NameDescription
normalNormal text.
boldBold text.
italicItalic text.
boldItalicBold and italic text.

DislikeAdListener

SignatureDescription
void DislikeAdListener(AdEvent event)Listens for when a native ad is closed

DislikeAdReason

Methods

Return TypeMethodDescription
StringgetDescriptionObtains the reason why a user dislikes an ad.

Methods

Return TypeMethodDescription
Future<String>getTestDeviceId()Retrieves the test device id.
Future<String>addTestDeviceId(String deviceId)Adds a new test device id.
Future<String>setDebugNeedConsent(DebugNeedConsent needConsent)Set whether user consent is needed.
Future<String>setUnderAgeOfPromise(bool ageOfPromise)Whether to process ad requests as directed to users under the age of consent.
Future<String>setConsentStatus(ConsentStatus status)Whether to display personalized ads.
Future<String>requestConsentUpdate(ConsentUpdateListener listener)Request consent information.
Future<String>updateSharedPreferences(String key, int value)Update the shared preferences related to user consent
Future<String>getSharedPreferences(String key)Retrieve a shared preference related to user consent.

ConsentUpdateListener

SignatureDescription
void ConsentUpdateListener(ConsentUpdateEvent event, {ConsentStatus consentStatus, bool isNeedConsent, List adProviders, String description})Ad status listener.

ConsentUpdateEvent

Enum Values

NameDescription
successConsent update was successful.
failedConsent update failed.

AdProvider

Constructors

SignatureDescription
AdProvider({ String id, String name, String serviceArea, String privacyPolicyUrl })Ad provider constructor.

Properties

NameTypeDescription
idStringAd provider id.
nameStringAd provider name.
serviceAreaStringService area information.
privacyPolicyUrlStringUrl for privacy policy.

Identifier Service

AdvertisingIdClient

Methods

Return TypeMethodDescription
Future<AdvertisingIdClientInfo>getAdvertisingIdInfo()Obtains the OAID and setting of Disable personalized ads.
Future<bool>verifyAdId (String adId, bool limitTracking)Verifies the OAID and setting of Disable personalized ads.

AdvertisingIdClientInfo

Methods

Return TypeMethodDescription
StringgetIdObtains the current OAID.
boolisLimitAdTrackingEnabledObtains the current setting of Disable personalized ads.

Identifier Service - InstallReferrer

InstallReferrerClient

Constructors

SignatureDescription
InstallReferrerClient({ InstallReferrerStateListener stateListener, bool test })Install referrer client constructor.

Methods

Return TypeMethodDescription
voidsetTestSets whether to run the service in test mode.
voidstartConnection(bool isTest)Starts the connection to the install referrer service.
voidendConnection()Ends the service connection and releases all occupied resources.
Future<bool>isReady()Indicates whether the service connection is ready.
Future<ReferrerDetails>getInstallReferrer()Obtains install referrer information.

InstallReferrerStateEvent

Enum Values

NameDescription
setupFinishedService connection is complete.
connectionClosedService connection is closed.
disconnectedConnection is lost and the service is disconnected.

InstallReferrerStateListener

SignatureDescription
void InstallReferrerStateListener(InstallReferrerStateEvent event, {ReferrerResponse responseCode})Install referrer connection state listener.

ReferrerResponse

Enum Values

NameDescription
disconnectedFailed to connect to the service.
okConnected to the service successfully.
unavailableThe service does not exist.
featureNotSupportedThe service is not supported.
developerErrorA call error occurred.

ReferrerDetails

Methods

Return TypeMethodDescription
StringgetInstallReferrerObtains install referrer information.
intgetReferrerClickTimestampMillisecondObtains the ad click timestamp, in milliseconds.
intgetInstallBeginTimestampMillisecondObtains the app installation timestamp, in milliseconds.

Configuration Description

No.

Licensing and Terms

Huawei Ads Kit Flutter Plugin uses the Apache 2.0 license.

Libraries

ad_param
ad_provider
ad_size
advertising_id_client
bundle
channels
constants
dislike_ad_reason
hms_ads
hms_ads_lib
hw_ads
install_referrer_client
interstitial_ad
native_ad
native_ad_configuration
native_ad_controller
native_styles
referrer_details
request_options
reward_ad
reward_verify_config
splash_ad
video_configuration
video_operator
view_types