Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate AdMob ads in the latest MonoGame Android (XNA)?

I have spent the last couple days researching AdMob integration for MonoGame Android and so far have not been able to successfully add a banner to the game I just made. All of the answers I have found so far are terribly outdated and none of the examples I found are working in the latest Android APIs.

I am using development build #983 of MonoGame 3.2 in Visual Studio.

I have tried:

  • using the sample found in this github repo: /CartBlanche/MonoGame-Samples/tree/master/AdMob

  • as well as the sample found in this github repo: /xamarin/monodroid-samples/tree/master/AdMob

  • Updating the SDK manager to download the google play services extras

  • Following the code sample at this page: http://www.craftworkgames.com/blog/monogame-code-snippets/using-admob-with-monogame/

As well as other methods found all over the internet. I have been very careful to select the correct build options when adding JAVA sources and JAR files to the project, but I have never found any AdView class, and the "Google Mobile Ads SDK v6.4.1" JAR that is out there is no longer supported by google, as outlined here: https://developers.google.com/mobile-ads-sdk/

If anyone has any new and up-to-date methods for integrating AdMob ads into a MonoGame Android project, I think the answers need a refresh (and I will be very grateful) =)

like image 546
Felipe Avatar asked Jan 11 '23 04:01

Felipe


1 Answers

After a long time looking for answers and testing multiple suggestions, I have finally found the solution.

Xamarin has a Google Play Services component that can be downloaded from the component store. I had originally tried this, but was running into problems with my solution file in VS2012. So this is what I did to add the component manually:

  • Login and download the component from https://components.xamarin.com/view/googleplayservices/

  • Create a folder in your Android solution called "lib" and extract the dll files from the zipped component folder into it

  • Add the dll files as references to your project. You should now have these four references added:

    1. GooglePlayServicesLib
    2. Xamarin.Android.Support.v4
    3. Xamarin.Android.Support.v7.AppCompat
    4. Xamarin.Android.Support.v7.MediaRouter
  • Navigate into your project properties, click on the Application tab (first one) and under "Java Max Heap Size", type in 1G (otherwise you get a Java Heap out of memory error while compiling)

  • Make sure your App is set to use API 14 or higher (Android 4.0). This can be done is the project properties page as well

  • In your AndroidManifest.xml file, make sure you have the following:

    <activity 
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    
  • Get your AdMob Unit ID

  • Change your Activity.cs file to something like this:

    using Android.Gms.Ads; // Add this include
    
    // Easy constants
    private const string AD_UNIT_ID = "YOUR_AD_ID";
    private const string TEST_DEVICE_ID = "YOUR_DEVICE_ID";
    private AdView adView;
    
    // Change OnCreate and make sure you're not trying to set SetContentView()
    // more than once
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Puzzle.ARunningMan.Activity = this;
        var g = new Puzzle.ARunningMan();
    
        createAds(g.Window);
    
        g.Run();
    }
    
    // Wrapped everything in a function for less confusion.
    // Thanks to Dylan Wilson at Craftwork Games for the
    // simple layout
    private void createAds(AndroidGameWindow window)
    {
        var frameLayout = new FrameLayout(this);
        var linearLayout = new LinearLayout(this);
    
        linearLayout.Orientation = Orientation.Horizontal;
        linearLayout.SetGravity(Android.Views.GravityFlags.Right | Android.Views.GravityFlags.Bottom);
    
        frameLayout.AddView(window);
    
        adView = new AdView(this);
        adView.AdUnitId = AD_UNIT_ID;
        adView.AdSize = AdSize.Banner;
    
        linearLayout.AddView(adView);
        frameLayout.AddView(linearLayout);
        SetContentView(frameLayout);
    
        try
        {
            // Initiate a generic request.
            var adRequest = new AdRequest.Builder()
                .AddTestDevice(AdRequest.DeviceIdEmulator)
                .AddTestDevice(TEST_DEVICE_ID)
                .Build();
    
            // Load the adView with the ad request.
            adView.LoadAd(adRequest);
        }
        catch (Exception ex)
        {
            // your error logging goes here
        }
    }
    
  • Compile (will take a little while at first), Run it, look in the LogCat window for a message tagged as "ads" that contains your device ID. More info here: http://webtutsdepot.com/2011/12/02/android-sdk-tutorial-get-admob-test-device-id/

  • Set the device ID string, recompile, run it

And finally, if you wait about 30-60 seconds, you will see a test add show up as a banner. Good luck to all, I hope this helps, as it is now current information

like image 131
Felipe Avatar answered Feb 06 '23 10:02

Felipe