c# - 统一 AdMob - ClassNotFound : com. google.unity.ads.UnityAdListener

标签 c# android unity3d admob

当尝试通过 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"

我的主要广告代码是:

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();
    }
  }
  ...
}

AdsTest 类的其余部分是标准事件空值(OnAdLoaded、OnAdClosed 等),与它们在 Google's docs 中的相同。 .

从另一个类调用广告脚本:

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

我在游戏开始时在 Controller 对象中初始化了 AdMob Id。

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

我已经从 unity-package 中导入了所有 Assets ,并强制解决了播放服务的依赖关系。

我的 AndroidManifest 也是这样的:

screenshot

环境和版本控制:

Unity - 2018.3.0f2 Windows 10 x64 个人版

Android SDK - 27

AdMob 插件 - v3.15.1 (从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 服务 - 通过 Admob 包安装

在 Android 8.1、API 27 上测试

emulator specs

最佳答案

我认为某些依赖项存在冲突,虽然依赖项解析器修复了它们,但 ProGuard 把事情弄得更糟。这是我解决这个特殊问题的方法:

  • 转到build设置 > 播放器设置 > 发布设置 ( Player settings docs )。
  • 启用自定义 ProGuard 配置。
  • 配置 proguard 文件以解决您的问题。在我这边,下面的代码 fragment 完成了这项工作。但是,如果您使用其他依赖项,它们可能会与此配置冲突。在这种情况下,更多的是尝试和失败,直到出现正确的配置……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

有关更多详细信息,请查看 blog post我已经写过关于这个问题的文章。

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

关于c# - 统一 AdMob - ClassNotFound : com. google.unity.ads.UnityAdListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53977246/

相关文章:

android - 使用 DialogFragment 时出现 IllegalStateException

c# - 将相机平移与 Unity 中的触摸控件相结合

c# - 从 DbDataReader 读取数据最快的方法是什么?

c# - Clipboard.GetText 返回 null(空字符串)

c# - 单例类的几个实例

iphone - Titanium 与原生工具

android - 如何使用异步任务以编程方式检查蓝牙是否已与其他设备连接?

c# - 你如何从无限字节流中读取 UTF-8 字符 - C#

c# - 如何在unity3d中显示和保存表情符号?

c# - Firebase 和 Unity?