SDL2 on Android: android_app struct pointer

Hi everyone,

I’d like to add AdMob ads to my almost-done game. I found a great howto: http://www.dynadream.com/ddweb/index.php/Special_Blog?id=20
The problem is, I can’t find out how to get the android_app struct pointer needed for the last step:

Code:

// Somewhere you defined this…
extern struct android_app *gandroidapp;

I search3e the complete SDL sources for android_app, tried to get it somehow through JNI_GetEnv, but still nothing :frowning:

can someone enlighten me on this topic?

Hi,

I think this is in
#include <android_native_app_glue.h>

but you can also just use jni to call some method in your activity like
this:

in your c++ code

JNIEnv env = static_cast<JNIEnv>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast(SDL_AndroidGetActivity());
jclass cls = env->GetObjectClass(activity);
jclass localcls = reinterpret_cast(env->NewGlobalRef(cls))
jmethodID showAds = env->GetStaticMethodID(localcls, “showAds”, “(Z)V”);
jmethodID hideAds = env->GetStaticMethodID(localcls, “hideAds”, “()V”);

bool onTop = true;
bool show = true;
if (show) {
env->CallStaticVoidMethod(localcls, showAds, ontop);
} else {
env->CallStaticVoidMethod(localcls, hideAds);
}

env->DeleteGlobalRef(localcls);

in your java activity class:
public static void showAds(final boolean ontop) {
[…]
}

public static void hideAds() {
[…]
}

These are just stripped down lines from my code - so i’m sure it
compiles - but you should see how it works, you should of course cache
the localcls, the env and the methodidsAm 11.10.2013 14:20, schrieb glezmen:

Hi everyone,

I’d like to add AdMob ads to my almost-done game. I found a great
howto: Showing AdMob's ads using native activity with Android's NDK - DynaDream - Dynamic Laboratory
The problem is, I can’t find out how to get the android_app struct
pointer needed for the last step:

Code:

// Somewhere you defined this…
extern struct android_app *gandroidapp;

I search3e the complete SDL sources for android_app, tried to get it
somehow through JNI_GetEnv, but still nothing Sad

can someone enlighten me on this topic?

Thank you Martin,

Thank you for your help!
I modified my code according to your code snippets, and now it compiles, and I can see the grey rectangle at the bottom of the screen (i waited for something like 10 minutes), and nothing else, the main screen of my app is not displayed.

In other tutorials/example codes I see jvm->AttachCurrentThread() calls, isn’t is neccessary?

what I have so far:

C++ code, in my MainMenu.cpp:

Code:

#ifdef ANDROID
#include “jni.h”
#include “android_native_app_glue.h”

void showAds()
{
JNIEnv env = static_cast<JNIEnv>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast(SDL_AndroidGetActivity());
jclass cls = env->GetObjectClass(activity);
jclass localcls = reinterpret_cast(env->NewGlobalRef(cls));
jmethodID showAds = env->GetStaticMethodID(localcls, “showAdPopup”, “()V”);
env->CallStaticVoidMethod(localcls, showAds);
}

#endif

in my Java activity class:

Code:

// Our popup window, you will call it from your C/C++ code later
public static void showAdPopup()
{  
    //if(adsinited)
    //{
    //  return;
    //}
    if(adView!=null)  {  
        _activity.runOnUiThread(new Runnable()  {  
            @Override
            public void run()  {  
                adsinited = true;
                // Out popup window
                popUp = new PopupWindow(_activity);
                // This is the minimum size for AdMob, we need to set this in case our target device run at 320x480 resolution (Otherwise no ad will be shown, see the padding kill below)
                popUp.setWidth(320);
                popUp.setHeight(50);
                popUp.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                popUp.setClippingEnabled(false);
                layout = new LinearLayout(_activity);
                mainLayout = new LinearLayout(_activity);
                // The layout system for the PopupWindow will kill some pixels due to margins/paddings etc

(No way to remove it), so padd it to adjust
layout.setPadding(-5, -5, -5, -5);
MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView, params);
popUp.setContentView(layout);
_activity.setContentView(mainLayout, params);
AdRequest adRequest = new AdRequest();
// Enable this if your are testing AdMob, otherwise you’ll risk to be banned!
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
_activity.adView.loadAd(adRequest);
// Show our popup window
popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 0, 0);
popUp.update();
}});
}
}

Now i tried to call the showAdPopup() right before starting my Main Menu loop, and the only thing I get is a black screen with a small gray rectangle at the bottom

Ladies and gentlemen, I can announce a success :slight_smile:
I couldn’t manage AdMob work (I’m pretty sure something is wrong on the Java side), but I tried Chartboost interstitial ads, and it works perfectly, using (almost) the same code pasted above (and adding example from the Chartboost help page).
Thanks for the help, Martin!