flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

[Flutter 3.22 & newer] AR crashes on Android < 13 - NoClassDefFoundError: Landroid/window/OnBackInvokedCallback

Open timbotimbo opened this issue 1 year ago • 10 comments

[UPDATE]
Check my comment below for a workaround.

Description

Since Flutter 3.22, Unity will crash on certain Android versions when ARFoundation is activated.
This only happens on Android <13. This might also affect other unity plugins using native code, I just noticed it using ARFoundation.

Unity works fine for 3D scenes but crashes when I switch to a scene that includes ARFoundation and its ARSession.

Error in the console: (unity 2022.3)

E/Unity   ( 7029): AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;
E/Unity   ( 7029): java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;
E/Unity   ( 7029): 	at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method)
E/Unity   ( 7029): 	at java.lang.reflect.Method.getReturnType(Method.java:148)
E/Unity   ( 7029): 	at java.lang.Class.getDeclaredMethods(Class.java:1880)
E/Unity   ( 7029): 	at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source:26)
E/Unity   ( 7029): 	at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
E/Unity   ( 7029): 	at com.unity3d.player.UnityPlayer.-$$Nest$mnativeRender(Unknown Source:0)
E/Unity   ( 7029): 	at com.unity3d.player.UnityPlayer$F$a.handleMessage(Unknown Source:122)
E/Unity   ( 7029): 	at android.os.Handler.dispatchMessage(Handler.java:102)
E/Unity   ( 7029): 	at android.os.Looper.loop(Looper.java:214)
E/Unity   ( 7029): 	at com.unity3d.player.UnityPlayer$F.run(Unknown Source:24)
E/Unity   ( 7029): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.window.OnBackInvokedCallback" on path: DexPathList[[zip file "

(unity 2023.2)

E/Unity   (17280): AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;
E/Unity   (17280): java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;
E/Unity   (17280):   at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method)
E/Unity   (17280):   at java.lang.reflect.Method.getReturnType(Method.java:148)
E/Unity   (17280):   at java.lang.Class.getDeclaredMethods(Class.java:1880)
E/Unity   (17280):   at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source:26)
E/Unity   (17280):   at com.unity3d.player.UnityPlayerForActivityOrService.nativeRender(Native Method)
E/Unity   (17280):   at com.unity3d.player.S.handleMessage(Unknown Source:140)
E/Unity   (17280):   at android.os.Handler.dispatchMessage(Handler.java:102)
E/Unity   (17280):   at android.os.Looper.loop(Looper.java:214)
E/Unity   (17280):   at com.unity3d.player.V.run(Unknown Source:24)
E/Unity   (17280): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.window.OnBackInvokedCallback" on path: DexPathList[

More info

Looking up OnBackInvokedCallback, it is added in API 33 (android 13) and is related to the predictive back gesture in Android 14.

I narrowed it down to this commit on Flutter master, which includes the engine PR Platform channel for predictive back in route transitions on android. Any Flutter master commit before this one won't crash.

I can also reproduce this with the flutter_embed_unity plugin. So it isn't specific to code in this plugin.

Flutter version

Crashed on:

  • Flutter 3.22,0, 3.22.2
  • master (3.23.0-13.0.pre.265 revision a9f554ac39)

Did not crash on:

  • Flutter 3.13.9
  • Flutter 3.16.9
  • Flutter 3.19.6.

Unity version

Only tested with: 2022.3.21f1, 2022.3.33f1, 2023.2.20f1. ArFoundation 5.1.2, 5.1.4 and 5.1.5

Android devices

