ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Flutter OHOS 三方库与插件适配相关问题

Flutter OHOS 三方库与插件适配相关问题 ohos 平台适配 flutter 三方库指导ohos 平台适配 flutter 三方库说明文档模版识别 Flutter 三方库是否需要适配 OpenHarmony插件工程的 ohos 目录无法在 DevEco 中同步问题现象在 DevEco Studio 中直接打开 Flutter 插件工程的ohos目录无法正常同步Sync项目。问题分析这是 DevEco Studio 的设计行为。Flutter 插件的ohos目录是 OpenHarmonymodule结构而非独立的 OpenHarmonyproject工程因此无法作为独立工程在 DevEco 中打开和同步。解决方案在 example 工程中开发调试使用 DevEco Studio 打开插件的example/ohos目录在该工程中开发和调试 ohos 平台代码。详见开发Flutter Plugin。以源码方式引用插件在宿主 Flutter 工程中通过 hvigor 插件方式直接依赖插件源码构建这样编写插件时可直接修改源码并获得代码提示体验与 Android、iOS 一致。详见使用 hvigor 插件方式编译 flutter 项目。注意对于既有的 flutter plugin 项目如果不做任何修改仅会导致 example 无法正常编译但不影响该 plugin 在 flutter 项目中的正常使用和编译。flutter pub get 依赖冲突报错报错日志Resolving dependencies... Because flutter_cache_manager 3.0.0-nullsafety.03.3.2depends on path_provider from hosted and flutter_cache_manager depends on path_provider from git, flutter_cache_manager 3.0.0-nullsafety.03.3.2forbidden. So bedause xxx depends on flutter_cache_manager3.3.1, version solving failed. pub get failed ...exitcode:1解决方案使用dependency_overrides消除依赖冲突。dependencies:flutter:sdk:flutterdependency_overrides:path_provider:git:url:https://gitcode.com/CPF-Flutter/flutter_packages.gitpath:packages/path_provider/path_providerpath_provider_ohos:git:url:https://gitcode.com/CPF-Flutter/flutter_packages.gitpath:packages/path_provider/path_provider_ohosFFI 插件通过 git 引入后 har 未打入 so 包问题现象FFI 插件引入预编译 so 包本地通过path依赖引入时 so 正常打入 har/hap改为git依赖后构建产物内不含 so运行期出现dlopen failed: xxx.so not found。问题分析本地path依赖时构建直接从本地磁盘的插件ohos/libs/abi/取 so不经过 git而git依赖时pub 将插件源码克隆到 PUB 缓存后纳入构建so 必须存在于 git 仓库源码的ohos/libs/abi/中。两者表现不一致的根因通常在于 so 未被提交到 git。常见原因与排查按概率从高到低so 未提交到 git最常见检查仓库根与ohos/.gitignore是否忽略了*.so或libs/。用git ls-files | grep \.so确认 so 已被跟踪若为空需用git add -f ohos/libs/arm64-v8a/libhello.so强制添加并修正.gitignore。so 目录位置错误so 必须放在ohos/libs/abi/ohos 模块根的libs/放在src/main/libs/等位置不会被 hvigor 自动打包。未移除 externalNativeOptions使用预编译 so 时ohos/build-profile.json5中的externalNativeOptions指向../src/CMakeLists.txt应移除否则会尝试从 CMake 源码现场编译。abi 不匹配真机为arm64-v8a模拟器为x86_64需将 so 放在对应 abi 目录。解决方案完整的端到端开发、打包、验证与发布流程见开发FFI plugin第 3 节放置 so、第 6 节验证 har、第 7 节发布到 git、第 8 节常见问题。Flutter 插件调用报 MissingPluginException问题现象Dart 侧调用某个 Flutter 插件如path_provider、url_launcher、shared_preferences等时抛出MissingPluginException典型日志如下[ERROR:flutter/runtime/dart_vm_initializer.cc(XXX)]Unhandled Exception:MissingPluginException(NoimplementationfoundformethodgetTemporaryDirectoryonchannelplugins.flutter.io/path_provider)#0MethodChannel._invokeMethod(package:flutter/src/services/platform_channel.dart:XXX) #1...该日志仅说明Dart 侧发出的平台通道调用没有原生端响应并不能直接判断是哪种原因。在 OHOS 上MissingPluginException有3 类根因请按下方决策树顺序排查。根因排查决策树按排查优先级Dart 侧抛 MissingPluginException │ ├─ 步骤1检查引擎是否走完标准 onAttach 生命周期 │ │ 根因 C生命周期缺 attachToAbility——最隐蔽最易被误判优先排查 │ └─ HiLog 中能搜到configureFlutterEngine/attachToAbility吗 │ ├─ 否 → 引擎未附着、插件注册流程被跳过 → 见【根因 C】 │ └─ 是 → 继续 │ ├─ 步骤2检查 GeneratedPluginRegistrant 是否调用 │ │ 根因 A漏调 registerWith——最常见的文档提示 │ └─ configureFlutterEngine 中有GeneratedPluginRegistrant.registerWith(engine)吗 │ ├─ 否 → 插件原生端实现未绑定到引擎 → 见【根因 A】 │ └─ 是 → 继续 │ └─ 步骤3检查插件本身是否有 ohos 平台层 │ 根因 B插件缺 ohos/ 目录 └─ 插件 pub 缓存目录下有 ohos/ 吗 ├─ 否 → 即便注册也无效 → 见【根因 B】 └─ 是 → 需抓全量日志进一步定位参考 troubleshooting/dfx/为什么先排 C根因 A漏调registerWith在多数文档里被当作唯一原因但实际项目中 C 类生命周期异常更隐蔽开发者的registerWith往往已经写了按 A 排查会扑空真正缺的是onAttach()/attachToAbility()。先确认生命周期再查注册调用。根因 C生命周期缺 attachToAbility优先排查最易误判症状特征registerWith已调用、插件也有ohos/目录但插件调用仍报MissingPluginException。原理插件原生 handler 的注册发生在onAttachedToEngine()中该回调由delegate.onAttach()触发的引擎创建 插件add()流程调用。delegate.onAttach()内部执行顺序为setupFlutterEngine() → attachToAbility() → providePlatformPlugin() → configureFlutterEngine() → processPendingMessages()。若宿主未走标准onAttach()引擎未附着到 Ability、configureFlutterEngine()不会被框架回调开发者即使写了registerWith也不会执行。常见触发场景Add-to-App 混编场景中自定义EntryAbility未继承FlutterAbility、未创建FlutterAbilityAndEntryDelegate、或onCreate()中漏调this.delegate?.onAttach(this.context)。自定义FlutterEntry子类未在页面aboutToAppear()中调用flutterEntry.aboutToAppear()该方法内部触发createView → onAttach → onWindowStageCreate。手动通过engines.createAndRunEngineByOptions()创建引擎后只调用了GeneratedPluginRegistrant.registerWith(engine)**未调用engine.getAbilityControlSurface()?.attachToAbility(host)**导致 AbilityAware 插件拿不到上下文、channel 绑定异常。验证步骤抓取 HiLog 过滤关键字确认onAttach()是否执行hdc shell hilog |grep -E configureFlutterEngine|attachToAbility|FlutterAbilityAndEntryDelegate若搜不到configureFlutterEngine相关日志说明onAttach()流程未走完插件注册被跳过。检查EntryAbility是否实现ExclusiveAppComponent、是否在onCreate/onDestroy中成对调用FlutterManager.getInstance().pushUIAbility(this)/popUIAbility(this)在onWindowStageCreate/onWindowStageDestroy中成对调用pushWindowStage(this, windowStage)/popWindowStage(this)。混编场景确认flutterEntry.aboutToAppear()在页面aboutToAppear中被调用且getFlutterView()在其之后调用。解决方案补齐标准生命周期调用详见OpenHarmony 应用生命周期回调机制§3.2.2 attachToAbility 调用时机与使用 FlutterEntry 实现混编。根因 A漏调 GeneratedPluginRegistrant.registerWith症状特征标准FlutterAbility流程引擎自动创建、onAttach正常但 Dart 侧插件仍报错。原理FlutterAbility/FlutterEntry默认只创建引擎不自动注册任何插件。GeneratedPluginRegistrant.registerWith(engine)会将pubspec.yaml引入的所有插件原生端实现绑定到引擎省略后 Dart 侧调用插件时原生 handler 不存在。验证步骤在自定义FlutterEntry/FlutterAbility的configureFlutterEngine中检查是否调用configureFlutterEngine(flutterEngine: FlutterEngine): void { super.configureFlutterEngine(flutterEngine);// 必须调用 superGeneratedPluginRegistrant.registerWith(flutterEngine);// 不可省略this.delegate?.addPlugin(newCustomPlugin());// 自定义插件}注意手动通过engines.createAndRunEngineByOptions()创建的引擎框架不会自动注册插件必须开发者自行调用registerWith(engine)并视场景补attachToAbility(host)参见根因 C 场景 3。解决方案补齐GeneratedPluginRegistrant.registerWith(flutterEngine)调用详见FlutterEntry 插件注册第二步与FlutterEnginePreload 预加载。根因 B插件本身缺 ohos 平台层症状特征registerWith已调用、生命周期也正常但只有个别插件报MissingPluginException其余插件正常。原理三方插件若未提供ohos/平台层实现GeneratedPluginRegistrant.registerWith()中不会包含该插件的原生绑定Dart 侧调用其平台通道时无人响应。验证步骤在插件的 pub 缓存目录~/.pub-cache/hosted/.../plugin_name/下检查是否有ohos/目录。或用识别 Flutter 三方库是否需要适配 OpenHarmony提供的方法识别。解决方案若社区已有 ohos 适配版本多在flutter_packages仓库改用 git 引用dependency_overrides: plugin_name: git: url: https://gitcode.com/CPF-Flutter/flutter_packages.git path: packages/plugin_name/plugin_name若无现成适配需自行适配详见三方库适配指导。
返回列表