Using AdMob in a MonoDroid Application

Using AdMob in a MonoDroid Application

While MonoDroid makes it doable to place in writing Android functions using .NET, what happens for those who want to make use of present third social gathering Java libraries in your app? In this put up I’ll discuss what your decisions are, and provide an occasion of the best way to serve adverts from AdMob in your MonoDroid utility using the same old AdMob Java library.

Talking To Java

As it exists correct now, in order to utilize Java libraries in your MonoDroid utility, you each should entry them by exact Java code or by JNI in C#. The group has talked about that someplace down the highway they’re going to launch assist for wrapping arbitrary Java libraries in .NET, nevertheless that obtained’t make it into MonoDroid v1.0. It’s worth noting, however, that when that perform is launched, doing that can most likely be a lot nicer than what I’m going to clarify proper right here, nevertheless this might hopefully get you going in the meantime.

In this occasion I’m going to level out you a pattern I’ve found works successfully to take care of points significantly clear, using every Java provide and JNI calls.

Setting Up AdMob

First we now have to rearrange AdMob in our utility. To accomplish that, embrace the JAR file in the endeavor, and set its Build Action (in the Properties window) to AndroidJavaProvide. You can use this assemble movement for JAR info in addition to Java provide info that you just want to embrace in your utility.

Next, open up the endeavor properties show display and go to the Android Manifest tab. We’ll want to set the bundle deal determine (you’ll see why in a bit), and likewise specify that we require the Internet permission. It ought to seek out your self attempting like this:

Save that, after which open the generated AndroidManifest.xml file in the endeavor’s Properties folder. We’ll need in order so as to add some particulars to the making use of side for AdMob to work:

Note that you just’ll want to change YOUR_ID_GOES_HERE collectively along with your exact author ID from AdMob. You can depart each factor else alone in this file, as a result of it was generated by the properties show display from sooner than.

Letting C# Talk To Java

As I mentioned earlier, MonoDroid permits with the intention to focus on on to Java using JNI and the Android.Runtime.JNIEnv class. JNI code will get very highly effective to study and debug really shortly, so I often try and protect that layer as simple as doable as soon as I can. To do this, we’ll define a helper class in Java which will take care of the precise AdMob interaction, and exposes straightforward methods that we are going to merely hook into from C#.

Here is a elementary AdMob helper class:

Even though in this particular case the tactic our our bodies are fairly easy, they are much additional readable than they’d be in JNI. Also, it’s easy to consider additional refined methods in a additional full implementation, and that’s the place this methodology will really repay. In this class we expose two static methods. The first registers the emulator as a examine machine, which tells AdMob that we’re working in examine mode and it should always return an advert for testing. The second approach takes in a view as a parameter, assuming it is an AdView, and tells it to refresh the advert being displayed. Also, you may even see that the bundle deal specified on the excessive is comparable as a result of the one we specified in our endeavor properties earlier.

Now we now have a easy extension degree for the C# side that doesn’t really have to know one thing about AdMob. We’ll define a static class in C# that mirrors the methods we uncovered from Java, calling them by JNI:

I obtained’t really go into particulars on how JNI works, nevertheless you may even see how points would start getting messy if we tried to do each factor by JNI. There’s a great deal of documentation on-line for those who want to dig by the JNI particulars and get crazy with it. Basically all we’re doing proper right here is getting a reference to the Java class, then looking that for the tactic by determine and approach signature, after which calling it with the arguments it desires.

The Application

With the framework out of the best way in which, now we’ll actually wire up an train that exhibits an advert. First we’ll need a format. In Main.axml, let’s define a straightforward format that displays 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 train:

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

And just like that, you’re serving AdMob adverts in your MonoDroid utility using the normal Java library! This methodology obtained’t work for each factor, nevertheless I’ve found it retains points straightforward for situations the place you have the facility to cleanly separate the library from the making use of code. If there was a requirement for the C# classes to inherit from varieties in the library, as an illustration, you wouldn’t be succesful to do it like this. Hopefully this helps give you an considered how Java/.NET interoperability could also be achieved in your MonoDroid functions. What totally different libraries are you plugging into your MonoDroid apps?

All provide code is accessible in my GitHub account.

Share This


Wordpress (0)