Creating Bindings for GoogleMobileAds Framework

Creating Bindings for GoogleMobileAds Framework

Hi all

I downloaded the framework for iOS and generate Java bindings using "MOE Actions->Link third party framework to Xcode"

-> jar file:

-> I create ads: 

GADBannerView adview = GADBannerView.alloc().initWithAdSize(GoogleMobileAds.kGADAdSizeMediumRectangle());
adview.setRootViewController(controller);
adview.setAdUnitID(banner_ID);
adview.setDelegate(new GADBannerViewDelegate() {
    @Override
    public void adViewDidReceiveAd(GADBannerView gadBannerView) {
        if (banner_AcceptShow)
            F_ShowOrHideBanner(true);
        else
            F_ShowOrHideBanner(false);
    }
});
adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f - adview.bounds().size().width() / 2f, padding_top + (h - padding_top - padding_bottom) / 2f - adview.bounds().size().height() / 2f), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));
GADRequest request = GADRequest.request();
adview.loadRequest(request);
controller.view().addSubview(adview);

when I run the game I get a crash with the following:

/teamcity/workdir/moe_repo_build/moe_repo/migeran/natj/src/main/native/natj/ObjCRuntime.mm:1231 FAILURE: Binding class refers to class GADBannerView, but it can not be found.
Child process terminated with signal 6: Abort trap

Any ideas? 

 

LibGDX 1.9.3
Multi OS Engine 1.0.702 on OSX
Android Studio 2.1.2

GoogleMobileAdsSdkiOS-7.8.1 4

 

thank you!

 

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi dthai

 

I have had the same problem, but with another library. You should add in Xcode in the Build Settings->Other linker tab additional key, for example -ObjC. That's worked for me.

I got this working with google mobile ads 7.9.1.

In Xcode go to Build Phases > Link Binary with Libraries and add GoogleMobileAds.framework along with AdSupport, CoreBluetooth, CoreMedia, CoreVideo, EventKit, EventkitUI, GLKit and MessageUI (there may be a couple of others needed that I had already added for GPGS).

Now I have an issue creating a Banner Ad, my code crashes at this line:

GADBannerView bannerView = GADBannerView.alloc().init();

with

2016-08-03 18:46:48.039 ios-moe[399:73814] -[eu.friendlymonster.totalsnooker.IOSMoeLauncher window]: unrecognized selector sent to instance 0x15dc9a80
com.intel.moe.natj.objc.ObjCException: -[eu.friendlymonster.totalsnooker.IOSMoeLauncher window]: unrecognized selector sent to instance 0x15dc9a80
	at ios.uikit.c.UIKit.UIApplicationMain(Native Method)
	at eu.friendlymonster.totalsnooker.IOSMoeLauncher.main(IOSMoeLauncher.java:74)

I believe I already have the -ObjC flag set in the linker, someone with the same issue below says it's about the property "window" in the UIApplicationDelegate, so could be a LibGDX issue? How are properties supposed to be bound and used?

http://stackoverflow.com/questions/37489049/google-admob-gadbannerview-u...

I have the same problem. I take 4 hours without any luck.

I have try -ObjC flag, with error:

/teamcity/workdir/moe_repo_build/moe_repo/migeran/natj/src/main/native/natj/ObjCRuntime.mm:1231 FAILURE: Binding class refers to class GADRequest, but it can not be found.

Anyone can make admob work? Without it the app can not be publish.

Hi Denis P 

it's still not working!

thank you!

Hi everybody!

Thank everybody for help!

I got this working with Google Mobile Ads 7.9.1, LIBGDX 1.9.3, Gradle tool 2.1.2, MOE Gradle Plugin 1.1.0 final-1, Android Studio 2.1.2.

1. I downloaded the framework for iOS and generate Java bindings using "MOE Actions->Link third party framework to Xcode"

-> jar file:

2. Edit build.gradle: add complie fileTree(dir: 'lib', include: ['*.jar'])

3. MOE Actions-> Open Project in XcodeXcode

4. Project in Xcode: Build Setting -> Linking -> Other Linker Flags -> Debug (add -ObjC) -> Release (add -Objc)

5. in Xcode: Right Click on ios-moe -> Add File to "ios-moe" -> . Options: check "Copy items if needed" - Select GoogleMobileAds.framework - click "Add"

6. Build Phases -> Link Binary With Libraries -> Add: AdSupport,AudioToolbox,AVFoundation,CoreBluetooth,CoreGraphics,CoreMedia,CoreMotion,CoreTelephony,CoreVideo,EventKit,EventKitUI,Foundation,GLKit,MediaPlayer,MessageUI,OpenGLES,SafariServices,StoreKit,SystemConfiguration,UIKit

