Ads Showing in Editor, but not in Build

Hi.

I’ve been working on incorporating ads into my game (using unity 6). However, even though the seem to be working in the editor, when I build and run to my android device, the ads don’t show up. (I’m using Unity Advertizing with no mediation).

I’m also getting an error when I play the built game that says: java.lang.ClassNotFoundException: com.unity3d.services.banners.IUnityBannerListener

I’ve tried everything. I’ve turned test mode on and off in the editor, I’ve turned test ads on and off in the game’s unity cloud settings. I’ve never had this happen before. I would appreciate any help I can get.

I’m trying to create both banner and interstatial ads.

Here’s the code that loads in an ad during the game:

        int showAdChoice = Random.Range(0, 11);
        
        if(showAdChoice %2 == 0)
        {
            gameStarted = true;
        }
        else
        {
            InterstitialAd.Instance.LoadAd();
        }

Here’s the ads initialization code:

using UnityEngine;
using UnityEngine.Advertisements;

public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
    [SerializeField] string _androidGameId;
    [SerializeField] string _iOSGameId;
    [SerializeField] bool _testMode = true;
    private string _gameId;

    void Awake()
    {
        InitializeAds();
    }

    public void InitializeAds()
    {
#if UNITY_IOS
            _gameId = _iOSGameId;
#elif UNITY_ANDROID
        _gameId = _androidGameId;
#elif UNITY_EDITOR
            _gameId = _androidGameId; //Only for testing the functionality in the Editor
#endif
        if (!Advertisement.isInitialized && Advertisement.isSupported)
        {
            Advertisement.Initialize(_gameId, _testMode, this);
        }
    }


    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }
}

Here’s the code for the banner ad:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

public class BannerAd : MonoBehaviour
{


    [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;

    [SerializeField] string _androidAdUnitId = "Banner_Android";
    [SerializeField] string _iOSAdUnitId = "Banner_iOS";
    string _adUnitId = null; // This will remain null for unsupported platforms.

    void Start()
    {
        // Get the Ad Unit ID for the current platform:
#if UNITY_IOS
        _adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
        _adUnitId = _androidAdUnitId;
#endif


        // Set the banner position:
        Advertisement.Banner.SetPosition(_bannerPosition);
        

    }

    private void Update()
    {
        if (Advertisement.isInitialized && !Advertisement.Banner.isLoaded)
        {
            LoadBanner();
        }
    }

    public void LoadBanner()
    {

        BannerLoadOptions options = new BannerLoadOptions
        {
            loadCallback = OnBannerLoaded,
            errorCallback = OnBannerError
        };


        Advertisement.Banner.Load(_adUnitId, options);
    }


    void OnBannerLoaded()
    {
        Debug.Log("Banner loaded");
        ShowBannerAd();

    }

    void OnBannerError(string message)
    {
        Debug.Log($"Banner Error: {message}");

    }


    void ShowBannerAd()
    {

        BannerOptions options = new BannerOptions
        {
            clickCallback = OnBannerClicked,
            hideCallback = OnBannerHidden,
            showCallback = OnBannerShown
        };


        Advertisement.Banner.Show(_adUnitId, options);
    }


    void OnBannerClicked() { }
    void OnBannerShown() { }
    void OnBannerHidden() { }


}

And here’s the code for the interstatial ad:

using UnityEngine;
using UnityEngine.Advertisements;

public class InterstitialAd : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    public static InterstitialAd Instance;


    [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    string _adUnitId;

    void Awake()
    {
        Instance = this;


        // 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()
    {
        Debug.Log("Loading Ad: " + _adUnitId);
        Advertisement.Load(_adUnitId, this);
    }

    public void ShowAd()
    {
        Debug.Log("Showing Ad: " + _adUnitId);
        Advertisement.Show(_adUnitId, this);
    }

    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        print("showing ad");
        ShowAd();
    }

    public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
        GameManager.gameStarted = true;

    }

    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
        GameManager.gameStarted = true;

    }

    public void OnUnityAdsShowStart(string _adUnitId) { }
    public void OnUnityAdsShowClick(string _adUnitId) { }
    public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) 
    { 
        GameManager.gameStarted = true;
    }
}