Flutter Fixes a Sneaky Multi-Engine Bug: FFI/JNI Plugins Now Know Their Engine ID
Flutter's FFI/JNI Plugins Finally Can Distinguish Between Different FlutterEngines
Actually, I think this problem is quite niche; if you don't encounter it, you wouldn't need it, but it's still quite practical. PR #195881: Expose engineId on native plugin bindings and registrars now allows distinguishing FlutterEngine directly via Dart through FFI/JNI.
Because previously it was only possible through MethodChannel, i.e., multi-Engine isolation was essentially natural, but now that we are pushing Native Interop, switching to FFI/JNI has actually removed this support.
In short:
- Flutter already has
PlatformDispatcher.instance.engineId, so Dart can know which Engine it belongs to. - The native plugin registration interfaces on Android, iOS, and macOS have not previously exposed this ID uniformly.
- Pigeon's Native Interop uses a process-level static registry; when the second Engine registers a plugin, it may directly overwrite the first Engine's instance.
- When the second Engine exits, it may also unregister the plugin that the first Engine is still using.
So it is quite necessary to let native plugins also get the corresponding engineId during registration and destruction. This was completely fine before; it's a bug that only appears with FFI.
This problem actually originated from Flutter issue #194920. For example, if your Flutter uses shared_preferences and also integrates WorkManager to execute background tasks, you can run into this problem:
The problem lies in the following execution sequence:
- Engine A starts, registers
pluginA, main UI works normally. - WorkManager starts Engine B, registers
pluginB, overwritingpluginAin the registry. - Engine B finishes its task and calls
onDetachedFromEngine(). - When
pluginBis unregistered, it directly removes the corresponding entry from the global registry. - Then Engine A is still alive, but when it calls
SharedPreferences.getInstance()again, it can't find the native plugin instance.
Error typically:
ArgumentError:
No instance with no name has been registered.
This is ridiculous: when a background task ends, it has nothing to do with the main interface, but it unregisters the native API that the main interface is still using. And it doesn't only happen with WorkManager; FCM background Engines, Add-to-App, multi-Engine warm-up, and other scenarios can all encounter similar problems.
Why didn't the old MethodChannel have this problem? The key is BinaryMessenger. Previously, using Pigeon + MethodChannel, the call chain was roughly:
That is, each Engine has its own message channel, so Dart A's requests are naturally handed to pluginA. But after switching to Native Interop, Dart can directly call JNI/FFI:
This loses a crucial piece of information: which FlutterEngine is this call from? The original Kotlin generated code for Pigeon was something like:
val ApiInstances:
MutableMap<String, ApiRegistrar> = mutableMapOf()
Here the String is the instance name. If two Engines both register the default name, they operate on the same key.
You might think, just assign a different name to each Engine, right? That's a lot of talk, but it can partially work around the issue. The problems are:
- All callers must correctly pass the corresponding name.
- Plugin registration happens on the Native side, and the Dart side may not be fully initialized yet.
- The creation and destruction order of FlutterEngine may not be fixed.
- Each plugin must maintain its own mapping from Engine to instance name.
So
engineIdis more appropriate because it's a Flutter Engine concept.
The main changes this time are:
| Platform | New API | Source |
|---|---|---|
| Android | FlutterPluginBinding.getEngineId() |
FlutterEngine.getEngineId() |
| iOS | FlutterBaseRegistrar.engineIdentifier |
FlutterEngine.engineIdentifier |
| macOS | FlutterPluginRegistrar.engineIdentifier |
FlutterEngine.engineIdentifier |
So the problem is simple, but it's actually quite practical, because I have multi-Engine scenarios, and if FFI can load plugins based on this, it gives much more flexibility:
But I really didn't expect that switching from MethodChannel to FFI could have this kind of pitfall awaiting, and Pigeon's bug hasn't been merged yet. You can only say there are always surprises.
Top 2 from juejin.cn, machine-translated. The original thread is authoritative.
Boss, any useful tools you've built with Flutter? I recently used AI to write a mini-program: specialises in resolving couple fights and family discord [melon-eating crowd]
Learning from master Xiao Guo