[Huawei Developers] Sherlock F:
More information like this, you can visit HUAWEI Developer Forum​

In this article, I will mention about HMS Ads Kit Integration into Solar2D (Corona) project.

Corona provides a handy template to start. Copy \Native\Project Template\App from your Corona install directory and paste it somewhere writable. On Windows, Corona would usually be installed to \Program Files (x86)\Corona Labs\Corona.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.02558604388665865216668030287132:50510928022357:2800:36F1636363F4EEA8AA0D1A1B4A3F9DC3EBABD969DAEC0907B25156761FDDB9E9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.02558604388665865216668030287132:50510928022357:2800:36F1636363F4EEA8AA0D1A1B4A3F9DC3EBABD969DAEC0907B25156761FDDB9E9.png[/IMG]

And rename App to your project name.

The contents of App folder are as follows. In App folder, we need only Android and Corona folders.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.48597986840505089257867270201445:50510928022357:2800:0DB07622356ADB7C59F07B28A8CB3742850FB9EBF6A0DF16A22C38ECAC936B20.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.48597986840505089257867270201445:50510928022357:2800:0DB07622356ADB7C59F07B28A8CB3742850FB9EBF6A0DF16A22C38ECAC936B20.png[/IMG]

In Corona folder, there is a sample project. When you open main.lua file, you can see how to call methods from android side. For example : library.init()

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.25760857424411720255737600483786:50510928022357:2800:62886E23F69B78AB8E374A52AEDD10861D05B93108B2F3F0159AAF7FC2B2E542.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.25760857424411720255737600483786:50510928022357:2800:62886E23F69B78AB8E374A52AEDD10861D05B93108B2F3F0159AAF7FC2B2E542.png[/IMG]

You can delete all of files in Corona folder and you can put our own codes. But you need to import our android library into our own main.lua folder to call method from android.

local library = require "plugin.library"

Open android folder with Android studio.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.38859252189479583088992923959414:50510928022357:2800:4D19B65B8E468E9DEC370E02E29FAA75F78CB0E2DA8B80C483AD9D6FB496EF2A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.38859252189479583088992923959414:50510928022357:2800:4D19B65B8E468E9DEC370E02E29FAA75F78CB0E2DA8B80C483AD9D6FB496EF2A.png[/IMG]

Let’s make the configuration settings;

1- Add maven repository into build.gradle.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.95295484557155431384105532850442:50510928022357:2800:AE26B09E70525D78565D85803B21DA2B7579F2902FC3BB5E868035A87ED75B23.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.95295484557155431384105532850442:50510928022357:2800:AE26B09E70525D78565D85803B21DA2B7579F2902FC3BB5E868035A87ED75B23.png[/IMG]

2- Change coronaMinSdkVersion to 19.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.48585872615675571036047160425024:50510928022357:2800:49D6D38B7FB730603642BC38CD3596F8602FD523DDBEFB5D2F1B30140090CBFA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.48585872615675571036047160425024:50510928022357:2800:49D6D38B7FB730603642BC38CD3596F8602FD523DDBEFB5D2F1B30140090CBFA.png[/IMG]

3- Add HMS Ads Kit SDK and change minSdkVersion to 19.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.56620957373021299259016002665106:50510928022357:2800:AC742FDD3AA295E146E284E91F061999811963E7E0A14BFB980A2F9F1BF525D8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.56620957373021299259016002665106:50510928022357:2800:AC742FDD3AA295E146E284E91F061999811963E7E0A14BFB980A2F9F1BF525D8.png[/IMG]

implementation ‘com.huawei.hms:ads-lite:13.4.32.300’

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.27018531583728702772117080828580:50510928022357:2800:39F9E252702C2EB2B0BB58F7CD175EBF1ADF62032720F6175C8D142CF0FD47DD.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080222.27018531583728702772117080828580:50510928022357:2800:39F9E252702C2EB2B0BB58F7CD175EBF1ADF62032720F6175C8D142CF0FD47DD.png[/IMG]

And we will use LuaLoader class and write code into LuaLoader class in plugin.

Now plugin side is ready to use HMS Ads Kit. In android project, we need to create wrapper for Ads Kit methods.

If you don`t have enough information about HMS Ads kit, i recommend to read this documentation

https://developer.huawei.com/consumer/en/hms/huawei-adskit

In this article i will show banner ad integration. So let’s start.

Before we begin, let’s define these variables in LuaLoader.class.

public AdParam adParam;
private BannerView bannerView;
private FrameLayout layout;
public String bannarAdId = "testw6vs28auh3";

For banner ad, we need to create a wrapper. (e.g BannerWrapper). And we need to add into invoke method.

@Override
public int invoke(LuaState L) {
// Register this plugin into Lua with the following functions.
NamedJavaFunction[] luaFunctions = new NamedJavaFunction[]{
new InitWrapper(),
new ShowWrapper(),
new BannerWrapper(),
// new InterstitialAdWrapper(),
};
String libName = L.toString(1);
L.register(libName, luaFunctions);
// Returning 1 indicates that the Lua require() function will return the above Lua library.
return 1;
}

And then let`s create BannerWrapper class.

@SuppressWarnings("unused")
private class BannerWrapper implements NamedJavaFunction {
@Override
public String getName() {
return "showBanner";
}
@Override
public int invoke(LuaState L) {
return showBanner(L);
}
}

