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

[Android] Fix Unity plugins using the AndroidJavaProxy. (mUnityPlayer error) #908

Merged
merged 4 commits into from
Feb 19, 2024

Conversation

timbotimbo
Copy link
Collaborator

@timbotimbo timbotimbo commented Jan 1, 2024

Description

Unity made changes to their AndroidJavaProxy which causes a crash when using certain packages.
This breaks popular Unity plugins like ARFoundation and Google Ads, and likely many more.

Unity expects a mUnityPlayer property to exist on the MainActivity, but the FlutterActivity used in Flutter apps doesn't have that property.

This change is found in the following Unity versions (and maybe more):

  • 2020.3.46 or higher
  • 2021.3.19 - 2021.3.20
  • 2022.2.4 - 2022.3.18

For the full description and discussion see #836

Error

Example of the error when the app crashes:

Non-fatal Exception: java.lang.Exception: AndroidJavaException : java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "mUnityPlayer" in class "Lcom/example/app/MainActivity;" or its superclasses
       at com.unity3d.player.UnityPlayer.nativeRender(com.unity3d.player.UnityPlayer)
       at com.unity3d.player.UnityPlayer.-$$Nest$mnativeRender(com.unity3d.player.UnityPlayer)
       at com.unity3d.player.UnityPlayer$C$a.handleMessage(com.unity3d.player.UnityPlayer$C$a)
       at android.os.Handler.dispatchMessage(android.os.Handler)
       at android.os.Looper.loopOnce(android.os.Looper)
       at android.os.Looper.loop(android.os.Looper)
       at com.unity3d.player.UnityPlayer$C.run(com.unity3d.player.UnityPlayer$C)
       at UnityEngine.AndroidJNISafe.CheckException(AndroidJNISafe.cs:24)
       at UnityEngine.AndroidJNISafe.GetFieldID(AndroidJNISafe.cs:87)
       at UnityEngine._AndroidJNIHelper.GetFieldID(AndroidJava.cs:1614)
       at UnityEngine.AndroidJNIHelper.GetFieldID(AndroidJNI.bindings.cs:91)
       at UnityEngine._AndroidJNIHelper.GetFieldID[ReturnType](AndroidJava.cs:1534)
       at UnityEngine.AndroidJNIHelper.GetFieldID[FieldType](AndroidJNI.bindings.cs:198)
       at UnityEngine.AndroidJavaObject._Get[FieldType](AndroidJava.cs:630)
       at UnityEngine.AndroidJavaObject.Get[FieldType](AndroidJava.cs:345)
       at UnityEngine.AndroidJNIHelper.CreateJavaProxy(AndroidJNI.bindings.cs:106)

Solution

This pull request adds a new class FlutterUnityActivity and interface IFlutterUnityActivity.
These contain a mUnityPlayer property that gets set by the plugin once Unity is initialized.

This does add a manual setup step, which i've added to the readme.

I've had some users use my branch since August 2023 and I haven't heard of any issues yet.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Merging

This can be merged with a squash merge, to hide the multiple documentation commits.

@timbotimbo timbotimbo merged commit ecee37d into juicycleff:master Feb 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants