Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 biometric_storage

指纹/人脸保护的加密存储,是密钥、令牌类数据的安全落点。biometric_storage 把“认证 + 写读”合并成简单的存储 API:认证通过才能读写受保护内容。把它带到鸿蒙时,生物识别与安全存储是两个都要对齐的能力面。

本文以 biometric_storage 5.0.1 为例,记录我如何保留原有 Dart API、补齐 Flutter OH 插件入口、在鸿蒙端接入真实系统能力,并在 HarmonyOS API 26 真机上完成构建、安装、启动和按钮调用验证。

适配仓库: oh-flutter/biometric_storage

适配分支: feat/ohos_biometric_storage_5.0.1

适配 TAG: 5.0.1-ohos.2

一、先看最终效果

图 1:HarmonyOS API 26 真机启动 Demo。页面显示 biometric_storage 5.0.1。

图 2:点击“查询生物认证能力”后返回 CanAuthenticateResponse.unsupported——本机未录入生物识别,userAuth.getAvailableStatus 如实返回。

验证点实测结果证据
Flutter OH 3.47 工程构建通过entry-default-signed.hap
安装、启动(真机)通过图 1
canAuthenticate真实查询(errorHwUnavailable 表示本机未录入生物识别)图 2
write → read写入并原样读回图 3
加密存储已接入 Asset Store Kit图 3

图 3:点击“读取加密存储”后返回 read - b3-roundtrip-20261016——写入系统加密存储后原样读回。

二、这个库原来是怎么工作的

biometric_storage 的接口分两层:

Dart BiometricStorage
  -> MethodChannel('biometric_storage')
  -> Android KeyStore + BiometricPrompt / iOS Keychain + LAContext
  -> canAuthenticate / init / read / write / delete

canAuthenticate 返回能力枚举,getStorage(name) 拿到句柄后 read/write 触发认证流程。

三、实际环境

Flutter 鸿蒙环境搭建不在本文重复展开,直接参考 Flutter OH 环境搭建指南。

组件版本
Flutter OH3.47.4-ohos-1.0.4
Dart3.13.3
DevEco Studio26.0.0 Release
HarmonyOS SDKAPI 26
测试设备华为畅享 90 Pro Max,HarmonyOS 7.0.0.105(API 26)
目标库biometric_storage 5.0.1
flutter --version
hdc list targets

四、确认上游版本并同步到 AtomGit

上游包地址是 https://github.com/authpass/biometric_storage,版本 5.0.1 使用 MIT(Copyright © 2019 Herbert) License。适配前我在 AtomGit 目标组织中搜索了同名库,没有找到可以直接使用的 OHOS 实现。

仓库适配提交为 f6ef42b,分支与 TAG 5.0.1-ohos.2 均指向该提交。

git clone https://atomgit.com/oh-flutter/biometric_storage.git
cd biometric_storage
git switch feat/ohos_biometric_storage_5.0.1
git log --oneline -5

五、创建适配分支并检查工程结构

git switch -c feat/ohos_biometric_storage_5.0.1

关键目录如下:

biometric_storage/
├── lib/                                        # 原有 Dart API 和 MethodChannel
├── ohos/
│   ├── index.ets                               # OHOS 插件导出入口
│   └── src/main/ets/components/plugin/
│       └── BiometricStoragePlugin.ets                           # 通道实现
├── example/lib/main.dart                        # 真机验证 Demo
├── example/ohos/                                # Flutter OH 宿主工程
├── pubspec.yaml                                # 声明 ohos 平台
└── README.OpenHarmony_CN.md                    # 鸿蒙适配说明

pubspec.yaml 中的关键声明是:

flutter:
  plugin:
    platforms:
      ohos:
        pluginClass: BiometricStoragePlugin

pluginClass 必须和 ohos/index.ets 导出的类名一致,否则 Dart 端能编译,运行时却不会注册插件。

六、适配过程:注册通道并对齐语义

