mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This is waiting on - https://github.com/flutter/flutter/pull/148777 - https://github.com/flutter/flutter/pull/148790 After this PR lands, there will likely be 1-2 more clean up PRs, after which the migration will be done! --- This moves the remaining wiki pages as planned in [flutter.dev/go/migrate-flutter-wiki-spreadsheet](https://docs.google.com/spreadsheets/d/1x65189ZBdNiLRygpUYoU08pwvXD4M-Z157c6pm8deGI/edit?usp=sharing) It also adds the team labels to the label bot for future PRs. Changes to the content were only updating cross links, or links to refer to the main branch rather than master. Remaining links to the wiki will be updated once all other pages have finished moving, they still work in the meantime. Part of https://github.com/flutter/flutter/issues/145009
65 lines
3.1 KiB
Markdown
65 lines
3.1 KiB
Markdown
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.
|
|
|
|
### Partial plugin registration
|
|
|
|
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`](https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen#step-3-optional-use-a-cached-flutterengine), or by overriding `provideFlutterEngine()` in [`FlutterActivity`](https://api.flutter.dev/javadoc/io/flutter/embedding/android/FlutterActivity.html#provideFlutterEngine-android.content.Context-) or [`FlutterFragment`](https://api.flutter.dev/javadoc/io/flutter/embedding/android/FlutterFragment.html#provideFlutterEngine-android.content.Context-) such that the `FlutterEngine` instance doesn't automatically register plugins.
|
|
|
|
```java
|
|
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:
|
|
|
|
```java
|
|
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.
|
|
|
|
```java
|
|
// 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());
|
|
``` |