Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.KeyguardManager.isDeviceLocked()' on a null object reference #3668

Open
dipupo6 opened this issue Nov 3, 2024 · 12 comments
Assignees
Labels
P2 Issue.

Comments

@dipupo6
Copy link

dipupo6 commented Nov 3, 2024

[REQUIRED] Step 1: Describe your environment

  • Unity version: 2020.3.30f1
  • Google Mobile Ads Unity plugin version: 8.6.0
  • Plugin installation method: .unitypackage import
  • Platform: Android
  • Platform OS version: All android versions
  • Any specific devices issue occurs on: Google Pixel 7a, Samsung Galaxy S21 5G
  • Mediation ad networks used, and their versions: None

[REQUIRED] Step 2: Describe the problem

Started seeing high crash rates (2-3%) at the beginning of last month on google play console which has been slowly going up and as at October 31st was now at 50%, so i had to remove admob ads and started working on the issue locally. I didn't change anything on my end. I started testing my game locally with test ads and i discovered that once a bannner ad gets requested with the LoadAd() function, it shows a black banner with "test ads" tag and then the game crashes. Same thing happens for rewarded ads. THE CRASH SEEMS TO ONLY OCCUR ON INSTANT PLAY VERSIONS OF MY GAME.

Steps to reproduce:

Import the Google Mobile ads package and the Google Play Instant plugin into a project, build an instant play version and request an ad.
Stack trace
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.KeyguardManager.isDeviceLocked()' on a null object reference
at com.google.android.gms.ads.omid.library.walking.e.run(:com.google.android.gms.policy_ads_fdr_dynamite@[email protected]:615)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8705)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)

using UnityEngine;
using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using PimDeWitte.UnityMainThreadDispatcher;

public class GoogleMobileAdsBanner : MonoBehaviour
{
public int adTime = 5;
private BannerView _bannerView;
private bool _isInitialized = false;

#if UNITY_ANDROID
    private string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
    private string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
    private string _adUnitId = "unused";
#endif

public void Start()
{   
  UnityMainThreadDispatcher.Instance().Enqueue(() => {
    // Initialize Mobile Ads first
    MobileAds.Initialize(initStatus => {
        _isInitialized = true;
        Debug.Log("Mobile Ads initialized successfully");
        // Start coroutine for delayed ad loading only after initialization
        StartCoroutine(WaitForAd());
    });
  });
}

IEnumerator WaitForAd()
{
    yield return new WaitForSeconds(adTime);
    
    if (!_isInitialized)
    {
        Debug.LogError("Attempted to load ad before Mobile Ads SDK was initialized");
        yield break;
    }

    try
    {
        // Ensure we're on the main thread using UnityMainThreadDispatcher
        UnityMainThreadDispatcher.Instance().Enqueue(() => {
            try
            {
                LoadAd();
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error loading ad: {ex.Message}\n{ex.StackTrace}");
            }
        });
    }
    catch (Exception ex)
    {
        Debug.LogError($"Error enqueueing ad load: {ex.Message}\n{ex.StackTrace}");
    }
}

public void CreateBannerView()
{
    if (!_isInitialized)
    {
        Debug.LogError("Attempted to create banner before Mobile Ads SDK was initialized");
        return;
    }

    Debug.Log("Creating banner view.");

    try
    {
        // If we already have a banner, destroy the old one.
        if(_bannerView != null)
        {
            DestroyAd();
        }

        // Create a 320x50 banner at top of the screen.
        _bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);

        // Listen to events the banner may raise.
        /*ListenToAdEvents();*/

        Debug.Log("Banner view created.");
    }
    catch (Exception ex)
    {
        Debug.LogError($"Error creating banner view: {ex.Message}\n{ex.StackTrace}");
    }
}

public void LoadAd()
{
    if (!_isInitialized)
    {
        Debug.LogError("Attempted to load ad before Mobile Ads SDK was initialized");
        return;
    }

    try
    {
        // Create an instance of a banner view first.
        if(_bannerView == null)
        {
            CreateBannerView();
        }

        // Create our request used to load the ad.
        var adRequest = new AdRequest();

        // Send the request to load the ad.
        Debug.Log("Loading banner ad.");
        _bannerView.LoadAd(adRequest);
    }
    catch (Exception ex)
    {
        Debug.LogError($"Error loading ad: {ex.Message}\n{ex.StackTrace}");
    }
}

