Using AdMob in a MonoDroid Application

3 min


While MonoDroid makes it doable to put in writing Android purposes utilizing .NET, what occurs if you wish to make use of current third social gathering Java libraries in your app? In this put up I’ll speak about what your choices are, and supply an instance of the way to serve adverts from AdMob in your MonoDroid utility utilizing the usual AdMob Java library.

Talking To Java

As it exists proper now, in order to make use of Java libraries in your MonoDroid utility, you both must entry them through precise Java code or by JNI in C#. The group has mentioned that someplace down the road they’ll launch help for wrapping arbitrary Java libraries in .NET, however that received’t make it into MonoDroid v1.0. It’s value noting, nevertheless, that when that function is launched, doing that will probably be a lot nicer than what I’m going to explain right here, however this could hopefully get you going in the meantime.

In this instance I’m going to point out you a sample I’ve discovered works effectively to maintain issues considerably clear, utilizing each Java supply and JNI calls.

Setting Up AdMob

First we have to arrange AdMob in our utility. To accomplish that, embrace the JAR file in the undertaking, and set its Build Action (in the Properties window) to AndroidJavaSupply. You can use this construct motion for JAR information in addition to Java supply information that you simply wish to embrace in your utility.

Next, open up the undertaking properties display screen and go to the Android Manifest tab. We’ll wish to set the package deal identify (you’ll see why in a bit), and likewise specify that we require the Internet permission. It ought to find yourself trying like this:

Save that, after which open the generated AndroidManifest.xml file in the undertaking’s Properties folder. We’ll want so as to add some particulars to the applying aspect for AdMob to work:

Note that you simply’ll wish to change YOUR_ID_GOES_HERE together with your precise writer ID from AdMob. You can depart every thing else alone in this file, because it was generated by the properties display screen from earlier than.

Letting C# Talk To Java

As I discussed earlier, MonoDroid permits so that you can discuss on to Java utilizing JNI and the Android.Runtime.JNIEnv class. JNI code will get very powerful to learn and debug actually shortly, so I usually attempt to preserve that layer so simple as doable once I can. To try this, we’ll outline a helper class in Java that may deal with the actual AdMob interplay, and exposes easy strategies that we will simply hook into from C#.

Here is a fundamental AdMob helper class:

Even although in this specific case the tactic our bodies are quite simple, they’re much extra readable than they’d be in JNI. Also, it’s simple to think about extra sophisticated strategies in a extra full implementation, and that’s the place this method will actually repay. In this class we expose two static strategies. The first registers the emulator as a check machine, which tells AdMob that we’re working in check mode and it ought to at all times return an advert for testing. The second technique takes in a view as a parameter, assuming it’s an AdView, and tells it to refresh the advert being displayed. Also, you may see that the package deal specified on the high is similar because the one we specified in our undertaking properties earlier.

Now we now have a straightforward extension level for the C# aspect that doesn’t actually need to know something about AdMob. We’ll outline a static class in C# that mirrors the strategies we uncovered from Java, calling them through JNI:

I received’t actually go into particulars on how JNI works, however you may see how issues would begin getting messy if we tried to do every thing by JNI. There’s loads of documentation on-line if you wish to dig by the JNI particulars and get loopy with it. Basically all we’re doing right here is getting a reference to the Java class, then looking out that for the tactic by identify and technique signature, after which calling it with the arguments it wants.

The Application

With the framework out of the way in which, now we will really wire up an exercise that shows an advert. First we’ll want a format. In Main.axml, let’s outline a easy format that exhibits an advert, and likewise has a button for refreshing the advert:

Notice that the AdView is coming from the JAR file we included earlier. Now the exercise:

Once the exercise is loaded it registers the emulator as a check machine, after which tells it to refresh the advert at any time when the button will get clicked. If you run the app, it’s best to see one thing like this:

And similar to that, you’re serving AdMob adverts in your MonoDroid utility utilizing the traditional Java library! This method received’t work for every thing, however I’ve discovered it retains issues easy for instances the place you’ve the power to cleanly separate the library from the applying code. If there was a requirement for the C# lessons to inherit from varieties in the library, for instance, you wouldn’t be capable to do it like this. Hopefully this helps offer you an thought of how Java/.NET interoperability may be achieved in your MonoDroid purposes. What different libraries are you plugging into your MonoDroid apps?

All supply code is accessible in my GitHub account.

What's Your Reaction?

hate hate
confused confused
fail fail
fun fun
geeky geeky
love love
lol lol
omg omg
win win


Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Voting to make decisions or determine opinions
Formatted Text with Embeds and Visuals
The Classic Internet Listicles
Upload your own images to make custom memes
Youtube, Vimeo or Vine Embeds
Soundcloud or Mixcloud Embeds
Photo or GIF
GIF format