googleads / googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK
https://developers.google.com/admob/unity
Apache License 2.0
1.36k stars 1.08k forks source link

GamePlayActivity no field mUnityPlayer cause a AndroidJavaException crash #2751

Closed guanghui1987 closed 1 year ago

guanghui1987 commented 1 year ago

Step 0: Are you in the right place?

[REQUIRED] Step 1: Describe your environment

[REQUIRED] Step 2: Describe the problem

  1. java GamePlayActivity really no field "mUnityPlayer" in my case, mUnityPlayer is in other SDK.I use unity as a library;
  2. Unity C# code, when call MobileAds.Initialize make the problem occur;

Steps to reproduce:

  1. Yes. GamePlayActivity really no field "mUnityPlayer" in my case, mUnityPlayer is in other SDK.I use unity as a library.
  2. Unity C# code, when call MobileAds.Initialize trigger;
  3. UnityPlayer maybe can find by R.id.unitySurfaceView;

What happened? How can we make the problem occur? This could be a description, log/console output, etc.

E/Unity: AndroidJavaException: java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class "Lio/wx/gp/play/ui/GamePlayActivity;" or its superclasses java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class "Lio/wx/gp/play/ui/GamePlayActivity;" or its superclasses at com.unity3d.player.UnityPlayer.nativeRender(Native Method) at com.unity3d.player.UnityPlayer.access$800(Unknown Source) at com.unity3d.player.UnityPlayer$g$1.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:203) at com.unity3d.player.UnityPlayer$g.run(Unknown Source) at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.AndroidJNISafe.GetFieldID (System.IntPtr clazz, System.String name, System.String sig) [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine._AndroidJNIHelper.GetFieldID (System.IntPtr jclass, System.String fieldName, System.St

Please share a downloadable sample project that reproduces the bug, if available. This greatly helps us to diagnose and fix the issue faster.

Relevant Code:

            List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };
            // Configure TagForChildDirectedTreatment and test device IDs.
            RequestConfiguration requestConfiguration =
                new RequestConfiguration.Builder()
                .SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
                .SetTestDeviceIds(deviceIds).build();
            MobileAds.SetRequestConfiguration(requestConfiguration);
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize(HandleInitCompleteAction);
NVentimiglia commented 1 year ago

Hi @guanghui1987

I am having trouble understanding the issue. To take a guess, It sounds like you are missing an android dependency which some code needs. That said, it is hard to tell from you summary what that code is. The solution could be as simple as calling Assets > External Dependency Manager > Android Force Resolve, but I could be misunderstanding the issue.

Can you provide a sample project which reproduces this issue on the help forum?

guanghui1987 commented 1 year ago

I am sure Assets > External Dependency Manager > Android Force Resolve succeeded. But I am not sure the behavior which AndroidJNIHelper.GetFieldID mUnityPlayer is admob or Unity.

guanghui1987 commented 1 year ago

Maybe I know the reason. Unity AndroidJavaObject.CallStatic maybe need to access mUnityPlayer. Unity AndroidJavaObject.Call maybe not need to access mUnityPlayer.

I do some testing in my case that is GamePlayActivity no field mUnityPlayer. and the results:

  1. Unity 2021.3.6f1c1 google-ads-mobile-unity v8.1.0 v8.2.0 ok
  2. Unity 2021.3.23f1c1 google-ads-mobile-unity v8.1.0 v8.2.0 not ok
  3. Unity 2021.3.25f1c1 google-ads-mobile-unity v8.1.0 v8.2.0 not ok
    
    public void Initialize(Action<IInitializationStatusClient> initCompleteAction)
    {
    //IL_000c: Unknown result type (might be due to invalid IL or missing references)
    //IL_0012: Expected O, but got Unknown
    //IL_0023: Unknown result type (might be due to invalid IL or missing references)
    //IL_0029: Expected O, but got Unknown
    this.initCompleteAction = initCompleteAction;
    AndroidJavaClass val = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject @static = ((AndroidJavaObject)val).GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass val2 = new AndroidJavaClass("com.google.android.gms.ads.MobileAds");
    ((AndroidJavaObject)val2).CallStatic("initialize", new object[2] { @static, this });
    }
NVentimiglia commented 1 year ago

@guanghui1987

Thanks for checking against different Unity installs, I will take a further look.

NVentimiglia commented 1 year ago

@guanghui1987

I was unable to replicate this issue using 2021.3.25f1c1. I am not sure what is happening, but I would need to look at the project or additional logging to figure out what is happening. If you could, can you please open a support case on the on the help forum?