How to add Google AdMob to android sketch?

Hello everyone,

I have created a game I would like to publish, but I need to add ads to it. I have looked through a lot of posts about adding Google AdMob to a sketch, but all of the post are out dated and when applied, they do not work. I have attached a very small sketch that includes a button and a counter. Can anyone help me get an ad to be displayed when pressing the button?

Thank you!

int TimesPressed = 0;

void setup() {
  fullScreen(P2D);
  textSize(100);
  textAlign(CENTER);
}

void draw() {
  background(0,255,255);
  if (mouseX > width/2-100 && mouseX < width/2+100 && mouseY > height/2-50 && mouseY < height/2+50) {
    fill(0,255,125);
  } else {
    fill(0,0,255);
  }
  rect(width/2-100,height/2-50,200,100);
  fill(255);
  text(TimesPressed,width/2,height/2+200);
}

void mousePressed() {
  if (mouseX > width/2-100 && mouseX < width/2+100 && mouseY > height/2-50 && mouseY < height/2+50) {
    TimesPressed += 1;
    //Start Ad Here
  }
}

@Lucas ===
my code runs well and is not outdated: see here:
https://discourse.processing.org/t/is-there-a-way-to-load-admob-ads-in-an-animation-thread-like-draw/22566/6

the question was not exactly the same but the code is the same.

@akenaton
I had looked at your code, but could not seem to get it to run. I can’t get it to run with your code you have linked either. I have included the google play services version 28 .jar file in my code folder, as well as included the android permissions for access network state, access wifi state, internet, read phone state, set orientation. My build fails with these warnings:

Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’.

java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

And

org.gradle.tooling.BuildException: Could not execute build using Gradle distribution ‘https://services.gradle.org/distributions/gradle-5.6.2-bin.zip’.

So I compared my android manifest with your example manifest from 4 years ago, and they aren’t identical, but I tried to recreate it as much as possible. Here is my manifest:

<?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: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>
        <meta-data android:name="com.google.android.gms.version" android:value="8115000"/>
    </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>

And the google play service file is the same one you uploaded a few years ago.

Am I missing something? I have tried this with the code you have linked and have had no luck.

@Lucas ===
try changing your android value to android:value="8298000"because its possible that at this time i have used another version from the .jar

@akenaton
I have changed the value in my android manifest, but I get the same build error. I appreciate the fast response, thank you! Now what should I try?

@Lucas === the error you get seems to be related to the .jar; i have to give a look at that as soon as possible but as i have tested my code eight days ago for the other guy i am sure that it works…

@akenaton
Okay, I appreciate your help. I will keep trying to find the issue in the meantime.

After exporting and building in Android Studio, I got the error:
Cause: method ID not in [0, 0xffff]: 65536

Not sure if this helps…

I have managed to get the test ad after exporting to android studios and changing a few things. I will post a list of step as well as any working files. I would still like to get it to work in processing, but for now it will work. If you are able to figure out anything akenaton, that would be excellent to add! I am going to mess around with the code a little more and later I will upload the steps and working files. Thank you for your help as well as the code templates akenaton!

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

@akenaton
I was able to get it working with the procedure above. I would still like for it to run in processing to make testing faster. If you have any ideas, that would be great! Also, have you been able to add rewarded videos to one of your sketches? I tired to convert the code by google into one to work with processing but I couldn’t get very far. If you have, could you post your working code? Thank you again for all of your help!

For everyone who is following along, I have been able to get an Interstitial Ad to display. However, after closing the ad and returning back to the sketch, the sketch/app is no longer responsive for about a minute. If anyone can help me understand or fix this issue, that would be great. I am going to continue to work on the code. Once I get a working version, I will create a list of steps like I did for the banner ad and add working files. I plan on creating a file and steps to easily allow everyone to add the three types of admob ads to their projects.

1 Like

I have found a way to include the current AdMob ads in your sketch app. I would recommend adding your ads last, because these changes will have to be done in android studio. I have added my working processing sketch so you can go through these steps with it. In the sketch I have attached, I have commented all the code that we will have to add in android studio.

  1. Make sure you are in android processing mode, then click file at the top of the screen, then export android project.
  2. Open android studio and click import project. Find your sketch folder. In the folder you should see a folder that say android and has the same logo as android studio. Select it and click ok.
  3. It is going to ask you if you want to recreate the wrapper. Click ok.
  4. After it builds, in the top left corner, click the app folder, than manifests, and open your manifests file.
  5. Remove this line:
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
  1. At the bottom right corner there should be a box that says plugin update recommended. Click update.
  2. At the top of the screen go to the refactor tab, then select migrate to androidX. It is up to you if you want to keep a .zip or not. Then click Migrate. At the bottom of the screen click Do Refactor.
  3. At the top of the page, click the Build tab and then Clean Project.
  4. On the left hand side, click Gradle Scripts, then build.gradle (Module: app). At the bottom of the file you will see dependencies. Add this inside the dependencies brackets at the bottom:
implementation 'com.google.android.gms:play-services-ads:19.3.0'
  1. At the top of this file it will ask you to sync now, go ahead and sync.
  2. Go back to the android manifest and add this code under the activity:
<meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

Make sure that you put your Google Admob App ID for the android value!
Your android manifest should look like this:

<?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="processing.test.ads">
    <application android:icon="@mipmap/ic_launcher" android:label="Ads">
        <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>
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>
  1. On the left hand side, go to app, than java, and than Ads (or your sketch name) and open the file. Add the imports that I have commented out to the imports… area and make sure you uncomment them.
  2. Next uncomment the RelativeLayout, the InterstitialAd, and the RewardedVideoAd.
  3. Under mouseReleased(), uncomment the Play_Reward_Ad(), the Show_Interstitial_Ads(), and the Banner_On_Off().
  4. Everything that I have commented out below that, go ahead and uncomment.
    Now you can build and install on your device or install on the virtual phone. The onRewarded() call will reward your user when they finish watching the add.
  5. When you are done testing, make sure to switch out the test ad ids with your ad ids. Other than that, you are all set.

I hope this helps you out! As mentioned, the processing sketch I created is attached below and can be modified to fit your sketches:

1 Like

Great you have it working! Thanks for sharing.

2 Likes