Invoke method which in BannerWrapper class, returns a method (e.g showBanner()).

Let`s create showBanner() method.

@SuppressWarnings({"WeakerAccess"})
public int showBanner(LuaState L) {
CoronaActivity activity = CoronaEnvironment.getCoronaActivity();
if (activity == null) {
return 0;
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
CoronaActivity activity = CoronaEnvironment.getCoronaActivity();
if (activity == null) {
return;
}
HwAds.init(CoronaEnvironment.getApplicationContext());
adParam = new AdParam.Builder().build();
if (layout == null) {
layout = new FrameLayout(activity);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
activity.getOverlayView().addView(layout, layoutParams);
}
if (bannerView == null) {
bannerView = new BannerView(CoronaEnvironment.getCoronaActivity());
bannerView.setAdId(bannarAdId);
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_320_50);
bannerView.loadAd(adParam);
layout.addView(bannerView);
}
}
});
return 0;
}

In this method firstly, we create an activity with using CoronaEnvironemnt.getCoronaAcitivty() method.

We call HyAds.init(context) method to initialize the HUAWEI Ads SDK and we initialize Param.Builder() to construct an ad request builder.

And then if layout is null, we initialize layout.

Lastly, If bannerView is null, we should initialize it. And we load this ads with loadAd() method, add this bannerView into layout.

That’s all.

When you call the showBanner() method on main.lua file, you see the banner ad.

local library = require "plugin.library"
library.showBanner()

When we run the android project, you should see the banner ads.

Let`s define AdListener to monitor the status of the ad.

private AdListener adListener = new AdListener() {
@Override
public void onAdLoaded() {
dispatchEvent("++Ad Loaded", false, "Loaded", "", "Banner");
}
@Override
public void onAdFailed(int errorCode) {
dispatchEvent(errorCode + "", true, "Loaded", "", "Banner");
}
@Override
public void onAdOpened() {
dispatchEvent("", false, "Openeed", "", "Banner");
}
@Override
public void onAdClicked() {
dispatchEvent("", false, "Clicked", "", "Banner");
}
@Override
public void onAdLeave() {
dispatchEvent("", false, "Leaved", "", "Banner");
}
@Override
public void onAdClosed() {
dispatchEvent("", false, "Closed", "", "Banner");
}
};

We use the dispatchEvent() method to send an information to the lua side. And the dispatchEvent() method takes only 1 parameter by default. I changed it to send more detailed information. I added some more parameters as follow.

public void dispatchEvent(final String data, final Boolean isError, final String phase, final String response, final String type) {
CoronaEnvironment.getCoronaActivity().getRuntimeTaskDispatcher().send(new CoronaRuntimeTask() {
@Override
public void executeUsing(CoronaRuntime runtime) {
LuaState L = runtime.getLuaState();
CoronaLua.newEvent(L, EVENT_NAME);
L.pushBoolean(isError);
L.setField(-2, "isError");
L.pushString(phase);
L.setField(-2, "phase");
L.pushString(response);
L.setField(-2, "response");
L.pushString(type);
L.setField(-2, "type");
L.pushString(data);
L.setField(-2, "message");
L.pushString("amazonAds");
L.setField(-2, "provider");
try {
CoronaLua.dispatchEvent(L, fListener, 0);
} catch (Exception ignored) {
}
}
});
}

And then we need to set listener into bannerView in showBanner method.

@SuppressWarnings({"WeakerAccess"})
public int showBanner(LuaState L) {
CoronaActivity activity = CoronaEnvironment.getCoronaActivity();
if (activity == null) {
return 0;
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
CoronaActivity activity = CoronaEnvironment.getCoronaActivity();
if (activity == null) {
return;
}
HwAds.init(CoronaEnvironment.getApplicationContext());
adParam = new AdParam.Builder().build();
if (layout == null) {
layout = new FrameLayout(activity);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
activity.getOverlayView().addView(layout, layoutParams);
}
if (bannerView == null) {
bannerView = new BannerView(CoronaEnvironment.getCoronaActivity());
bannerView.setAdId(bannarAdId);
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_320_50);
bannerView.setAdListener(adListener);
bannerView.loadAd(adParam);
layout.addView(bannerView);
}
}
});
return 0;
}

Now, on android side everything is ready. We need to update lua part. We should import json lib because we send more than one value via dispatchEvent(). And in listener method, we use json.prettify(event) method.

local library = require "plugin.library"
local json = require("json")
local function listener( event )
print( "Received event from Library plugin " , json.prettify(event))
end
library.init( listener )
library.showBanner()

Let’s run it and see the logs.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080928.34443768441682643258267646925725:50510928022357:2800:69863E07363A2EEE6CEA0E2AE6A9E9669FA0BC55B2B0473CF9F2AF5A642F940E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/931/862/5190090000024931862.20200916080928.34443768441682643258267646925725:50510928022357:2800:69863E07363A2EEE6CEA0E2AE6A9E9669FA0BC55B2B0473CF9F2AF5A642F940E.png[/IMG]

As you can see, we integrated the HMS Ads Kit into the corona project and we can see the status of the advertisement.

You can add all the features of the HMS ads kit to your project in this way.

You can find the sample project from github. And Interstitial ad has been added in this sample project.

https://github.com/kayahanbaskeser/hms-ads-kit-integration-into-corona-project