7. Edit progaurd.cfg(Applications/Intel/multi_os_engine/tools/progaurd.cfg):

add:

-keep class com.intel.googlemobileads.** { *; }
-keep enum com.intel.googlemobileads.** { *; }

8. My ADSManager class:

package com.huesoft.fruit.shoot;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.iosmoe.IOSApplication;
import com.intel.googlemobileads.GADBannerView;
import com.intel.googlemobileads.GADInterstitial;
import com.intel.googlemobileads.GADRequest;
import com.intel.googlemobileads.GADRequestError;
import com.intel.googlemobileads.c.GoogleMobileAds;
import com.intel.googlemobileads.protocol.GADBannerViewDelegate;
import com.intel.googlemobileads.protocol.GADInterstitialDelegate;

import ios.coregraphics.struct.CGPoint;
import ios.coregraphics.struct.CGRect;
import ios.coregraphics.struct.CGSize;
import ios.uikit.UIScreen;
import ios.uikit.UIViewController;
import ios.uikit.UIWindow;


/**
 * Created by ldthai on 08/09/2016.
 */
public class IOSADSManager {
    private Boolean isPortrait = true;
    private Boolean isTest = false;
    private String interstitial_ID = "";
    private String banner_ID = "";
    private BannerType bannerType = BannerType.Medium;
    private Boolean banner_AcceptShow = false;
    private double padding_left = 0;
    private double padding_right = 0;
    private double padding_top = 0;
    private double padding_bottom = 0;
    private double w = 0;
    private double h = 0;
    private Boolean interstitial_AcceptReload = true;
    private long interstitial_TimeLastShow = 0;
    private GADBannerView adview;
    private UIWindow interstitialWindow;
    private UIViewController rootViewController;
    private GADInterstitial interstitial;

    public enum BannerType {
        Top, Bottom, Medium;
    }

    public IOSADSManager(Boolean isPortrait, Boolean isTest, String interstitial_ID, Boolean interstitial_AcceptReload, String banner_ID, BannerType bannerType, Boolean banner_AcceptShow, double screenWidth, double screenHeight, double padding_left, double padding_right, double padding_bottom, double padding_top) {
        IOSADSManager.this.isPortrait = isPortrait;
        IOSADSManager.this.isTest = isTest;
        IOSADSManager.this.interstitial_ID = interstitial_ID;
        IOSADSManager.this.interstitial_AcceptReload = interstitial_AcceptReload;
        IOSADSManager.this.banner_ID = banner_ID;
        IOSADSManager.this.bannerType = bannerType;
        IOSADSManager.this.banner_AcceptShow = banner_AcceptShow;
        IOSADSManager.this.w = screenWidth;
        IOSADSManager.this.h = screenHeight;
        IOSADSManager.this.padding_left = padding_left;
        IOSADSManager.this.padding_right = padding_right;
        IOSADSManager.this.padding_bottom = padding_bottom;
        IOSADSManager.this.padding_top = padding_top;

    }

