当创建一个 FlutterEngine 时,内存成本 ≈ 10–20MB 起(视情况),内部会发生:

  • 初始化 Dart VM
  • 加载 isolate(通常是 main isolate)
  • 加载 AOT snapshot(release)或 JIT kernel(debug)
  • 绑定:Skia 渲染器、Platform Channel、插件注册器
    本质上:
FlutterEngine
   ├── Dart Isolate
   ├── Rendering (Skia)
   ├── Platform Channel
   ├── Asset Bundle
   └── Plugin Registry

0x0. 单引擎和多引擎的对比

多个 Engine,共享底层资源

| 特性           | 单独创建 Engine | 使用 EngineGroup |
| -------------- | -------------- | -------------- |
| Dart VM        | 共享(进程级)   | 共享            |
| Snapshot 加载   | 每次加载        | 首次加载复用     |
| 内存占用        | 高           | 更低              |
| Isolate        | 独立          | 独立             |
| 插件            | 独立          | 独立             |

运行流程图(EngineGroup)

Application
    ↓
FlutterEngineGroup
    ↓
createAndRunEngine()
    ↓
Create Isolate
    ↓
Bind Plugins
    ↓
Attach to Activity/Fragment
    ↓
Render UI

0x1. 单引擎架构图

                ┌─────────────────────────┐
                │       FlutterEngine     │
                │─────────────────────────│
                │  Dart VM Instance       │
                │  (may be shared process)│
                │─────────────────────────│
                │  Root Isolate           │
                │─────────────────────────│
                │  Plugin Registry        │
                │─────────────────────────│
                │  BinaryMessenger        │
                │─────────────────────────│
                │  Skia Renderer          │
                └────────────┬────────────┘
                             │
                       Platform Channels
                             │
                    Android / iOS Native

0x2. 多引擎FlutterEngineGroup架构图

                    ┌──────────────────────┐
                    │      Dart VM         │
                    │  (shared in process) │
                    └──────────┬───────────┘
                               │
        ┌──────────────────────┼──────────────────────┐
        │                      │                      │
┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│ FlutterEngine │      │ FlutterEngine │      │ FlutterEngine │
│   (Engine A)  │      │   (Engine B)  │      │   (Engine C)  │
│───────────────│      │───────────────│      │───────────────│
│ Isolate A     │      │ Isolate B     │      │ Isolate C     │
│ Plugin Reg A  │      │ Plugin Reg B  │      │ Plugin Reg C  │
│ Skia Context  │      │ Skia Context  │      │ Skia Context  │
└───────────────┘      └───────────────┘      └───────────────┘

0x3. 多个FlutterEngine之间的数据传递和共享

方案1:通过Native和Flutter之间的MethodChannel转发

Engine A
   ↓ MethodChannel
Native (Android/iOS)
   ↓
Engine B MethodChannel

1️⃣ Engine A 发送数据

const channel = MethodChannel('engineA');
await channel.invokeMethod('sendToB', {
  "msg": "hello from A"
});

2️⃣ Native 接收并转发

val channelA = MethodChannel(engineA.dartExecutor.binaryMessenger, "engineA")
val channelB = MethodChannel(engineB.dartExecutor.binaryMessenger, "engineB")

channelA.setMethodCallHandler { call, result ->
    if (call.method == "sendToB") {
        val data = call.arguments
        channelB.invokeMethod("receiveFromA", data)
        result.success(null)
    }
}

3️⃣ Engine B 接收

const channel = MethodChannel('engineB');
channel.setMethodCallHandler((call) async {
  if (call.method == "receiveFromA") {
    print(call.arguments);
  }
});

方案2:EventBus(Android),NotificationCenter(iOS)

方案3:共享本地存储

  • SQLite
  • SharedPreferences,
  • 直接文件
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