Skip to content

Android <9 crash - no non-static method UnityPlayerSingleton;.hidePreservedContent() #15

@timbotimbo

Description

@timbotimbo

Description

I'm reposting this issue because it affects this plugin too.

On some Unity versions, android devices on Android 8 or lower will immediately crash when Unity is started.

java.lang.NoSuchMethodError: no non-static method "Lcom/learntoflutter/flutter_embed_unity_android/unity/UnityPlayerSingleton;.hidePreservedContent()V"

Breaking change

I tested multiple Unity versions to find the exact version that breaks this.

The crash starts at Unity 2021.3.31 and 2022.3.10.
Both these versions have this entry in the changelog, that seems like the culprit.

  • Android: Show the last rendered frame when the application come back from the pause state. (UUM-30881)

As of 2022.3.19 this crash no longer happens, but there is nothing mentioned in the Unity changelog.

It's your choice to either add a workaround that might become redundant or tell people to upgrade to 2022.3.19+

Reproduction

  1. Build and run any project using this plugin, like the example.
  2. Wait until Unity launches in Flutter app.
  3. On Android <= 8 and the specified Unity versions, it will instantly crash.

Workaround

For affected versions, you can avoid the crash by defining the missing function and calling the private function using reflection.
This fixes the crash on Android <= 8 and doesn't change any behavior on higher android versions.

I haven't benchmarked this, but it might introduce a some milliseconds delay on Android 9+ as you add a reflection call that isn't otherwise needed.

import java.lang.reflect.Method


     fun hidePreservedContent() {

        // manually call the private function using reflection.
        UnityPlayer::class.java.declaredMethods
        .find { it.name == "hidePreservedContent" }
        ?.let {
            it.isAccessible = true
            it.invoke(this)
        }
    
     }

Versions

❌ indicates a crash.

Unity:
2022.3.0 ✔
2022.3.9 ✔
2022.3.10 ❌
2022.3.14 ❌
2022.3.15 ❌
2022.3.18 ❌
2022.3.19 ✔
2022.3.20 ✔

Android:
Android 6 ❌
Android 7.1.2 ❌
Android 8 ❌ (reported)
Android 9 ✔
Android 13 ✔

Devices:
This seems to happen on any device on Android <= 8.

  • Samsung Galaxy J5, Android 6
  • Wileyfox Swift, Android 7
  • (reported) Huawei Mate 10 Lite, Android 8
  • (reported) Samsung Galaxy S7

Logs

Log Unity 2022.3.18, Android 7
E/AndroidRuntime( 5807): FATAL EXCEPTION: UnityMain
E/AndroidRuntime( 5807): Process: com.learntoflutter.flutter_embed_unity_example, PID: 5807
E/AndroidRuntime( 5807): java.lang.Error: FATAL EXCEPTION [UnityMain]
E/AndroidRuntime( 5807): Unity version     : 2022.3.18f1
E/AndroidRuntime( 5807): Device model      : Wileyfox Wileyfox Swift
E/AndroidRuntime( 5807): Device fingerprint: Wileyfox/Swift/crackling:7.1.2/N2G48B/eeea277114:user/release-keys
E/AndroidRuntime( 5807): CPU supported ABI : [arm64-v8a, armeabi-v7a, armeabi]
E/AndroidRuntime( 5807): Build Type        : Release
E/AndroidRuntime( 5807): Scripting Backend : IL2CPP
E/AndroidRuntime( 5807): Libs loaded from  : lib/arm64
E/AndroidRuntime( 5807): Strip Engine Code : true
E/AndroidRuntime( 5807): 
E/AndroidRuntime( 5807): Caused by: java.lang.NoSuchMethodError: no non-static method "Lcom/learntoflutter/flutter_embed_unity_android/unity/UnityPlayerSingleton;.hidePreservedContent()V"
E/AndroidRuntime( 5807): 	at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
E/AndroidRuntime( 5807): 	at com.unity3d.player.UnityPlayer.-$$Nest$mnativeRender(Unknown Source)
E/AndroidRuntime( 5807): 	at com.unity3d.player.UnityPlayer$F$a.handleMessage(Unknown Source)
E/AndroidRuntime( 5807): 	at android.os.Handler.dispatchMessage(Handler.java:98)
E/AndroidRuntime( 5807): 	at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime( 5807): 	at com.unity3d.player.UnityPlayer$F.run(Unknown Source)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions