4
votes

My app is getting the following error when trying to load or show interstitial ad via AdMob for Unity: ClassNotFoundException: com.google.unity.ads.UnityAdListener.

AndroidJavaException: java.lang.ClassNotFoundException: com.google.unity.ads.UnityAdListener
java.lang.ClassNotFoundException: com.google.unity.ads.UnityAdListener
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:453)
    at java.lang.Class.forName(Class.java:378)
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
    at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
    at android.os.MessageQueue.next(MessageQueue.java:394)
    at android.os.Looper.loop(Looper.java:142)
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.unity.ads.UnityAdListener"

My main advertisment code is:

public class AdsTest : MonoBehaviour
{
  private InterstitialAd interstitial;

  public void LoadAd()
  {
    string adUnitId = "ca-app-pub-3940256099942544/1033173712";
    interstitial = new InterstitialAd(adUnitId);

    interstitial.OnAdLoaded += HandleOnAdLoaded;
    interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
    interstitial.OnAdOpening += HandleOnAdOpened;
    interstitial.OnAdClosed += HandleOnAdClosed;
    interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

    AdRequest request = new AdRequest.Builder().Build();
    interstitial.LoadAd(request);
  }

  public void ShowAd()
  {
    if (interstitial.IsLoaded())
    {
        interstitial.Show();
    }
  }
  ...
}

The rest of the AdsTest class are the standard event voids (OnAdLoaded, OnAdClosed, ect.), the same as they're in Google's docs.

Calling the ads script from another class:

void OnTriggerEnter2D(Collider2D trigger)
{
    ReturnToStart();
    // where ads is a instance of the AdsTest class
    // ads = new AdsTest();
    ads.LoadAd();
    ads.ShowAd();
}

And I've initialized the AdMob Id at the beginning of the game in a controller object.

// executed when the app starts
string appId = "ca-app-pub-3522556458609123~3670809634";
MobileAds.Initialize(appId);

I've imported all the assets from the unity-package and force resolved the play services dependencies.

Also my AndroidManifest looks like this:

screenshot

Environment and versioning:

Unity - 2018.3.0f2 Personal for Windows 10 x64

Android SDK - 27

AdMob Plugin - v3.15.1 (downloaded from Github)

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) Client VM (build 25.191-b12, mixed mode, sharing)

Play services - as installed via the Admob package

Tested on Android 8.1, API 27

emulator specs

1
Import admob plugin to new project and implement a sample project check working fine after that open your main project and compare player settings should be same. I resolved this like this.Pawan Chaurasiya

1 Answers

1
votes

I think some of the dependencies had a conflict and while the dependency resolver fixed them, ProGuard messed the things even more. Here is how I solved this particular problem:

  • Go to Build Settings > Player Setting > Publishing Settings (Player settings docs).
  • Enable custom ProGuard config.
  • 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… ProGuard docs.
-keep class com.google.unity.** {
   *;
}
-keep public class com.google.android.gms.ads.**{
   public *;
}
-keep public class com.google.ads.**{
   public *;
}
-keepattributes *Annotation*
-dontobfuscate

For more details take a look at the blog post I've written about the issue.

NOTE: In more recent project with the newest version of AdMob and the Android SDK, the problem didn't appear. (As of May 2019)