Flutter Fixes a Sneaky Multi-Engine Bug: FFI/JNI Plugins Now Know Their Engine ID
Multi-engine Flutter apps using Pigeon's Native Interop can now avoid a silent, hard-to-debug crash where background tasks kill the main engine's plugin state. Without this fix, apps combining shared_preferences with WorkManager or similar background engines risk intermittent failures that only surface in production.
Multi-engine Flutter apps using Pigeon's Native Interop (FFI/JNI) can now tell which FlutterEngine a native plugin call belongs to. PR #195881 exposes engineId on the native plugin binding APIs for Android, iOS, and macOS, closing a gap that caused a nasty failure. When a background engine (for example, from WorkManager) registers a plugin, it overwrites the main engine's plugin instance in a process-level static registry. When the background engine later detaches, it unregisters the plugin entirely, leaving the main engine's next call failing with 'No instance with no name has been registered.'
The root cause is the difference between MethodChannel and Native Interop. With MethodChannel, each engine has its own BinaryMessenger, so plugin calls are naturally routed to the correct instance. Native Interop bypasses that messaging layer with direct JNI/FFI calls, losing the engine identity. The fix restores that identity by exposing engineId on the plugin registrar, allowing plugins to scope their state per engine. The change is small but unblocks reliable multi-engine usage for FFI-based plugins, covering Add-to-App, FCM background tasks, and warm-up scenarios.
The shift from MethodChannel to FFI/JNI trades explicit per-engine message routing for direct calls, but it discards the engine context that was implicit in the messenger.
The bug demonstrates that performance-oriented native interop requires explicit context propagation to avoid cross-engine contamination; engineId is the minimal fix.
Flutter's Native Interop can now support production multi-engine scenarios, but plugin authors must update their registration logic to use the new IDs.