Skip to content

fix(android): app crashes if the phone rotates while it's starting - #525

Open
SRWieZ wants to merge 1 commit into
NativePHP:mainfrom
SRWieZ:fix/android-native-event-before-bridge-loads
Open

SRWieZ wants to merge 1 commit into
NativePHP:mainfrom
SRWieZ:fix/android-native-event-before-bridge-loads

Conversation

@SRWieZ

@SRWieZ SRWieZ commented Oct 3, 2026

Copy link
Copy Markdown
Member

What's wrong

On Android, if the phone rotates (or switches dark mode) in the first moments after launch, the app crashes:

java.lang.UnsatisfiedLinkError: No implementation found for void
  com.nativephp.mobile.ui.nativerender.NativeElementBridge.nativeElementWriteEvent(int, int, int, byte[])
  at NativeElementBridge$Companion.sendNativeEvent(NativeElementBridge.kt:873)
  at MainActivity.sendOrientationChanged(MainActivity.kt:513)
  at MainActivity.onConfigurationChanged(MainActivity.kt:508)

Seen on a Xiaomi running Android 16, 1.5 s after launch. It depends on timing, so it's hard to trigger on purpose: turn the phone right as the app opens.

Why

sendNativeEvent() calls a native (C) function. That function only exists once the PHP runtime has loaded, and the runtime loads lazily, a moment after the screen appears. A rotation or dark-mode change that arrives before that point calls a function that isn't there yet.

What this does

If the runtime isn't loaded yet, the event is dropped (with a log line) instead of crashing. Nothing is lost: PHP reads the orientation and appearance itself when it starts. One file, iOS untouched.

…runtime loads

NativeElementBridge.sendNativeEvent called nativeElementWriteEvent
unguarded, but the JNI library only loads (and registers that method)
lazily with the PHP runtime. A configuration change in the first moments
of launch — a rotation, a dark-mode flip — reached onConfigurationChanged
first and crashed the app with UnsatisfiedLinkError (seen on a Xiaomi
running Android 16, 1.5 s after launch). The event is now dropped instead;
PHP reads orientation and appearance fresh when it boots.
@simonhamp

Copy link
Copy Markdown
Member

Does it need to return if it fails? Can't we just log it and continue as we were before?

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.

2 participants