// 秘密存储:Asset Store Kit(系统级加密存储)
const attributes = new Map<asset.Tag, asset.Value>();
attributes.set(asset.Tag.ALIAS, this.toBytes(name));
attributes.set(asset.Tag.SECRET, this.toBytes(content));
attributes.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_UNLOCKED);
await asset.add(attributes);

// 读回
const query = new Map<asset.Tag, asset.Value>();
query.set(asset.Tag.ALIAS, this.toBytes(name));
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL);
const entries = await asset.query(query);
const secret = entries[0].get(asset.Tag.SECRET) as Uint8Array;
方法OpenHarmony 实现
canAuthenticateuserAuth.getAvailableStatus(FACE, ATL2) 真实查询
write / read / delete@ohos.security.asset(DEVICE_UNLOCKED 受锁屏保护)
init查询一次验证存储链路

七、构建配置与签名

示例工程的目标 SDK 对齐到 26,与 @ohos/flutter_ohos 的最低要求一致:

{
  "name": "default",
  "compatibleSdkVersion": "26.0.0",
  "targetSdkVersion": "26.0.0",
  "runtimeOS": "HarmonyOS"
}

Demo 的 AppScope/app.json5 统一成 com.example.flutter_oh_demo,与 DevEco 调试签名对应。证书、p12、p7b 和密码没有提交到仓库,读者应在自己的 DevEco Studio 中生成调试签名。

八、通过 AtomGit 引入适配版

在另一个 Flutter OH 应用中使用时,建议锁定 TAG:

dependencies:
  biometric_storage:
    git:
      url: https://atomgit.com/oh-flutter/biometric_storage.git
      ref: 5.0.1-ohos.2
flutter pub get

然后在 pubspec.lock 检查 source: git、AtomGit 地址和 resolved-ref,确认没有解析到 pub.dev 的同名版本。

九、构建、安装和真机验证

cd example
flutter pub get
flutter analyze
flutter build hap --debug
hdc install -r build/ohos/hap/entry-default-signed.hap
hdc shell aa start -a EntryAbility -b com.example.flutter_oh_demo

构建输出为 ✓ Built build/ohos/hap/entry-default-signed.hap.,随后 hdc 报告安装成功并启动 Ability。真机上按本文的截图顺序逐一点击按钮完成验证。

十、适配结果和当前边界

加密存储已完整接入,有两点需要在实现时注意:

  1. AssetMap 是 Map<Tag, Value>,ArkTS 禁止非标识符属性名的对象字面量,必须 new Map() 后 set()。
  2. Dart 层 canAuthenticate 与 _promptInfoForCurrentPlatform 都没有 ohos 分支:前者直接返回 unsupported(根本不调原生),后者会抛 StateError。两处都已补 Platform.isOhos。

canAuthenticate 返回 errorHwUnavailable 是真实设备状态(测试机未录入指纹/人脸),不是实现问题。

十一、FAQ

Q1:read 为什么不返回 null?

  • 现象: Asset Store 读到了之前写入的值。
  • 解决方法: 空值判断交给调用方。
  • 验证结果: 真机读回写入内容。

Q2:canAuthenticate 返回值怎么用?

  • 现象: Success 表示生物识别可用;errorHwUnavailable/errorNoBiometricEnrolled 表示设备未就绪。
  • 解决方法: 按枚举分支处理。
  • 验证结果: 真机返回 errorHwUnavailable(未录入)。

Q3:写入为什么不报错?

  • 现象: DEVICE_UNLOCKED 策略在设备解锁时允许写入。
  • 解决方法: 无需额外处理。
  • 验证结果: 写入后读回一致。

十二、总结

biometric_storage 5.0.1 已接入鸿蒙的系统加密存储:用 Asset Store Kit 存取秘密(DEVICE_UNLOCKED 访问控制),用 userIAM 查询生物识别能力。真机上完成了写入-读回往返验证。

十三、参考资料和仓库地址

欢迎加入CPF-Flutter 鸿蒙社区:https://atomgit.com/CPF-Flutter

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