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 (main)
- Glossary
- Contributor Guide
- Chat on Discord
- Design documents
- Code of Conduct
- Issue triage reports (latest)
- Our Values
- Tree hygiene
- Issue hygiene and Triage
- Style guide for Flutter repo
- Project teams
- Contributor access
- What should I work on?
- Popular issues
- Running and writing tests
- Release process
- Flutter Framework Gardener Rotation
- Rolling Dart
- Manual Engine Roll with Breaking Commits
- Updating Material Design Fonts & Icons
- Postmortems and Retrospectives
- Hotfix Documentation Best Practices
- In case of emergency
- Landing Changes With Autosubmit
- Setting up the Framework development environment
- The Framework architecture
- 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
- Managing template image assets
- 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
- Crashes
- more...
- Setting up the Packages development environment
- Plugins and Packages repository structure
- Contributing to Plugins and Packages
- Understanding Packages tests
- Plugin Tests
- Releasing a Plugin or Package
- more...