[Huawei Developers] Freemind R:
More articles like this, you can visit HUAWEI Developer Forum

In this article we will be working on HMS Ads kit and integrate into a flutter application.

In first part we will work on the setup part and in second part we will work on implementing the ads in application.

Step 1: Create a project in AGC console

Step 2: You have to install flutter and dart plugin to android studio. Go to File -> Settings -> Plugins

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224516.54749873278035461750212035754031:50510706012523:2800:9EE635F91B17F9836295481F0B61349331CD3CAE485D9665AB790FF56BAC363E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224516.54749873278035461750212035754031:50510706012523:2800:9EE635F91B17F9836295481F0B61349331CD3CAE485D9665AB790FF56BAC363E.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224526.82742615345720915893641553209997:50510706012523:2800:BE711397EC00CBBFB9DC3A068BDEE0D5A8CF8D4075E1D7AFC06CFFF08D949ABF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224526.82742615345720915893641553209997:50510706012523:2800:BE711397EC00CBBFB9DC3A068BDEE0D5A8CF8D4075E1D7AFC06CFFF08D949ABF.png[/IMG]

Once both are installed it will show you an option to create a new project in flutter.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224541.05957401875812640937006219213335:50510706012523:2800:21E571B96EB00013FE55A9A5B5640EAC231918CF32482438C4F12AD128DBA25E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224541.05957401875812640937006219213335:50510706012523:2800:21E571B96EB00013FE55A9A5B5640EAC231918CF32482438C4F12AD128DBA25E.png[/IMG]

To confirm run the below command in terminal “flutter doctor”

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224557.23785876870013431491399957809748:50510706012523:2800:9B3ADE32F423E0D5D75D37BDAF4F805376C5B0E42E46AD608DC8F12B0ED7033A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224557.23785876870013431491399957809748:50510706012523:2800:9B3ADE32F423E0D5D75D37BDAF4F805376C5B0E42E46AD608DC8F12B0ED7033A.png[/IMG]

If you are still facing any issue in setup please comment below and I will make sure to resolve them.

Step 3: Download the folder and add in in your workspace where your flutter project resides.

https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Library-V1/flutter-sdk-download-0000001050196675-V1

Step 4: Open pubspec.yaml file and add the below code.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224612.67780165417938754888370752535893:50510706012523:2800:EE53C9A209F87ECDCB61D6942B838D6715686F496C464D6F5BC7F1DA3E983FFB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224612.67780165417938754888370752535893:50510706012523:2800:EE53C9A209F87ECDCB61D6942B838D6715686F496C464D6F5BC7F1DA3E983FFB.png[/IMG]

huawei_ads:
path: ../huawei_ads/

..means to go 2 step backwards in folder and Huawei_ads is the folder name which you downloaded in previous step.

Once you change the pubspec.yaml file it will ask you to get the dependencies.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224623.62678866851260359349239165002950:50510706012523:2800:6E1A4567C5DA01B9FE6353FEFF27078B0CD5AFC60BFCE3766D6B82FF03EE8ECE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224623.62678866851260359349239165002950:50510706012523:2800:6E1A4567C5DA01B9FE6353FEFF27078B0CD5AFC60BFCE3766D6B82FF03EE8ECE.png[/IMG]

Click on pub get button highlighted above and you can get the latest dependencies.

Below message should be shown if the step goes successful.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224633.84742650506550137333831202997887:50510706012523:2800:7390C994AAF51C296298EE0F4419BC9545FCC871248A73160E66B7F2282CB9EC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224633.84742650506550137333831202997887:50510706012523:2800:7390C994AAF51C296298EE0F4419BC9545FCC871248A73160E66B7F2282CB9EC.png[/IMG]

To confirm the above step is successful you can also open external libraries and check flutter plugin added to it.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224642.58426086119055269901566410142463:50510706012523:2800:BADB85763641CAD2C3348449DC5CEC235ED665A4E03195D0A4A18AC30BBE6195.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627224642.58426086119055269901566410142463:50510706012523:2800:BADB85763641CAD2C3348449DC5CEC235ED665A4E03195D0A4A18AC30BBE6195.png[/IMG]

Step 5: Open the android project level gradle file and add below code.

dependencies {
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
}

add below code in buildscript and allprojects section of gradle file

maven { url 'https://developer.huawei.com/repo/' }

Step 6: Go to app level gradle file and add below dependencies and agc services

implementation 'com.huawei.hms:ads-lite:13.4.30.301'
implementation 'com.huawei.hms:ads-consent:3.4.28.305'
implementation 'com.huawei.hms:ads-identifier:3.4.28.313'
implementation 'com.huawei.hms:ads-installreferrer:3.4.28.313'

apply plugin: 'com.huawei.agconnect'

Step 7: Add the agconnect-services.json into android -> app folder

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627230038.82869400846118343437297020715825:50510706012523:2800:9648F9C848ED57F6A19DE7BEA29BB53B488F9CF38C110D06E8C145E9244EEC22.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627230038.82869400846118343437297020715825:50510706012523:2800:9648F9C848ED57F6A19DE7BEA29BB53B488F9CF38C110D06E8C145E9244EEC22.png[/IMG]

