Coder Unbridled
Making AdMob work with kivy

After many requests, I've put together a complete, step-by-step guide on integrating AdMob ads into a kivy application. It works, it's up-to-date (since the transition to Google Play services), and I hope it's fairly straightforward.

So, without further ado, here it is: how to display AdMob ads in your kivy application.


The first step is to get buildozer working according to the kivy docs
(the kivy VM iso might be useful if you're not running linux)

Once you have buildozer set up (buildozer init), build your project:
buildozer android debug
which will create the necessary files for the next steps.

Tip: buildozer requires a version number for Android. Adding
__version__ = '1.0.0'
at the top of your main.py will do the trick.

When you're done building your project with buildozer, you're ready to start adding AdMob support.

1. Open the Android SDK Manager and download "Google Repository" and "Google Play services" which are in the Extras category.

2. Copy the google-play-services_lib folder from
ANDROIDSDK/extras/google/google_play_services/libproject
to your app folder (where you keep your main.py)

Tip: ANDROIDSDK will likely be here:
/home/USERNAME/.buildozer/android/platform/android-sdk-21

2.1 Edit your buildozer.spec file and replace this line:
#android.library_references =
with the following two lines:
and then uncomment (delete the '#') this line #android.permissions = INTERNET so that AdMob can pull ads from the net.

For the rest of the post, YOUR_PROJECT is the folder where you keep buildozer.spec for this app

3. Find your AndroidManifest.tmpl.xml template (since AndroidManifest.xml will be overwritten when you run buildozer again), it should be here: YOUR_PROJECT/.buildozer/android/platform/python-for-android/dist/YOUR_APP/templates/AndroidManifest.tmpl.xml

3.1 Edit your AndroidManifest.tmpl.xml so that immediately above this line:

<activity android:name="org.renpy.android.PythonActivity"
You insert the following lines:

4. Find your PythonActivity.java file, which should be here: YOUR_PROJECT/.buildozer/android/platform/python-for-android/dist/YOUR_APP/src/org/renpy/android/PythonActivity.java

4.1 Edit your PythonActivity.java so that after

import android.content.Context;
You insert the following lines:
and then after this line:
public class PythonActivity extends Activity implements Runnable {
insert this line:

finally, replace this line:
setContentView(mView);
with the following block of code:

That's it! Rebuild the project (buildozer android debug) and you should have ads!
Don't forget to replace ca-app-pub-3940256099942544/6300978111 in PythonActivity.java with your actual AdMob ad unit ID before building in release mode.


If this post was useful to you, donate via:
Bitcoin: 1CRnh12Wa3sX9TETocv2VHuGTanNC7ryLG

Permalink / Share