Skip to content

Instantly share code, notes, and snippets.

@Bigfoot71
Last active December 9, 2025 23:33
Show Gist options
  • Select an option

  • Save Bigfoot71/b3a658458ece93ddcb06f4c78f85076a to your computer and use it in GitHub Desktop.

Select an option

Save Bigfoot71/b3a658458ece93ddcb06f4c78f85076a to your computer and use it in GitHub Desktop.
Example of AdMob integration in Raymob

AdMob Integration in the Raymob Repository

This gist serves as a guide on how to integrate AdMob into your Raymob project. For the actual Raymob repository, please refer to Raymob.

Please note that this gist focuses on the integration of interstitial ads, but the process is similar for other types of ads as well.

If you have any doubts, please consult the AdMob Android Quick Start Guide for further information.

To integrate AdMob into your project, follow these steps:

  1. Add the following line to the end of the app/build.gradle file, under dependencies:
    implementation 'com.google.android.gms:play-services-ads:22.4.0'
  2. Edit the existing NativeLoader.java and app/AndroidManifest.xml files in your project with the corresponding files provided in this gist.
  3. Add the example file admob.h to your project.
  4. Think about setting android.useAndroidX to true at the beginning of your gradle.properties; you'll need it for using the @NonNull annotations.

That's it! AdMob should now be successfully integrated into your Raymob repository.

NOTE: The AdMob identifiers provided in this example are meant for testing purposes and are provided by Google. Remember to replace them with your own identifiers.

Additional Information

If your application is intended to be available only on the Google Play Store, you can also use com.google.android.gms:play-services-ads-lite:22.4.0 instead of com.google.android.gms:play-services-ads:22.4.0.

This implementation will be much lighter and will help reduce the size of your application. For more information, please refer to this link: https://developers.google.com/admob/android/lite-sdk?hl=en

#ifndef ADMOB_H
#define ADMOB_H
#include "raymob.h"
void RequestInterstitialAd(void);
bool IsInterstitialAdLoaded(void);
void ShowInterstitialAd(void);
#ifdef ADMOB_IMPL
void RequestInterstitialAd(void)
{
const jobject nativeLoaderInstance = GetNativeLoaderInstance();
if (nativeLoaderInstance != NULL)
{
JNIEnv *env = AttachCurrentThread();
jclass nativeLoaderClass = (*env)->GetObjectClass(env, nativeLoaderInstance);
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "requestInterstitialAd", "()V");
(*env)->CallVoidMethod(env, nativeLoaderInstance, method);
DetachCurrentThread();
}
}
bool IsInterstitialAdLoaded(void)
{
bool adLoaded = false;
const jobject nativeLoaderInstance = GetNativeLoaderInstance();
if (nativeLoaderInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass nativeLoaderClass = (*env)->GetObjectClass(env, nativeLoaderInstance);
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "isInterstitialAdLoaded", "()Z");
adLoaded = (bool)((*env)->CallBooleanMethod(env, nativeLoaderInstance, method));
DetachCurrentThread();
}
return adLoaded;
}
void ShowInterstitialAd(void)
{
jobject nativeLoaderInstance = GetNativeLoaderInstance();
if (nativeLoaderInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass nativeLoaderClass = (*env)->GetObjectClass(env, nativeLoaderInstance);
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "showInterstitialAd", "()V");
(*env)->CallVoidMethod(env, nativeLoaderInstance, method);
DetachCurrentThread();
}
}
#endif //ADMOB_IMPL
#endif //ADMOB_H
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- Add this for apps targeting Android 13 or higher & GMA SDK version 20.3.0 or lower -->
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<application>
<activity>
<!-- [...] -->
</activity>
<!-- Add this for AdMob (change the test ID on your own when the time comes) -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
</application>
</manifest>
package com.raylib.raymob;
/* Add these imports */
import androidx.annotation.NonNull;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
/* Add this to your class */
public class NativeLoader extends NativeActivity {
private InterstitialAd mInterstitialAd;
/* [...] */
public void requestInterstitialAd() {
AdRequest adRequest = new AdRequest.Builder().build();
runOnUiThread(new Runnable() {
@Override
public void run() {
// This is the test ID for interstitial ads; replace it with your own when the time comes.
InterstitialAd.load(NativeLoader.this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
}
);
}
});
}
public boolean isInterstitialAdLoaded() {
return mInterstitialAd != null;
}
public void showInterstitialAd() {
if (mInterstitialAd != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mInterstitialAd.show(NativeLoader.this);
mInterstitialAd = null;
}
});
}
}
}
@bamfon
Copy link

