juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.13k stars 515 forks source link

onUnityMessage: java.lang.ClassNotFoundException: com.xraph.plugins.flutterunitywidget.UnityUtils #324

Closed gisinator closed 3 years ago

gisinator commented 3 years ago

Describe the bug I have successfully setup Unity 2020.1.17f together with ARFoundation 4.1.5 and flutter 1.22.6 with plugin 4.0.0-alpha.4 . I can send messages from flutter to unity without any issues and control the rotation of an AR cube with the help of flutter buttons, But when I try to send a message from unity to flutter, I see the following error in AndroidStudio logging:

java.lang.ClassNotFoundException: com.xraph.plugins.flutterunitywidget.UnityUtils

To Reproduce Steps to reproduce the behavior:

  1. In a script attached to a Gameobject in unity, add this line into a function: UnityMessageManager.Instance.SendMessageToFlutter("Message from Unity to flutter");

  2. Now, from within flutter, call this function with the help of a flutter --> Unity message

Expected behavior I'd expect to see "Message from Unity to flutter" within my Android Studio logger

Actual behaviour I get the error mentioned in the title

Additional context The same error has been mentioned here and the workaround was this:

Add the following to android > unityLibrary > proguard-unity.txt -keep class com.unity3d.plugin. { ; } -keep class com.xraph.plugins.flutterunitywidget.UnityUtils { *; }

Unfortunately, even with these lines in my progurad-unity.txt, the error does not disappear

Any ideas?

gisinator commented 3 years ago

Two more observations, even though I don't know if they help in any way:

1) when I add some prints in "SendMessageToFlutter(string message)" function within UnityMessageManager.cs script: `public void SendMessageToFlutter(string message) {

if UNITY_ANDROID

        try

        {

        print("now trying to get jc ");

            AndroidJavaClass jc = new AndroidJavaClass("com.xraph.plugins.flutterunitywidget.UnityUtils");

        print("jc tried");

            jc.CallStatic("onUnityMessage", message);

        }

        catch (Exception e)

        {

            print("###damn: " + e.Message);

        }

    #elif UNITY_IOS && !UNITY_EDITOR

        NativeAPI.onUnityMessage(message);

    #endif
}`

I can see both prints "now trying to get jc" and "jc tried" when I build and run in Unity. But as soon as I export it to flutter and build from there, I only see "now trying to get jc", followed by "###damn: java.lang.ClassNotFoundException: com.xraph.plugins.flutterunitywidget.UnityUtils".

2) I setup a new basic 2019.4.17 Unity with only examples from FlutterUnityPackage-3.0.0 and nothing else. Even then I get the same ClassNotFoundException in flutter.

So, it has nothing to do with Unity 2020 or my own code since I just use the Demo that is in the package. Plus, the missing Class must somehow get lost on the way to Flutter since its running without an error when building within Unity.

Any ideas?

jaboyc commented 3 years ago

I'm getting the same issue. Haven't found a workaround yet.

gisinator commented 3 years ago

Still trying to figure out what the problem causes. I'm only a beginner, but this seems at least confusing to me:

In Android Studio after exporting from Unity, I find the following file: "build/flutter_unity_widget/generated/source/buildConfig/debug/com/xraph/plugin/flutter_unity_widget/BuildConfig.java"

In this file, the first line is: package com.xraph.plugin.flutter_unity_widget;

What confuses me: The error message says that the missing class is spelled without those underscores: java.lang.ClassNotFoundException: com.xraph.plugins.flutterunitywidget.UnityUtils

Just trying to throw in my observations and still hoping that someone can figure this out :)

Thanks

jaboyc commented 3 years ago

I finally got it to work! Inside UnityMessageManager.cs in the downloaded unitypackage, simply change line 122 to AndroidJavaClass jc = new AndroidJavaClass("com.xraph.plugin.flutter_unity_widget.UnityPlayerUtils"); and line 139 to AndroidJavaClass jc = new AndroidJavaClass("com.xraph.plugin.flutter_unity_widget.UnityPlayerUtils"); The old code was incorrectly referencing the java class, not sure why, but this finally fixed it for me

gisinator commented 3 years ago

wow, that's awesome, thank you @jaboyc for your ongoing efforts! I can confirm: With these edits within UnityMessageManager, it works :)

@juicycleff any chance you can update the package? :)

juicycleff commented 3 years ago

@jaboyc @gisinator you are using the old UnityPackage. The unity package in v4 branch has the correct references

juicycleff commented 3 years ago

Also the fact that you also see the AndroidClass script in there means it's v3 unity package you are using. I will go ahead and close this.

gisinator commented 3 years ago

The only UnityPackages I could find were FlutterUnityPackage.unitypackage and FlutterUnityPackage-3.0.0, I had not heard of a V4 package before. So, my bad, guess I should have searched further. Anyway, thank you @juicycleff for clarifying and your great plugin :)

juicycleff commented 3 years ago

@gisinator true, I have updated the branch and as such master is now latest. Thanks 😊