4

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:

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) =)

Felipe
  • 10,606
  • 5
  • 40
  • 57
  • I have figured out a solution by adding the google play services component in manually. Since I have low reputation I cannot answer my own question until 8 hours have passed, so I will post it later – Felipe Jul 23 '14 at 21:16
  • I'm the author of the blog post you linked in the question. I haven't had a chance to implement AdMob using the latest SDK, sorry about that. I'll keep an eye on this question and when you post the answer I'll update the blog. Cheers. – craftworkgames Jul 23 '14 at 23:39
  • @craftworkgames Not a problem, your page steered me in the right direction. If anything, it should be painless to use your code as is, provided that the GooglePlayServices component is working, but in my case, other errors in it kept that from happening – Felipe Jul 24 '14 at 01:50

1 Answers1

9

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

Felipe
  • 10,606
  • 5
  • 40
  • 57
  • Update (10/9/14) When using this code on a new project with the latest monogame build (#1350) I ran into problems using frameLayout.AddView(). Apparently something has changed in the monogame source and it would not compile. I ended up removing the downloaded references and installing them via the nuGET manager, and it worked – Felipe Oct 10 '14 at 02:42