跪拜 Guibai
← All articles
Android · Flutter · Frontend

Flutter Fixes a Sneaky Multi-Engine Bug: FFI/JNI Plugins Now Know Their Engine ID

By 恋猫de小郭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— PR #195881 adds engineId to native plugin bindings and registrars for Android, iOS, and macOS.
— Android now exposes FlutterPluginBinding.getEngineId(), sourced from FlutterEngine.getEngineId().
— iOS gains FlutterBaseRegistrar.engineIdentifier, mapped from FlutterEngine.engineIdentifier.
— macOS gets FlutterPluginRegistrar.engineIdentifier, also from FlutterEngine.engineIdentifier.
— Pigeon's Native Interop uses a static registry, so a second engine's plugin registration overwrites the first engine's entry.
— When the second engine detaches, it unregisters the shared entry, causing 'No instance with no name has been registered' for the first engine.
— Scenarios like WorkManager background tasks, FCM background engines, Add-to-App, and multi-engine warm-up are all at risk.
Conclusions

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.

Concepts & terms
engineId
A unique identifier for each FlutterEngine instance, now available in both Dart and native plugin bindings, allowing plugins to scope their state per engine.
BinaryMessenger
The per-engine message channel in MethodChannel that routes Dart-to-native calls to the correct plugin instance, ensuring each engine's calls go to its own plugins.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