public void ShowAd()
{
    if (_bannerView != null)
    {
        _bannerView.Show();
    }
    else
    {
        Debug.LogWarning("Banner view is not initialized");
    }
}

public void HideAd()
{
    if (_bannerView != null)
    {
        _bannerView.Hide();
    }
}

public void DestroyAd()
{
    if (_bannerView != null)
    {
        Debug.Log("Destroying banner view.");
        _bannerView.Destroy();
        _bannerView = null;
    }
}
// TODO(you): code here to reproduce the problem
@dipupo6
Copy link
Author

dipupo6 commented Nov 3, 2024

@NVentimiglia please, help me out. i'm losing ad revenue because of this.

@dipupo6 dipupo6 changed the title Crashes caused by admob plugin on android versions java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.KeyguardManager.isDeviceLocked()' Nov 3, 2024
@dipupo6 dipupo6 changed the title java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.KeyguardManager.isDeviceLocked()' java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.KeyguardManager.isDeviceLocked()' on a null object reference Nov 3, 2024
@solclovser
Copy link

I'm having the same issue. It started this week and drove my revenue to zero.

@dipupo6
Copy link
Author

dipupo6 commented Nov 3, 2024

@solclovser. Good to know it's not from my end. Let's wait for a comment from one of the officials. What version of Google Play Instant plugin are you using?. I'm going to upgrade to the latest version to see if the crash will stop.

@solclovser
Copy link

@solclovser. Good to know it's not from my end. Let's wait for a comment from one of the officials. What version of Google Play Instant plugin are you using?. I'm going to upgrade to the latest version to see if the crash will stop.

I upgraded everything to the latest -even trying Unity 6 but the problem persists. I will let you know if I find something.

@NVentimiglia
Copy link
Member

Can you please file a bug ticket with Admob Support, I will most likely need to get the Android team involved in this. I will see if I can replicate this on my end.

@NVentimiglia NVentimiglia self-assigned this Nov 4, 2024
@NVentimiglia NVentimiglia added the P1 Priority issue. label Nov 4, 2024
@dipupo6
Copy link
Author

dipupo6 commented Nov 4, 2024

Can you please file a bug ticket with Admob Support, I will most likely need to get the Android team involved in this. I will see if I can replicate this on my end.

I've done that. Hopefully, this gets resolved as soon as possible.

@NVentimiglia NVentimiglia added P2 Issue. and removed P1 Priority issue. labels Nov 4, 2024
@solclovser
Copy link

I also have an ongoing ticket with the Ads SDK Team. I just tried the sample project.
Unity 6000.0.23f1
Target API 34
GoogleMobileAds-v9.3.0
com.google.play.instant-1.8.0

Everything is fine in the installed build.
When switched to instant the game crashes when I click Load Ad on any sample unit.

@dipupo6
Copy link
Author

dipupo6 commented Nov 6, 2024

@NVentimiglia please, is there any update on the situation?. We're really losing money.

@dipupo6
Copy link
Author

dipupo6 commented Nov 7, 2024

@NVentimiglia please, help us with a workaround. There has been no update from the Admob Support. This is really sad and sudden.

@NVentimiglia
Copy link
Member

thanks for the reminder.

I did escalated this issue internally. I am not too familiar with instant apps, can you provide some details on how those apps are different. A sample apk we could test against would also help here.

@dipupo6
Copy link
Author

dipupo6 commented Nov 7, 2024

thanks for the reminder.

I did escalated this issue internally. I am not too familiar with instant apps, can you provide some details on how those apps are different. A sample apk we could test against would also help here.

They are different in the sense that players don't have to install them in order to open them.

@dipupo6
Copy link
Author

dipupo6 commented Nov 16, 2024

@NVentimiglia On further testing, I discovered that I'm able to play instant games that have admob ads on my Pixel 5a running Android 14, but they are crashing on my Pixel 7a running Android 15. If that's going to be of any help to you guys in solving the issue. I'm not sure if this has always been the case or if this means they're already working on the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Issue.
Projects
None yet
Development

No branches or pull requests

3 participants