rmawatson / flutter_isolate

Launch an isolate that can use flutter plugins.
MIT License
262 stars 80 forks source link

[Feature] switch to FlutterEngineGroup to save 99% memory usage #149

Open chipweinberger opened 7 months ago

chipweinberger commented 7 months ago

see here: https://docs.flutter.dev/add-to-app/multiple-flutters

The 2.0.0 Flutter release drastically reduces the memory footprint of additional Flutter engines from ~19MB on Android and ~13MB on iOS, to ~180kB on Android and iOS. This ~99% fixed cost reduction allows the multiple Flutters pattern to be used more liberally in your add-to-app integration.

sample code: https://github.com/flutter/samples/tree/master_archived/add_to_app/multiple_flutters

Here's a basic guide on how to use multiple Flutters in both Android and iOS projects:

For Android:

  1. Add Flutter to Your Project: Ensure you have Flutter integrated into your existing Android project. Follow the official guide if you haven't done this yet.

  2. Use FlutterEngineGroup: FlutterEngineGroup is a class that manages multiple FlutterEngine instances. You can create a single FlutterEngineGroup and use it to spawn multiple FlutterEngine instances.

    Example:

    FlutterEngineGroup engineGroup = new FlutterEngineGroup(context);
    
    // Create a FlutterEngine for a specific entrypoint or Dart file
    FlutterEngine flutterEngine = engineGroup.createAndRunEngine(context, entrypoint);
  3. Display Flutter UI: Use FlutterActivity or FlutterFragment to display the UI rendered by a FlutterEngine. Each instance can run different Dart entrypoints or the same entrypoint with different initial routes.

    Example:

    startActivity(
       FlutterActivity
           .withNewEngine()
           .initialRoute("/my_route")
           .build(context)
    );

For iOS:

  1. Add Flutter to Your Project: Integrate Flutter into your existing iOS project following the official integration guide.

  2. Use FlutterEngineGroup: On iOS, use FlutterEngineGroup to manage multiple FlutterEngine instances.

    Example:

    FlutterEngineGroup* engineGroup = [[FlutterEngineGroup alloc] initWithName:@"my_engines" project:nil];
    
    // Create a FlutterEngine
    FlutterEngine* flutterEngine = [engineGroup makeEngineWithEntrypoint:nil libraryURI:nil];
  3. Display Flutter UI: Use a FlutterViewController to display the Flutter content. Each FlutterEngine can be attached to its own FlutterViewController.

    Example:

    FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    [self presentViewController:flutterViewController animated:YES completion:nil];

Notes:

For more detailed information and the latest updates, refer to the Flutter documentation and the multiple_flutters module in the official Flutter samples repository.

chipweinberger commented 7 months ago

some discussion here: https://github.com/rmawatson/flutter_isolate/issues/111#issuecomment-1152470831

chipweinberger commented 7 months ago

PRs: