DEV Community

ntop
ntop

Posted on

Integrate Ad SDK in Golang games

Days ago, I wrote an article about porting Golang project to Android with gomobile build command. These days, Our team has developed a new game, and we are going to integrate AD SDK to make money.

It's not easy to add java code to a Golang project, but if you think in the other way, we can simply copy the go binary to an Android project, that we can take this project as a normal Android project.

So here is the main step:

  1. Create a new Android project
  2. Copy the GoNativeActivity.java file to project (you can find the file in gomobile package)
  3. Copy the .so to project
  4. Configure the AndroidManifest.xml file to use your new Acitivity

That's all, Build and Run, you can see that the Android project has been running our Golang code. But where can I get these .so files? I just unzip the .apk built from gomobile build -target=android ./ command.

The next step is to add ads. Adding ads to this special project is no different from any Android project. Here, we add the Ad code in GoNativeActivity. Note that, GoNativeActivity has no view tree, we can't just add AdView as a subview. A way to solve this problem is to use a PopupWindow to contain the adview.

In fact, it's very simple:

// load adview
popup = new PopupWindow(this);
popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
// banner
AdView adView = new AdView(this);
popup.setContentView(adView);

// show ad at bottom of screen
popup.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
Enter fullscreen mode Exit fullscreen mode

In my project, I integrated AdMob's Banner SDK. It works very well with test ad-uint-id. Here are screenshots of my game:
Shoot Stack

I have released the MAC version and Android version. You can get MAC version from itch.io: https://ntop.itch.io/shootingblock or download from GooglePlay: https://play.google.com/store/apps/details?id=io.korok.shootingblock

It's casual game, I really like the art style. (PS: Is there a way to close the ad remotely, it shows always. It seems AdMob doesn't provide a way to control frequency)

Oldest comments (0)