bamfon commented Oct 28, 2023

Hi, thank you for the code.

I seem to get an error on
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "isInterstitialAdLoaded", "()Z");
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "requestInterstitialAd", "()V");
jmethodID method = (*env)->GetMethodID(env, nativeLoaderClass, "showInterstitialAd", "()V");
The error comes back as 'nativeLoaderClass' is not a JVM object.

@Bigfoot71
Copy link
Author

@bamfon Thank you for letting me know. Indeed, I just tried, and Android Studio is warning me about it too. However, since the compilation is successful, and the test ads are displaying correctly, I assume this must be related to an IDE issue.

The logic is exactly the same as that used in cpp/deps/raymob/features.c, and no messages appear in that file.

@SachinDas246
Copy link

Hi @Bigfoot71 Thank you for this project.
But can you suggest me an approach to implement banner ads. The problem is that, Since there is no layout, how can I use a AdView.

@SachinDas246
Copy link

I just got a solution and hope it helps some people. In this approach we use a popup window. you can find more about this groups.google.com & dynadream.com . its not a perfect solution so if you @Bigfoot71 have better solution , please do share.

My method that shows banner ad :

public void showAdPopup(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            popUp = new PopupWindow(_activity);
            popUp.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popUp.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            popUp.setClippingEnabled(false);
            layout = new LinearLayout(_activity);
            mainLayout = new LinearLayout(_activity);
            layout.setPadding(-5,-5,-5,-5);
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0,0,0,0);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(mAdview,params);
            popUp.setContentView(layout);
            _activity.setContentView(mainLayout,params);

            AdRequest adRequest = new AdRequest.Builder().build();

            mAdview.loadAd(adRequest);
            popUp.showAtLocation(mainLayout,Gravity.BOTTOM,0,0);
            popUp.update();

        }
    });

}```

@Bigfoot71
Copy link
Author

its not a perfect solution so if you @Bigfoot71 have better solution , please do share.

@SachinDas246 I have never used banners in my Android games, so I couldn't advise you on the best approach, but if it works without any issues for the application when displaying the pop-up, then your solution is likely the right one ;)

However, if you are writing your application in C++, you can see this alternative approach:
https://developers.google.com/admob/cpp/quick-start
https://developers.google.com/admob/cpp/banner

@beardBall
Copy link

I am getting errors in android studio.
First I kept getting errors for symbols not found for three admob functions. I copied them into raymob.h and helpers.c.
now when I call ShowInterstitialAd from main.cpp I get NPE.
But I am only calling it after the ad is loaded.

if(IsInterstitialAdLoaded()) {
ShowInterstitialAd();
}

Repo with code
https://github.com/beardBall/raymobTest

Thanks in advance

@beardBall
Copy link

ok got it working but not sure why. Because it should have worked the way it was.

I moved the check for null inside the run function and now it works.

public void run() {
if(mInterstitialAd != null) {
mInterstitialAd.show(NativeLoader.this);
mInterstitialAd = null;
}
}

@BSSE23043
Copy link

I am making a game using c++, the first error that I got was because of all the "(*env)->" present inside admob.h, so I changed all of those occurances them to "env->", other than that I followed the exact same instructions provided here. When i tested the interstitial ad feature, nothing was happening, so I followed the fix provided by @beardBall and now everything is working fine!

@prilcool
Copy link

anyone got a full working .c example of ad showing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment