SonarSystems / Cocos-Helper

Cocos2d-x external framework helper.
MIT License
120 stars 48 forks source link

Android Admob Fix Crash Preload Interstitial #52

Closed LordNeznay closed 8 years ago

LordNeznay commented 8 years ago

Using a Admob preload interstitial in C++ in Adnroid, I get the following error: E/AndroidRuntime(31701): FATAL EXCEPTION: main E/AndroidRuntime(31701): Process: com.LordNeznay.TheSculpin, PID: 31701 E/AndroidRuntime(31701): java.lang.UnsatisfiedLinkError: Native method not found: sonar.systems.frameworks.AdMob.AdMobAds.FullscreenAdPreloaded:(Z)V E/AndroidRuntime(31701): at sonar.systems.frameworks.AdMob.AdMobAds.FullscreenAdPreloaded(Native Method) E/AndroidRuntime(31701): at sonar.systems.frameworks.AdMob.AdMobAds$1.onAdLoaded(AdMobAds.java:116) E/AndroidRuntime(31701): at com.google.android.gms.ads.internal.client.zzc.onAdLoaded(Unknown Source) E/AndroidRuntime(31701): at com.google.android.gms.ads.internal.client.zzn$zza.onTransact(Unknown Source) E/AndroidRuntime(31701): at android.os.Binder.transact(Binder.java:361) E/AndroidRuntime(31701): at ua.c(:com.google.android.gms.DynamiteModulesA:152) E/AndroidRuntime(31701): at pm.p(:com.google.android.gms.DynamiteModulesA:857) E/AndroidRuntime(31701): at qn.p(:com.google.android.gms.DynamiteModulesA:411) E/AndroidRuntime(31701): at pm.b(:com.google.android.gms.DynamiteModulesA:466) E/AndroidRuntime(31701): at po.b(:com.google.android.gms.DynamiteModulesA:172) E/AndroidRuntime(31701): at alk.a(:com.google.android.gms.DynamiteModulesA:128) E/AndroidRuntime(31701): at alw.a(:com.google.android.gms.DynamiteModulesA:102) E/AndroidRuntime(31701): at alk.a(:com.google.android.gms.DynamiteModulesA:92) E/AndroidRuntime(31701): at avl.e(:com.google.android.gms.DynamiteModulesA:428) E/AndroidRuntime(31701): at avl.onPageFinished(:com.google.android.gms.DynamiteModulesA:387) E/AndroidRuntime(31701): at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:449) E/AndroidRuntime(31701): at com.android.org.chromium.android_webview.AwContentsClient$AwWebContentsObserver$1.run(AwContentsClient.java:73) E/AndroidRuntime(31701): at android.os.Handler.handleCallback(Handler.java:733) E/AndroidRuntime(31701): at android.os.Handler.dispatchMessage(Handler.java:95) E/AndroidRuntime(31701): at android.os.Looper.loop(Looper.java:146) E/AndroidRuntime(31701): at android.app.ActivityThread.main(ActivityThread.java:5593) E/AndroidRuntime(31701): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(31701): at java.lang.reflect.Method.invoke(Method.java:515) E/AndroidRuntime(31701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) E/AndroidRuntime(31701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) E/AndroidRuntime(31701): at dalvik.system.NativeStart.main(Native Method)


This error occurs after some time, after the function call SonarCocosHelper::AdMob::preLoadFullscreenAd(); As I understand, it happened at the time when the application receives a response to an interstitial ad request. I found the following way to fix this error.

OscarLeif commented 8 years ago

You fix the problem ? Wow that's nice @SonarSystems Probably this will be great.

LordNeznay commented 8 years ago

Yes, the problem was fixed.

But, I do not fully understand what was supposed to do in a function public static native void FullscreenAdPreloaded(boolean result);, so I'm not entirely sure that it can be safely removed from the code. So please check the correctness of my decision and if it is correct, make the necessary changes in the repository.

mfadliishak commented 8 years ago

Hi there.

public static native void FullscreenAdPreloaded(boolean result);

Above function is a callback to C++ in JNIResults.h and JNIResults.cpp. Removing it is a bit extreme though as for you, you might not use it but for me, in my game, I use that callback for something.

The simplest way is to comment the

FullscreenAdPreloaded(true);

in onAdLoaded function.

But, the java.lang.UnsatisfiedLinkError: Native method not found: error indicates that it cannot find that generated function in JNIResults.h.

Have you add JNIResults.cpp in Android.mk?

I have no problem so far using it though. I also added on open and on close native callback too, like so:

public static native void FullscreenAdPreloaded(boolean result);
public static native void interstitialAdOponedAdmob();
public static native void interstitialAdClosedAdmob();

the listener:

interstitial.setAdListener(new AdListener()
        {
            @Override  
            public void onAdLoaded()
            {
                // Code to be executed when an ad finishes loading.
                 if(preLoadCalled)
                 {
                     FullscreenAdPreloaded(true);
                 }
                 else
                 {
                     LoadFullscreenAd();
                 }

                 preLoadCalled = false;

            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
                interstitialAdOponedAdmob();
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when when the user is about to return
                // to the app after tapping on an ad.
                interstitialAdClosedAdmob();

                preLoadCalled = false;
                PreLoadFullscreenAd();
            }
        });

and in JNIResults.h:

JNIEXPORT void JNICALL Java_sonar_systems_frameworks_AdMob_AdMobAds_interstitialAdOponedAdmob
    (JNIEnv *env, jclass thiz);

JNIEXPORT void JNICALL Java_sonar_systems_frameworks_AdMob_AdMobAds_interstitialAdClosedAdmob
    (JNIEnv *env, jclass thiz);

JNIResults.cpp:

JNIEXPORT void JNICALL Java_sonar_systems_frameworks_AdMob_AdMobAds_interstitialAdOponedAdmob
(JNIEnv *env, jclass thiz)
{
    // CPP code here
}

JNIEXPORT void JNICALL Java_sonar_systems_frameworks_AdMob_AdMobAds_interstitialAdClosedAdmob
(JNIEnv *env, jclass thiz)
{
     // CPP code here
}
LordNeznay commented 8 years ago

Thank you! It really works. Thus, my not need fixes. It remains to tell about it to everyone who encountered this problem, because the problem is very popular, but by its resolution no information.