How to implement AdMob in Unity? A bit more detailed guide.

Radi Cho
4 min readFeb 5, 2019

--

Some tips to avoid C# problems and how to solve the ClassNotFoundException unity.ads.UnityAdListener on Android.

AdMob — one of the largest global ad networks and Unity — the world’s leading real-time game engine, when combined can make millions of dollars from your games. But it requires a bit more efforts than the official docs suggest.

1. Download the AdMob plugin, resolve the dependencies, and set up the AndroidManifest.

These are the basics described on developers.google.com/admob/unity/start, so make sure to follow them first. And do not forget — MobileAds.Initialize should take place on the app startup or at least before any other use of the MobileAds SDK.

2. The C# part. Long story short — Your ads class should extend MonoBehaviour.

My app had a Controller class and an Ads class, so I tried to initialize an instance of the Ads class in the controller. Ads ads = new Ads() . And I was stuck with “Object reference not set to an instance of an object” error. At first it was very weird for me, because in C# no matter what the two classes are you should be able to initialize similar instances, but after a bit of research and a lot of tests I found that your Ads class should extend monobehaviour.

If you just followed some of the guides for implementing Banner or Interstitial you may not face this issue because MonoBehaviour is part or the demo scripts on developers.google.com/admob but I should mention it to make sure you can solve it just in case.

You can also get NullReferenceException when initialization of ad format specific instances. To avoid similar problems:
1. Declare class-level variables, like this: InterstitialAd interstitial;
2. Then assign value in the constructor, Awake or Start methods of your class.
interstitial = new InterstitialAd (adUnitId);
For more information: answers.unity.com/answers/1198842/view.html

3. The Android /hard/ part, or how to avoid / solve ClassNotFoundException: ads.UnityAdListener

After My C# part was error free I tried to build the project and generate APK. It worked and I started the application. But no ads showing. Took a look in the logcat and found another issue:
ClassNotFoundException: com.google.unity.ads.UnityAdListener.
It turns out that this is quite popular issue without clear answer anywhere. I found dozens of Unity forums, Google Groups Threads, StackOverflow posts and GitHub issues, but all they without clear answer.
stackoverflow.com/questions/53977246/unity-admob-classnotfound-com-google-unity-ads-unityadlistener

So let me show you how I solved the error.

Warning: my “solution” is more of few workarounds which solve the errors. In the future versions of the AdMob plugin the problem might be solved officially. And also — If you find better solution please share it so the community can live an easier life :)

You should be using the Gradle build system with Compression method — default. APK or App Bundle should not matter.
Also make sure you’ve resolved the dependencies (Step 1.)

My Target API level was 27, but find out that there’s bug in the SDK, so decided to downgrade, so the solution should work with API level 26.
groups.google.com/forum/#!topic/google-admob-ads-sdk/EjlkfnF6wVk

Next up — Proguard. Now if you’re researching on this problem you may find that the ClassNotFoundException will disappear if you disable Proguard. However after doing it there’ll be tons of duplicated and conflicting class and the chance of successfully building the APK is really small. A better idea is to leave Progurad enabled (or if it isn’t — enable it) and then write custom progurard config file to make sure the ads.UnityAdListener class can pass.

To do it go to Build Settings > Player Setting > Publishing Settings
docs.unity3d.com/Manual/class-PlayerSettings.html

Then configure the proguard file to work for your problem. On my side the code snippet below does the job. But if you’re using additional dependencies they may conflict with this configuration. In such case it’s more of try and fail until the right config shows up…
www.guardsquare.com/en/products/proguard/manual/usage

Similar approach was shown in a Github issue by SlobodanNikolic — github.com/googleads/googleads-mobile-unity/issues/598#issuecomment-370849059

There are a lot more known similar ClassNotFoundException errors with different Admob related classes. You can try to approach them similarly.

The debugging

As you see I went trough long process of debugging and research. To make it easier you may want to enable more detailed logging.

Final thoughts

In a perfect world everyone should be able to implement AdMob in Unity only by following the steps in the official guide and many of you maybe never had the problems described here, actually. Maybe you’re so lucky to have the right config by default, or you know something we don’t. If so please share it to help the community :)

Hopefully the article ‘ll turn out helpful :)
To support me just leave a clap or two, or follow me
on Twitter: http://twitter.com/radi_cho/
and GitHub: https://github.com/radi-cho

--

--

Radi Cho

Google Developer Expert in Machine Learning. Forbes Bulgaira "30 under 30".