Support Google AdMob Ads in Godot Android games
Introduction
In a previous note I wrote about creating Android games from an existing Godot project . This is a continuation on that note and will explore the possibility to add Google AdMob ads in an Android game created with Godot. Adding AdMob ads support to Godot requires rebuilding the export templates, hence, that step is described in detail here.
Google AdMob
When using AdMob for real you need to create an application and an Ad Unit in Google AdMob. It is also possible to use test ads when developing the game.
Create a Google AdMob account. Then sign in and create an application by clicking Apps –> Add App. You will be asked if you have uploaded the App to Play Store. Next you need to setup the new app, write a name and select platform:
Now your app will get an AdMob AppId which is needed later when exporting the game from Godot. Click on Create Ad Unit
Select an Ad format, in this example a Banner used.
In the next step add a name for the ad unit. You can make some additional configuration in the Advanced Setting, e.g. choose if the ad should play a movie etc. When finished click Create Ad Unit
Now you have an Ad Unit Id and an App Id. Both will be needed later.
You can later find your App and Ad Unit in AdMob user interface where you also will be able to find the App Id and Unit Id.
Build export templates with support for third party AdMob module
There are a few cases when rebuilding export templates are required. In
the Android case for example any changes that normally are done in a projects
build.gradle
like targetSdkVersion
and minSdkVersion
require you to
rebuild the export templates.
Adding a third party module in Godot may also require you to rebuild the export templates. In this case the AdMob module for supporting ads in the exported Android game is being added.
Getting Godot and the AdMob module
To build the export templates you need to clone the godot source code.
Clone godot source from github and checkout the same version tag as the Godot version you are running, in my case I am running ArchLinux and currently I have Godot version 3.1.2 installed i.e:
$ git clone https://github.com/godotengine/godot.git
$ cd godot
$ git checkout 3.1.2-stable
$ cd ..
Clone the AdMod github repo https://github.com/kloder-games/godot-admob
$ git clone https://github.com/kloder-games/godot-admob.git
then copy the admob
directory to godot/modules
directory.
$ cp -r godot-admob/admob godot/modules/
Building
Install Dependencies
Godot is using the scons build system. Java-8 is needed to build Android export templates.
$ sudo pacman -S jdk8-openjdk scons
Android SDK and Android NDK
are needed, download and unpack them. Android SDK is installed by installing
Android Studio. Then set the environment
variables ANDROID_HOME
and ANDROID_NDK_ROOT
to where they are located
$ export ANDROID_HOME=/home/bjorn/android-sdk/
$ export ANDROID_NDK_ROOT=/home/bjorn/android-ndk-r21b/
Build Godot Libraries for Android
It should now be possible to start building the Godot libraries. This will take a while. You need to specify platform, target and architecture.
First specify the architectures to build for. Google Play Store requires that
64-bit architectures are supported so build at least the architectures
armv7
and arm64v8
. If the application should be running in an emulator,
make sure to build x86
(in case the emulator is using that architecture,
which is recommeded for performace reasons).
If you want to load the application on an Android phone without Play Store it
is easier if you also build debug
target (then you don’t need to sign with
your own key, it is enough with the default debug key provided with Android
SDK), but then debug versions of the Godot libraries need to be built.
$ cd godot
$ scons platform=android target=release android_arch=armv7
$ scons platform=android target=release android_arch=arm64v8
$ scons platform=android target=release android_arch=x86
$ scons platform=android target=debug android_arch=armv7
$ scons platform=android target=debug android_arch=arm64v8
$ scons platform=android target=debug android_arch=x86
When all builds are completed there should be 2 library files for each build,
libc++_shared.so
and libgodot_android.so
, in
godot/platform/android/java/libs/<target>/<arch>
If, for some reason, the builds need to be redone clean with:
$ scons platform=android --clean
Update Android files
Building libraries will also create AndroidManifest.xml
and build.gradle
in
platform/android/java
. Those files need some changes before building
export templates.
You need to add the application id for your app in Google AdMod in
AndroidManifest.xml
. Open godot/platform/android/java/Manifest.xml
:
Replace:
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
With:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="<your-application-id>"/>
Where <your-application-id>
is replaced with the Application id from Google
AdMob see Google AdMob section.
In my case I also want to change minSdkVersion
to only support a bit newer
Android versions. Default this is set to 18 which is Android version 4.3 which
is from 2013. I update this to 22 which is Android 5.1 from 2015. Open
godot/platform/android/java/build.gradle
and set
minSdkVersion 22
Note that if you rebuild the Android Godot libraries the files you changed here will be overwritten and hence you need to update them again.
This also means that you need to build separate export templates for every
application you have, since the Application Id is specified in
AndroidManifest.xml
. This is unfortunate but I haven’t yet found any other
way.
Upgrade AdMob SDK
This is an optional step and can be skipped.
Current version of AdMob module uses AdMod SDK version 16.0.0. The latest
version is currently 19.1.0 so let’s try to use that.
Open build.gradle
and replace
compile ('com.google.android.gms:play-services-ads:16.0.0') { exclude group: 'com.android.support' }
with
compile ('com.google.android.gms:play-services-ads:19.1.0') { exclude group: 'androidx.core' }
Then you also need to add the following to packagingOptions
exclude 'META-INF/androidx.legacy_legacy-support-core-utils.version'
exclude 'META-INF/androidx.loader_loader.version'
exclude 'META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
exclude 'META-INF/androidx.print_print.version'
exclude 'META-INF/androidx.documentfile_documentfile.version'
Build Android export templates
Now it’s time to build the export template apk's
$ cd platform/android/java/
$ ./gradlew build
The result from this build step are the files godot/bin/android_release.apk
and godot/bin/android_debug.apk
.
The export template build can be cleaned with
$ ./gradlew clean
Troubleshooting build problems
In ArchLinux the build may fail with
* What went wrong:
Could not open terminal for stdout: could not get termcap entry
This is solved by setting the TERM environment like this:
$ export TERM=xterm-color
Adding Google Ads to the Android game
Time to add some code to the Godot project for interacting with AdMob SDK.
Showing Ads in Godot
Showing the ads is simple. First some initialization and then load the ad. This is an example for a banner:
var admob = null
if (Engine.has_singleton("AdMob")):
admob = Engine.get_singleton("AdMob")
admob.init(false, get_instance_id())
admob.loadBanner("ca-app-pub-3940256099942544/6300978111", false)
Then you can show and hide the banner as you wish with:
admob.showBanner()
admob.hideBanner()
Note that the code above will show test ads. The banner ad unit id is a
test banner from Google, make sure to replace it with the real ad unit id
(that was created in Google AdMob) and also change to
admob.init(true, get_instance_id())
because the first argument is whether the
ad is real or test (true means it is a real ad).
Exporting the game
For instruction on how to export the game follow the procedure, with the modifications mentioned below, described in Create Android Game Applications from Godot.
In addition to the linked description:
In Custom package
select the export template apk’s you just built
both for debug and release.
Make sure to enable Internet
and Access Network State
in privilegies since
the AdMob module need them.
For exporting to Play Store make sure to uncheck the Export with Debug
checkbox.
Example
This is an example of a banner ad in the Godot sample game Platformer. The banner is only shown on the game menu and not when playing the game, hence, the banner can cover the game navigation buttons.
There is also an interstitial ad when exiting the game.
You can install and try the sample game from Google Play Store and you can also find the source code on GitHub.