How to add Google AdMob to android sketch?

I was able to get the code to work after adding one additional line in android studios. It appears that I was using too many methods. Google can explain it better than me:

https://developer.android.com/studio/build/multidex

Here are the steps that are needed to get this code up and running:

  1. Put the google-play-services.jar file in the code folder, which I have attached. In the folder I have attached, it is already located in the proper folder.
  2. Change the android manifest to:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
    <application android:debuggable="true" android:icon="@mipmap/ic_launcher" android:label="">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.android.gms.ads.AdActivity"/>
        <meta-data android:name="com.google.android.gms.version" android:value="8298000"/>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.SET_ORIENTATION"/>
</manifest>
  1. Import the following libraries at the top of the processing sketch, but note that processing will no longer install the sketch onto a device:
import android.os.Looper;
import android.os.Bundle;
import android.view.Window;
import android.widget.RelativeLayout;
import android.app.Activity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.MobileAds;
  1. Add the code for the banner, which can be seen in the file I have attached.
  2. Export the project from android processing mode by clicking file and than export android project.
  3. Then open android studio and click import project, find the folder that says android and has the same android studio icon and select ok.
  4. Allow it to build whatever it needs if it pops up.
  5. Open Gradle Scripts, then build.gradle (Module: app) and then adding this inside the defaultConfig:
multiDexEnabled true
  1. Now the application can be built and installed on your device.

I hope this helps anyone having the same issues. Below is the link for the sketch, manifest, and jar file.

1 Like