Devices without crash:

  • Samsung S20FE, Android 13
  • Xiaomi redmi note 10 pro, Android 13
  • Wilefox Swift, Android 7, (doesn't support AR)

Devices with crash:

  • Samsung S10, Android 12
  • Nokia 8.1, Android 11
  • Lenovo tab M10, Android 10
  • Samsung S8, Android 9

Some things I've tried

  • Adding proguard rules like -keep class android.window.** { *; } -keep class android.window.OnBackInvokedCallback { *; }.
  • Adding android:enableOnBackInvokedCallback="false" to all androidmanifest activities and applications.
  • Updating to the latest Unity 2022.3.33
  • Updating Android Studio to Jellyfish 2023.3.1 Patch2 and updating all SDK tools
  • Adding implementation "androidx.activity:activity:1.6.0 in gradle (OnbackInvokedCallback is mentioned in the changes.)

timbotimbo avatar Jun 21 '24 08:06 timbotimbo

Yup, got the same for Vuforia integration. The app successfully runs on android version 14, but not on anything else. It results on black screen.

image

KailaashJeevan avatar Jun 21 '24 09:06 KailaashJeevan

I`ve also experienced some crashes too, but related to memory leak after open unity AR interaction my app a go back to previous screen.

CristovoXDGM avatar Jun 24 '24 08:06 CristovoXDGM

After a report in the Flutter repo this can be labeled as a Unity issue.

The cause

Once AR boots up, Unity uses the getMethodID function in the ReflectionHelper class of unity-classes.jar . This function gets a list of all function in the current Activity using Java reflection getDeclaredMethods(), and then tries to match function signatures. It then repeats this for all super classes of the current Activity.

The getDeclaredMethods call instantly crashes on Android <13 because it can't handle the OnBackInvokedCallback return type that is defined in API 33.

What now

Flutter won't stop using this class for a few people using the niche use-case of embedding Unity with AR. So AR is broken from now on unless this can be fixed on the Unity side.

With some luck a try-catch around getDeclaredMethods might be enough.

However this means either:

  1. Getting Unity to modify their reflection usage.
  2. Decompiling unity-classes.jar to add a try-catch.

I've tried to decompile unity-classes.jar, but none of the decompilers that I could find returned valid code that could compile again without errors.

Unity reproduction

To replicate this issue witthout Flutter:

  1. export a Unity project using AR as an Android project.
  2. edit the UnityPlayerActivity.java file. Either add a parent Activity that includes this function, or add this function to the Activity itself.
import android.annotation.TargetApi;
import android.window.OnBackInvokedCallback;
   @TargetApi(33)
    protected OnBackInvokedCallback getOnBackInvokedCallback() {
        return null;
    }
  1. Now run this using Android Studio on on a device with Android API <33.

TLDR (too long didn't read)

AR is broken with Flutter 3.22+. Don't expect this to be fixed for months, if at all.

timbotimbo avatar Jul 21 '24 17:07 timbotimbo

This is now in the Unity issue tracker.

They seem to have omitted most details from my bug report, but I guess it is a first step.

Anyone affect by this should probably upvote this in the issuetracker.

timbotimbo avatar Aug 13 '24 08:08 timbotimbo

note that if you want to use AR with flutter 3.19.6 on android, make sure useAndroidViewSurface: true or the player view will freeze

t5ujiri avatar Aug 19 '24 09:08 t5ujiri

Unity got back to me on the bug report, calling it "By Design".

Given that Flutter devs point me at Unity, and Unity considers it by design, AR is likely a lost cause on Android < 13.

Since 3.16 and 3.19 contain various serious bugs (https://github.com/juicycleff/flutter-unity-view-widget/issues/912, https://github.com/juicycleff/flutter-unity-view-widget/issues/929) the last reliable version is 3.13.9, which is getting old.

timbotimbo avatar Oct 11 '24 08:10 timbotimbo

I took a final look at this and actually found a simple workaround. Using FlutterFragmentActivity instead of FlutterActivity prevents the crash.

This because the FlutterFragmentActivity did not (yet?) get updated with the OnBackInvokedCallback type that Unity crashes on.

Workaround

Update MainActivity.kt to use this:

import io.flutter.embedding.android.FlutterFragmentActivity;

class MainActivity: FlutterFragmentActivity() {
}

I tested ARFoundation on a Galaxy S8 with Android 9 on these Flutter versions:

  • Flutter 3.22.3
  • Flutter 3.24.5
  • Flutter 3.27.1
  • Flutter master (Flutter 3.28.0-1.0.pre.158) (impeller disabled)
  • Flutter 3.29.3

This workaround should allow AR apps stuck on Flutter 3.1x to upgrade to 3.22+.
Flutter still seems to be working on predictive-back support for FragmentActivity, so there is a chance that this workaround will be broken in a future Flutter update.

Drawbacks and side effects

I'm still playing around with this so this list might not be complete yet.

  • Using FragmentActivity might break compatibility with some other Flutter plugins that require you to use their MainActivity.

  • Checking the Flutter repo issues, there also appear to be some bugs with the android back button/gesture in FlutterFragmentActivity.

    For example with predictive back enabled on Android 13+, the back gesture/button might close the entire app instead of popping the current page. To be safe for now, disable predictive back or only use this workaround for Android < 13.

<!-- Androidmanifest -->
<application
    ...
    android:enableOnBackInvokedCallback="false">

So if you want to test this workaround, pay extra attention to the 'back' behavior in your app.

timbotimbo avatar Dec 17 '24 11:12 timbotimbo

Also works for me on the most recent flutter version (3.29.0).

BenVae avatar Feb 14 '25 10:02 BenVae

Still working on Flutter version 3.32.2 thank you so much bro.

nachischo avatar Jun 09 '25 16:06 nachischo