-
Notifications
You must be signed in to change notification settings - Fork 1
Experimental: Use old plugins with new embedding
Flutter's v2 Android embedding includes reflection code that should find and register all plugins listed in your pubspec.yaml
file without any intervention on your part. If your desired plugins are not registered automatically, please file an issue.
To prevent Flutter from registering all plugins and instead register only specific plugins of your choosing, do the following.
First, construct a FlutterEngine
either as a cached FlutterEngine
, or by overriding provideFlutterEngine()
in FlutterActivity
or FlutterFragment
such that the FlutterEngine
instance doesn't automatically register plugins.
FlutterEngine flutterEngine = new FlutterEngine(
context,
FlutterLoader.getInstance(),
new FlutterJNI(),
dartVmArgs, // or an empty array if no args needed
false // this arg instructs the FlutterEngine NOT to register plugins automatically
);
Second, register the plugins that you want. If you overrode provideFlutterEngine()
in FlutterActivity
or FlutterFragment
then override configureFlutterEngine()
to add plugins:
public void configureFlutterEngine(FlutterEngine engine) {
// The ShimPluginRegistry is how the v2 embedding works with v1 plugins.
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(
flutterEngine,
new PlatformViewsController()
);
// Add any v1 plugins to the shim
// MyV1Plugin.registerWith(
// shimPluginRegistry.registrarFor("com.my.package.MyV1Plugin")
// );
// Add any v2 plugins that you want
// engine.getPlugins().add(new MyPlugin());
}
If you went with the cached FlutterEngine
approach instead of FlutterActivity
and FlutterFragment
method overrides, then you can add plugins whenever you'd like. You can even add them immediately after instantiating your FlutterEngine
. However, be advised that some v1 plugins expect an Activity
to be available immediately upon registration. This will not be the case unless you add plugins in configureFlutterEngine()
as shown earlier.
// Instantiate cached FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(
context,
FlutterLoader.getInstance(),
new FlutterJNI(),
dartVmArgs, // or an empty array if no args needed
false // this arg instructs the FlutterEngine NOT to register plugins automatically
);
// Immediately add plugins to the cached FlutterEngine.
// The ShimPluginRegistry is how the v2 embedding works with v1 plugins.
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(
flutterEngine,
new PlatformViewsController()
);
// Add any v1 plugins to the shim
// MyV1Plugin.registerWith(
// shimPluginRegistry.registrarFor("com.my.package.MyV1Plugin")
// );
// Add any v2 plugins that you want
// engine.getPlugins().add(new MyPlugin());
- Home of the Wiki
- Roadmap
- API Reference (stable)
- API Reference (master)
- Glossary
- Contributor Guide
- Chat on Discord
- Code of Conduct
- Issue triage reports
- Our Values
- Tree hygiene
- Issue hygiene and Triage
- Style guide for Flutter repo
- Project teams
- Contributor access
- What should I work on?
- Running and writing tests
- Release process
- Rolling Dart
- Manual Engine Roll with Breaking Commits
- Updating Material Design Fonts & Icons
- Postmortems
- Setting up the Framework development environment
- The Framework architecture
- The flutter tool
- API Docs code block generation
- Running examples
- Using the Dart analyzer
- The flutter run variants
- Test coverage for package:flutter
- Writing a golden-file test for package:flutter
- Setting up the Engine development environment
- Compiling the engine
- Debugging the engine
- Using Sanitizers with the Flutter Engine
- Testing the engine
- The Engine architecture
- Flutter's modes
- Engine disk footprint
- Comparing AOT Snapshot Sizes
- Custom Flutter engine embedders
- Custom Flutter Engine Embedding in AOT Mode
- Flutter engine operation in AOT Mode
- Engine-specific Service Protocol extensions
- Crashes
- Supporting legacy platforms
- Metal on iOS FAQ
- Engine Clang Tidy Linter
- Why we have a separate engine repo
- Reduce Flutter engine size with MLGO
- Setting up the Plugins development environment
- Setting up the Packages development environment
- Plugins and Packages repository structure
- Plugin Tests
- Contributing to Plugins and Packages
- Releasing a Plugin or Package
- Unexpected Plugins and Packages failures