    public void initADSBanner() {
        if (adview != null)
            return;
        UIViewController controller = ((IOSApplication) Gdx.app).getUIViewController();
        adview = GADBannerView.alloc().initWithAdSize((bannerType == BannerType.Medium ? GoogleMobileAds.kGADAdSizeMediumRectangle() : (isPortrait ? GoogleMobileAds.kGADAdSizeSmartBannerPortrait() : GoogleMobileAds.kGADAdSizeSmartBannerLandscape())));
        adview.setRootViewController(controller);
        adview.setAdUnitID(banner_ID);
        adview.setDelegate(new GADBannerViewDelegate() {
            @Override
            public void adViewDidReceiveAd(GADBannerView gadBannerView) {
                if (banner_AcceptShow)
                    F_ShowOrHideBanner(true);
                else
                    F_ShowOrHideBanner(false);
            }
        });


        if (bannerType == BannerType.Medium) {
            adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f - adview.bounds().size().width() / 2f, padding_top + (h - padding_top - padding_bottom) / 2f - adview.bounds().size().height() / 2f), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));
        } else if (bannerType == BannerType.Top)
            adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f
                    - adview.bounds().size().width() / 2f, padding_top), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));
        else
            adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f
                    - adview.bounds().size().width() / 2f, h - padding_bottom - adview.bounds().size().height()), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));

        GADRequest request = GADRequest.request();
        
        adview.loadRequest(request);
        controller.view().addSubview(adview);
        adview.setHidden(true);

    }

    public void F_ShowOrHideBanner(boolean show) {
        if (adview != null) {
            if (show) {
                banner_AcceptShow = true;
                adview.setHidden(false);

                if (bannerType == BannerType.Medium) {
                    adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f - adview.bounds().size().width() / 2f, padding_top + (h - padding_top - padding_bottom) / 2f - adview.bounds().size().height() / 2f), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));
                } else if (bannerType == BannerType.Top)
                    adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f
                            - adview.bounds().size().width() / 2f, padding_top), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));
                else
                    adview.setFrame(new CGRect(new CGPoint(padding_left + (w - padding_left - padding_right) / 2f
                            - adview.bounds().size().width() / 2f, h - padding_bottom - adview.bounds().size().height()), new CGSize(adview.bounds().size().width(), adview.bounds().size().height())));

            } else {
                banner_AcceptShow = false;
                adview.setHidden(true);
            }
        }
    }

    public void initInterstitial() {

        rootViewController = ios.uikit.UIViewController.alloc();
        interstitial = GADInterstitial.alloc().initWithAdUnitID(interstitial_ID);

        interstitial.setDelegate(new GADInterstitialDelegate() {
            @Override
            public void interstitialDidFailToReceiveAdWithError(GADInterstitial gadInterstitial, GADRequestError gadRequestError) {
                if (interstitialWindow != null)
                    interstitialWindow.setHidden(true);
            }

            @Override
            public void interstitialDidDismissScreen(GADInterstitial gadInterstitial) {
                if (interstitialWindow != null)
                    interstitialWindow.setHidden(true);
                if (interstitial_AcceptReload)
                    F_ReloadInterstitial();
            }
        });


        GADRequest requestInterstitial = GADRequest.alloc().init();

        interstitial.loadRequest(requestInterstitial);
    }

    public void F_ReloadInterstitial() {
        interstitialWindow = null;
        rootViewController = null;
        interstitial = null;
        initInterstitial();
    }

    public void F_ShowInterstitial() {
        if (interstitial != null && interstitial.isReady()) {

            long time_temp = System.currentTimeMillis();
            if (interstitial_TimeLastShow == 0
                    || time_temp - interstitial_TimeLastShow > 5 * 1000 * 60) {
                interstitial_TimeLastShow = time_temp;

                if (rootViewController == null) {
                    rootViewController = UIViewController.alloc().init();
                }
                if (interstitialWindow == null) {
                    interstitialWindow = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds());
                    interstitialWindow
                            .setRootViewController(rootViewController);
                }
                if (interstitialWindow != null && interstitial != null) {
                    interstitialWindow.setHidden(false);
                    interstitialWindow.makeKeyAndVisible();
                    interstitial.presentFromRootViewController(rootViewController);
                }
            }
        }

    }
}

*  IOSMoeLaucher class:

package com.huesoft.fruit.shoot;

import com.badlogic.gdx.backends.iosmoe.IOSApplication;
import com.badlogic.gdx.backends.iosmoe.IOSApplicationConfiguration;
import com.intel.moe.natj.general.Pointer;

import ios.coregraphics.struct.CGSize;
import ios.foundation.NSAutoreleasePool;
import ios.foundation.NSDictionary;
import ios.foundation.NSURL;
import ios.uikit.UIApplication;
import ios.uikit.UIDevice;
import ios.uikit.UIScreen;
import ios.uikit.c.UIKit;
import ldthai.hsmobile.commons.Leaderboard;
import ldthai.hsmobile.commons.LoadImageListenner;
import ldthai.hsmobile.commons.MyOSType;

public class IOSMoeLauncher extends IOSApplication.Delegate {

    protected IOSMoeLauncher(Pointer peer) {
        super(peer);
    }

    MyGdxGame myGdxGame;
    IOSADSManager iosadsManager;

    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        config.useAccelerometer = false;
        Boolean isPortrait = true;
        double screenWidth, screenHeight, paddingLeft = 0, paddingRight = 0, paddingBottom = 0, paddingTop = 0;
        float cameraWidth, cameraHeight, scaleX = 1f, scaleY = 1f, scale;

        final CGSize screenSize = UIScreen.mainScreen().bounds()
                .size();
        screenWidth = screenSize.width();
        screenHeight = screenSize.height();
        if (isPortrait) {
            cameraWidth = 640f;
            cameraHeight = 960f;
            if (screenWidth > screenHeight) {
                double tam = screenWidth;
                screenWidth = screenHeight;
                screenHeight = tam;
            }
            cameraHeight = (int) (cameraWidth * screenHeight / screenWidth);
            scaleX = 1f;
            scaleY = cameraHeight / 960f;


        } else {
            cameraWidth = 960f;
            cameraHeight = 640f;
            if (screenWidth < screenHeight) {
                double tam = screenWidth;
                screenWidth = screenHeight;
                screenHeight = tam;
            }
            cameraWidth = (int) (cameraHeight * screenWidth / screenHeight);
            scaleY = 1f;
            scaleX = cameraWidth / 960f;
        }
        scale = (scaleX < scaleY ? scaleX : scaleY);
        if (paddingLeft > 0)
            paddingLeft = paddingLeft * scale
                    * ((float) screenWidth / cameraWidth);

