开发FFI plugin
可参考 开发FFI插件。
1. 创建 package
flutter create --template=plugin_ffi hello --platforms=android,ios,ohos
2. 构建和绑定本地原生代码
pubspec.yaml 中指定 FFI插件的平台如下:
plugin:
platforms:
android:
ffiPlugin: true
ohos:
ffiPlugin: true
ios:
ffiPlugin: true
3. 放置预编译 so 文件
若插件包含预编译的 so 动态库,需将其放置到插件对应的 ohos/libs/<abi> 目录下。支持的 abi 目录包括:
arm64-v8ax86_64
目录结构示例:
ohos/
└── libs/
├── arm64-v8a/
│ └── libhello.so
└── x86_64/
└── libhello.so
4. 绑定本地原生代码
为了使用本地原生代码,需要在 Dart 中进行绑定。
为了避免手工编写,它们由头文件 (src/hello.h) 中的 package:ffigen 生成。运行以下指令重新生成绑定:
dart run ffigen --config ffigen.yaml
5. 调用本地原生代码
运行时间很短的本地原生函数可以在任何 isolate 中直接调用。例如,请查看 lib/hello.dart 中的 sum。
运行时间较长的本地原生函数应在 helper isolate 上调用,以避免在 Flutter 应用程序中掉帧。例如,请查看 lib/hello.dart 中的 sumAsync。