You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity安卓真机广告报错:showListener为空且找不到IUnityAdsShowListener类

问题描述

安卓真机运行游戏时出现以下报错:

showListener is null, you will not receive any callbacks
AndroidJavaException: java.lang.ClassNotFoundException: com.unity3d.ads.IUnityAdsShowListener
java.lang.ClassNotFoundException: com.unity3d.ads.IUnityAdsShowListener
Caused by: java.lang.ClassNotFoundException: com.unity3d.ads.IUnityAdsShowListener

Unity编辑器内运行完全正常,已按官方广告文档配置并使用示例代码,但问题未解决。代码如下:

using UnityEngine;
using UnityEngine.Advertisements;
 
public class InterstitialAd : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    private string _androidAdUnitId = "Interstitial_Android";
    private string _iOsAdUnitId = "Interstitial_iOS";
    private string _adUnitId;
 
    void Awake()
    {
        // Get the Ad Unit ID for the current platform:
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOsAdUnitId
            : _androidAdUnitId;
    }
 
    // Load content to the Ad Unit:
    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
        Debug.Log("Loading Ad: " + _adUnitId);
        Advertisement.Load(_adUnitId, this);
    }
 
    // Show the loaded content in the Ad Unit:
    public void ShowAd()
    {
        // Note that if the ad content wasn't previously loaded, this method will fail
        Debug.Log("Showing Ad: " + _adUnitId);
        Advertisement.Show(_adUnitId, this);
    }
 
    // Implement Load Listener and Show Listener interface methods: 
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        // Optionally execute code if the Ad Unit successfully loads content.
    }
 
    public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    }
 
    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
    }
 
    public void OnUnityAdsShowStart(string _adUnitId) { }
    public void OnUnityAdsShowClick(string _adUnitId) { }
    public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) { }
}
解决方案

这个报错的核心是安卓打包后缺失Unity Ads SDK中的IUnityAdsShowListener类,编辑器正常是因为Unity自带了编辑器端的SDK依赖,真机打包时没正确引入。以下是针对性的解决步骤:

  • 检查Unity Ads版本兼容性:
    IUnityAdsShowListener是Unity Ads 3.0及以上版本才引入的接口,如果你的Ads版本过低,会出现找不到类的问题。打开Package Manager,确认Unity Ads包的版本,建议升级到与当前Unity编辑器版本匹配的最新稳定版,同时删除旧版本残留的文件。

  • 确保打包时SDK被正确包含:

    1. 打开Player Settings > Publishing Settings,勾选Custom Main Gradle Template和Custom Launcher Gradle Template(Gradle打包模式下)。
    2. 打开Assets/Plugins/Android/mainTemplate.gradle,检查是否存在Unity Ads的依赖项:implementation 'com.unity3d.ads:unity-ads:X.X.X'(X.X.X为你的Ads版本号)。如果没有,手动添加后保存,再重新打包。
    3. 尝试删除项目根目录的Library文件夹,重启Unity后重新导入Unity Ads包,清理缓存后再打包。
  • 关闭或配置混淆规则:
    如果开启了ProGuard/R8代码混淆,可能会误删Unity Ads的类。在Assets/Plugins/Android/proguard-user.txt中添加以下规则:

    -keep class com.unity3d.ads.** { *; }
    -keep class com.unity3d.services.** { *; }
    

    添加后重新打包,确保SDK类不被混淆。

  • 切换打包方式测试:
    尝试将打包方式从Gradle切换为Internal(部分Unity版本支持),或者反之,有时候打包工具的配置问题会导致SDK依赖未被正确打包进APK。

  • 确认初始化流程:
    确保在调用LoadAd之前,已经通过Advertisement.Initialize完成了SDK初始化,且传入的Game ID是安卓平台的官方ID,不是编辑器的测试ID。初始化失败也会间接导致后续的接口调用异常。

内容的提问来源于stack exchange,提问作者Si Dang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 22:49:53