Unity - App crashes when closing Admob rewarded ads

126 views Asked by At

I have a rewarded ad in my game. The ad works perfectly in the editor but crashes on real devices

LGE LM-V510N, vesion: 12.0, abi: arm64-8a, sdk: 31

even though I take no action when the reward event and ad close event are fired. I'm still using test ad units.

Logcat

2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.SecurityException: listen
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at java.lang.Exception android.os.Parcel.createExceptionOrNull(int, java.lang.String) (Parcel.java:2426)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at java.lang.Exception android.os.Parcel.createException(int, java.lang.String) (Parcel.java:2410)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.os.Parcel.readException(int, java.lang.String) (Parcel.java:2393)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.os.Parcel.readException() (Parcel.java:2335)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void com.android.internal.telephony.ITelephonyRegistry$Stub$Proxy.listenWithEventList(int, java.lang.String, java.lang.String, com.android.internal.telephony.IPhoneStateListener, int[], boolean) (ITelephonyRegistry.java:1055)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.telephony.TelephonyRegistryManager.listenFromListener(int, java.lang.String, java.lang.String, android.telephony.PhoneStateListener, int, boolean) (TelephonyRegistryManager.java:275)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.telephony.TelephonyManager.listen(android.telephony.PhoneStateListener, int) (TelephonyManager.java:6192)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void com.unity3d.player.UnityPlayer.addPhoneCallListener() ((null):-1)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at boolean com.unity3d.player.UnityPlayer.nativeRender() ((null):-2)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at boolean com.unity3d.player.UnityPlayer.access$300(com.unity3d.player.UnityPlayer) ((null):-1)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at boolean com.unity3d.player.UnityPlayer$e$1.handleMessage(android.os.Message) ((null):-1)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at boolean android.os.Looper.loopOnce(android.os.Looper, long, int) (Looper.java:201)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void android.os.Looper.loop() (Looper.java:288)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]   at void com.unity3d.player.UnityPlayer$e.run() ((null):-1)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579] Caused by: android.os.RemoteException: Remote stack trace:
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]    at com.android.internal.telephony.TelephonyPermissions.enforceCarrierPrivilege(TelephonyPermissions.java:634)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]    at com.android.internal.telephony.TelephonyPermissions.checkReadPhoneState(TelephonyPermissions.java:149)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]    at com.android.internal.telephony.TelephonyPermissions.checkCallingOrSelfReadPhoneState(TelephonyPermissions.java:94)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]    at com.android.server.TelephonyRegistry.checkListenerPermission(TelephonyRegistry.java:3352)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]    at com.android.server.TelephonyRegistry.listen(TelephonyRegistry.java:1110)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579] 
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579] (Throwable with no stack trace)
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579] 
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]     in call to CallVoidMethodV
2023/12/09 16:27:46.991 29213 29299 Fatal .NhiemHK.Shado java_vm_ext.cc:579]     from boolean com.unity3d.player.UnityPlayer.nativeRender()


AdsManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System;

public class AdsManager : MonoBehaviourSingleton<AdsManager>
{

#if UNITY_ANDROID
    private string rewardedId = "ca-app-pub-3940256099942544/5224354917";
    private string bannerId = "ca-app-pub-3940256099942544/6300978111";
    private string interstitialId = "ca-app-pub-3940256099942544/1033173712";
    private string rewardedInterstitialId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
    private string rewardedId = "unused";
    private string bannerId = "unused";
    private string interstitialId = "unused";
    private string rewardedInterstitialId = "";
#else
    private string rewardedId = "unused";
    private string bannerId = "unused";
    private string interstitialId = "unused";
    private string rewardedInterstitialId = "";
#endif

    void Start()
    {
        MobileAds.Initialize((initStatus) => {
            Debug.Log("Mobile ads loaded");
            LoadRewardedAd();
            //LoadBannerAd();
            //LoadInterstitialAd();
        });    
    }

    private void Update()
    {
        if (rewardedCallbackTrigger)
        {
            rewardedCallback?.Invoke();
            
            rewardedCallbackTrigger = false;
            rewardedSuccesfully = false;
            rewardedCallback = null;
        }
    }
#region Rewarded Ads

    private RewardedAd rewardedAd;
    private Action rewardedCallback = null;
    private bool rewardedSuccesfully = false;
    private bool rewardedCallbackTrigger = false;
    /// <summary>
    /// Loads the rewarded ad.
    /// </summary>
    private void LoadRewardedAd()
    {
        // Clean up the old ad before loading a new one.
        if (rewardedAd != null)
        {
            rewardedAd.Destroy();
            rewardedAd = null;
        }

        Debug.Log("Loading the rewarded ad.");

        // create our request used to load the ad.
        var adRequest = new AdRequest();
        //adRequest.Keywords.Add("unity-admob-sample");

        // send the request to load the ad.
        RewardedAd.Load(rewardedId, adRequest,
            (RewardedAd ad, LoadAdError error) =>
            {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
                {
                    Debug.LogError("Rewarded ad failed to load an ad " +
                                   "with error : " + error);
                    return;
                }

                Debug.Log("Rewarded ad loaded with response : "
                          + ad.GetResponseInfo());

                rewardedAd = ad;
                
                RegisterEventHandlers(rewardedAd);
            });
    }

    public void ShowRewardedAd(Action callback = null)
    {
        const string rewardMsg =
            "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";

        if (rewardedAd != null && rewardedAd.CanShowAd())
        {
            rewardedCallback = callback;
            rewardedAd.Show((Reward reward) =>
            {
                // TODO: Reward the user.
                Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
                
                rewardedSuccesfully = true;
            });
            //return true;
        } else
        {
            Debug.Log("Cannot show rewarded ad");
            FantomLib.AndroidPlugin.ShowToast("Cannot show rewarded ad");
        }
        //return false;
    }

    private void RegisterEventHandlers(RewardedAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Rewarded ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("Rewarded ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Rewarded ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Rewarded ad full screen content closed.");
            // Reload the ad so that we can show another as soon as possible.

            //if (rewardedSuccesfully) rewardedCallbackTrigger = true;
            if (rewardedSuccesfully)
            {
                //rewardedCallback?.Invoke();
                rewardedSuccesfully = false;
            }

            LoadRewardedAd();
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Rewarded ad failed to open full screen content " +
                           "with error : " + error);
            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
    }

    #endregion
}

Video

Screen recording when testing on a real device

I am a newbie and using Admob for the first time. I hope everyone can help me find the error and fix it

0

There are 0 answers