        String bannerID = "my_bannerid";
        String interstitialID = "my_interstitialid";
        Boolean isTest = true, insterstitialAcceptReload = true,bannerAcceptShow=false;
        iosadsManager = new IOSADSManager(isPortrait, isTest, interstitialID, insterstitialAcceptReload, bannerID, IOSADSManager.BannerType.Medium, bannerAcceptShow, screenWidth, screenHeight, paddingLeft, paddingRight, paddingBottom, paddingTop);

        Leaderboard leaderboard = new Leaderboard() {

            @Override
            public void F_OpenMoreAppStore(String strPackage) {
                // TODO Auto-generated method stub
//                try {
//
//                    try {
//                        UIApplication.sharedApplication().openURL(
//                                NSURL.URLWithString("itms://itunes.apple.com/app/id"
//                                        + strPackage));
//                    } catch (Exception e) {
//                    }
//                } catch (Exception e) {
//                }
                try {

                    try {
                        UIApplication.sharedApplication().openURL(
                                NSURL.URLWithString("itms://itunes.apple.com/developer/id728308952"));
                    } catch (Exception e) {
                        UIApplication.sharedApplication().openURL(
                                NSURL.URLWithString("https://itunes.apple.com/developer/id728308952"));
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public String F_GetDeviceID() {
                // TODO Auto-generated method stub
                return getUUID();
            }

            @Override
            public void F_SetAlignADS(Boolean isBottom) {
                // TODO Auto-generated method stub

            }

            @Override
            public void F_ShowADS(ADSType ads) {
                // TODO Auto-generated method stub
                if (ads == ADSType.Banner) {
                    iosadsManager.F_ShowOrHideBanner(true);
                }
                if (ads == ADSType.Interstitial) {
                    iosadsManager.F_ShowInterstitial();
                }
            }

            @Override
            public void F_HideADS(ADSType ads) {
                // TODO Auto-generated method stub
                if (ads != null && ads == ADSType.Banner) {
                    iosadsManager.F_ShowOrHideBanner(false);
                }
            }

            @Override
            public void F_MenuBack() {
                // TODO Auto-generated method stub

            }

            @Override
            public String F_GetLanguageCode() {
                return "en";
            }

            @Override
            public void F_SubmitScore(int score) {
                //iosGameServices.submitScore(score);
            }

            @Override
            public void F_ShowLeaderBoard() {
                //iosGameServices.showScores();
            }

            @Override
            public void F_UnlockAchievement(int score) {

            }

            @Override
            public void F_SignInGooglePlayServices() {
                //iosGameServices.signIn();
            }

            @Override
            public void getImageFromUrlForGWT(String url, LoadImageListenner loadImageListenner) {

            }
        };

        myGdxGame = new MyGdxGame(MyOSType.IOS, cameraWidth,
                cameraHeight, scaleX, scaleY, leaderboard);
        return new IOSApplication(myGdxGame, config);
    }

    @Override
    public boolean applicationDidFinishLaunchingWithOptions(UIApplication application, NSDictionary<?, ?> launchOptions) {
        Boolean b = super.applicationDidFinishLaunchingWithOptions(application, launchOptions);
        iosadsManager.initADSBanner();
        iosadsManager.initInterstitial();
        return b;
    }

    public static void main(String[] argv) {
        NSAutoreleasePool pool = NSAutoreleasePool.alloc();
        UIKit.UIApplicationMain(0, null, null, IOSMoeLauncher.class.getName());
        pool.dealloc();
    }

    public static String getUUID() {
        try {
            UIDevice uidevice = UIDevice.currentDevice();
            if (uidevice != null && uidevice.identifierForVendor() != null
                    && uidevice.identifierForVendor().toString() != null)
                return uidevice.identifierForVendor().toString();
            else
                return "";
        } catch (Exception e) {
            return "";
        }

    }
}

 

9. Finish:

 

Good Luck ^_^

Thanks ldthai

I can make it work now.

Other note is add proguard config

-keep class com.intel.googlemobileads.** { *; }
-keep enum com.intel.googlemobileads.** { *; }

 

Quote:

Pham V. wrote:

Thanks ldthai

I can make it work now.

Other note is add proguard config

-keep class com.intel.googlemobileads.** { *; }
-keep enum com.intel.googlemobileads.** { *; }

 

 

Nice ^_^

i have edited my answer! 

Thank you ^_^

Leave a Comment

Please sign in to add a comment. Not a member? Join today