Till here your initial setup is completed.

Now you have to create a UI where you can showcase your ads.

I am using a RaisedButton in order to call the logic of ads creation. You can use your own triggering mechanism in order to show the ads.

Banner Ads:

import 'package:flutter/material.dart';
import 'package:huawei_ads/hms_ads_lib.dart';

class BannerAds extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: RaisedButton(
child: Text(
'Load Banner Ads',
),
onPressed: () {
BannerAd _bannerAd = createBannerAd();
_bannerAd
..loadAd()
..show();
},
),
);
}
}

BannerAd createBannerAd() {
return BannerAd(
adUnitId: "testw6vs28auh3",
size: BannerAdSize(width: 360, height: 57),
adParam: AdParam.build(),
);
}

Make sure you use the proper imports

Create a method which returns BannerAd

I am using test add id but you can use your own add id.

I am showcasing with 2 banner size but you can modify them according to your need.

Call the load add and show add method to show the banner ad.

Below is the table from which you can choose ad size.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225159.26043902778131584700914475671932:50510706012523:2800:CB7551667E9CEA66BEFB7844BCE0FDC64E4FF1CC178F0BBE80DA72122CEDD16A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225159.26043902778131584700914475671932:50510706012523:2800:CB7551667E9CEA66BEFB7844BCE0FDC64E4FF1CC178F0BBE80DA72122CEDD16A.png[/IMG]

Below are the screenshot for Banner Ads

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225221.75665458345846374811125351395475:50510706012523:2800:80D7257D91583840538D14C325146DD9B7762B79DC01DFDD1399C948451F3AC0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225221.75665458345846374811125351395475:50510706012523:2800:80D7257D91583840538D14C325146DD9B7762B79DC01DFDD1399C948451F3AC0.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225222.78577697622208549660008022947533:50510706012523:2800:B52D2E02A981377F09AC5493629B5F3A88D9628124C4D67468E37A581FF563EA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225222.78577697622208549660008022947533:50510706012523:2800:B52D2E02A981377F09AC5493629B5F3A88D9628124C4D67468E37A581FF563EA.png[/IMG]

Splash Ads:

import 'package:flutter/material.dart';
import 'package:huawei_ads/hms_ads_lib.dart';

class SplashAds extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: RaisedButton(
child: Text(
'Load Splash Ads',
),
onPressed: () {
SplashAd splashAd = createSplash();
splashAd
..loadAd(
adUnitId: "testq6zq98hecj",
orientation: SplashAdOrientation.portrait,
adParam: AdParam.build(),
topMargin: 1);
},
),
);
}
}

SplashAd createSplash() => SplashAd(
adType: SplashAdType.above,
ownerText: 'Owner Text',
footerText: 'Footer Text',
loadListener: (SplashAdLoadEvent event, {int errorCode}) {
print("SplashAdLoadEvent : $event");
if (event == SplashAdLoadEvent.dismissed) {
//Handle Dismiss logic here
}
});

Create a method which returns SplashAd

I am using test add id but you can use your own add id.

topMargin can increase the margin space between splash and top border.

You can also define the orientation of add as well.

You can customize Owner text and Footer Text.

Below are the screenshot for Splash Ads

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225506.14014487382925308811763701326142:50510706012523:2800:6A09F83CB287A87E7E9F344C5F7CBB9C3CE957B38169AEF06F8053545FF7EEA7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225506.14014487382925308811763701326142:50510706012523:2800:6A09F83CB287A87E7E9F344C5F7CBB9C3CE957B38169AEF06F8053545FF7EEA7.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225506.75636690974174601095298738381319:50510706012523:2800:36B05054BBE3AED9AE047AC0964BF166F6860E2E0BC0A5161F6D4E487E2D3111.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225506.75636690974174601095298738381319:50510706012523:2800:36B05054BBE3AED9AE047AC0964BF166F6860E2E0BC0A5161F6D4E487E2D3111.png[/IMG]

Interstitial Ads

import 'package:flutter/material.dart';
import 'package:huawei_ads/adslite/interstitial/interstitial_ad.dart';

class InterstitialAds extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: RaisedButton(
child: Text(
'Load Interstitial Ads',
),
onPressed: () {
InterstitialAd _interstitialAd = createInterstitialAd();
_interstitialAd
..loadAd()
..show();
},
),
);
}
}

InterstitialAd createInterstitialAd() {
return InterstitialAd(
adUnitId: "teste9ih9j0rc3",
);
}

Create a method which returns Interstitial Ad

I am using test add id but you can use your own add id.

Below are the screenshot for Interstitial Ad.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225638.37026239158195937923855714488797:50510706012523:2800:B92F134371768BEEBBA7EB7B98B841E9B0E6B2D0F06203A8592D8802F6ED5424.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/002/333/0890091000000002333.20200627225638.37026239158195937923855714488797:50510706012523:2800:B92F134371768BEEBBA7EB7B98B841E9B0E6B2D0F06203A8592D8802F6ED5424.png[/IMG]

I hope you with the above snippets you can implement above three categories of ads.