flutter/docs/wiki_archive/Experimental-Use-old-plugins-with-new-embedding.md
Kate Lovett 1fbcbb73a0
[wiki migration] Leftover wiki pages and links (#148989)
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
2024-05-28 15:12:10 +00:00

3.1 KiB

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, 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());