Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 unique_device_identifier
Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 unique_device_identifier
获取设备匿名标识符,用于在同一设备上区分不同安装实例。
本文以 unique_device_identifier 2.1.0 为例,记录我如何保留原有 Dart API、补齐 Flutter OH 插件入口、在鸿蒙端接入真实系统能力,并在 HarmonyOS API 26 真机上完成构建、安装、启动与调用验证。
适配仓库: unique_device_identifier
适配分支: feat/ohos_unique_device_identifier_2.1.0
适配 TAG: 2.1.0-ohos.1
一、先看最终效果

图 1:HarmonyOS API 26 真机启动示例页
| 验证点 | 实测结果 |
|---|---|
| Flutter OH 3.47 工程构建 | 通过,产出 entry-default-signed.hap |
| 安装、启动(真机) | 通过 |
| 读取标识 | 空串(OAID 未授权返回全零,udid 为空) |
二、这个库原来是怎么工作的
上游 unique_device_identifier 通过平台通道与原生侧通信,通道定义在 Dart 源码里,原生侧只需要注册同名通道并实现方法分发,Dart 代码一行都不用改。
MethodChannel('unique_device_identifier')
-> get_unique_identifier
三、实际环境
Flutter 鸿蒙环境搭建不在本文重复展开,直接参考 Flutter OH 环境搭建指南。
| 组件 | 版本 |
|---|---|
| Flutter OH | 3.47.4-ohos-1.0.4 |
| Dart | 3.13.3 |
| DevEco Studio | 26.0.0 Release |
| HarmonyOS SDK | API 26 |
| 测试设备 | 华为畅享 90 Pro Max(CHZ-AL00),HarmonyOS 7.0.0.109(API 26) |
| 目标库 | unique_device_identifier 2.1.0 |
flutter --version
hdc list targets
这里要特别注意 Flutter 版本:本机同时装了 3.41 和 3.47.4-ohos,3.41 会因为 SDK 约束解析失败,构建时必须显式把 3.47.4-ohos 放到 PATH 前面。
四、确认上游版本并同步到 AtomGit
上游包地址是 https://github.com/jonghoon023/unique_device_identifier,版本 2.1.0 使用 MIT License。适配前我用 AtomGit API(GET /api/v5/repos/<org>/<lib>)在 oh-flutter、hxa-flutter、CPF-Flutter 三个组织做了排重,未发现同名 OHOS 实现。
git clone https://atomgit.com/oh-flutter/unique_device_identifier.git
cd unique_device_identifier
git switch feat/ohos_unique_device_identifier_2.1.0
git log --oneline -5
五、创建适配分支并检查工程结构
git switch -c feat/ohos_unique_device_identifier_2.1.0
关键目录如下:
unique_device_identifier/
├── lib/ # 原有 Dart API 和通道
├── ohos/
│ ├── index.ets # OHOS 插件导出入口
│ └── src/main/ets/components/plugin/
│ └── UniqueDeviceIdentifierPlugin.ets
├── example/lib/main.dart # 真机验证 Demo
├── example/ohos/ # Flutter OH 宿主工程
├── pubspec.yaml # 声明 ohos 平台
└── README.OpenHarmony_CN.md # 鸿蒙适配说明
flutter:
plugin:
platforms:
ohos:
pluginClass: UniqueDeviceIdentifierPlugin
pluginClass 必须和 ohos/index.ets 导出的类名一致。这一步是本批次最容易踩的坑:pubspec.yaml 里漏掉 ohos 声明时,Flutter 生成的 GeneratedPluginRegistrant.ets 是个空 try 块,Dart 侧调用直接返回 null,构建期没有任何报错,运行时也不抛 MissingPluginException,只是所有返回值都是空的。我第一次跑 is_lock_screen2 就卡在这里,查了半天日志才发现注册表是空的。
六、适配过程:注册通道并对齐语义
通道名必须与上游 Dart 源码逐字一致。本库的映射如下:
| 方法 | OpenHarmony 实现 | 说明 |
|---|---|---|
get_unique_identifier | identifier.getOAID(),未授权时回退 deviceInfo.udid | partially |
核心代码:
if (call.method !== 'get_unique_identifier') {
result.notImplemented();
return;
}
const ZERO_OAID: string = '00000000-0000-0000-0000-000000000000';
identifier.getOAID()
.then((oaid: string): void => {
if (oaid && oaid.length > 0 && oaid !== ZERO_OAID) {
result.success(oaid);
} else {
result.success(deviceInfo.udid ?? '');
}
})
.catch((): void => {
result.success(deviceInfo.udid ?? '');
});
七、构建配置与签名
示例工程的目标 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。签名 profile 里绑定的 bundleName 必须与它一致,否则 SignHap 直接报 00303074 Configuration Error。证书、p12、p7b 和密码不提交到仓库。
另外两个和签名、SDK 相关的坑:
build-profile.json5里签名材料写${HOME}不会展开,必须是绝对路径,否则报00303107 Invalid storeFile value。DEVECO_SDK_HOME要指向Contents/sdk(含版本目录的上一级)。指到sdk/default报00303312 Cannot find the corresponding SDK version,完全不设则报00303217。
八、通过 AtomGit 引入适配版
在业务工程中使用时锁定 TAG:
dependencies:
unique_device_identifier:
git:
url: https://atomgit.com/oh-flutter/unique_device_identifier.git
ref: 2.1.0-ohos.1
flutter pub get
然后在 pubspec.lock 里确认 source: git、AtomGit 地址和 resolved-ref,避免解析到 pub.dev 的同名版本。
九、构建、安装和真机验证
cd example
flutter pub get
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 报告安装成功。所有示例共用同一个 bundleName,重装后要先 aa force-stop 再 aa start,否则系统只是把旧任务拉到前台,看到的是上一次安装的界面——这个现象在批量验证时骗过我一次。
取实测值时用 uitest 定位控件并点击,而不是靠肉眼判断:
hdc shell "uitest dumpLayout -p /data/local/tmp/l.json"
hdc file recv /data/local/tmp/l.json /tmp/l.json
hdc shell "uitest uiInput click <x> <y>"
hdc shell "uitest screenCap -p /data/local/tmp/s.png"
十、适配结果和当前边界
identifier.getOAID()需要ohos.permission.APP_TRACKING_CONSENT(user_grant)。用户未授权时,系统不报错,而是返回全零 UUID00000000-0000-0000-0000-000000000000,本实现显式识别这个哨兵值并回退到deviceInfo.udid。- 实测设备上即使声明了权限,
getOAID()仍返回全零、deviceInfo.udid也为空,最终返回空串。这是鸿蒙对匿名设备标识的隐私策略,不使用「全零串加前缀」之类的方式伪造出一个看似有效的标识,如实返回空值由调用方处理。
十一、FAQ
Q1:为什么返回空串?
- 原因:
getOAID()需要APP_TRACKING_CONSENT(user_grant)。用户未授权时系统不报错,而是返回全零 UUID00000000-0000-0000-0000-000000000000。实现显式识别这个哨兵值并回退到deviceInfo.udid,实测该机udid也为空,最终返回空串。 - 验证结果: 示例页「Unique Identifier」一栏为空,这是实测确认的隐私边界。
Q2:为什么不能自己生成一个 ID 顶上?
- 原因: 可以随机生成并持久化,但那就不是「设备标识」了,卸载重装就变,语义与原库不符。本文选择如实返回空值,由调用方决定降级策略。
Q3:为什么在 module.json5 里要写 reason?
- 原因:
APP_TRACKING_CONSENT是 user_grant 权限,鸿蒙要求必须声明reason与usedScene,否则构建报The reason attribute is mandatory for user_grant permissions。配套还要在resources/base/element/string.json里加对应字符串。
十二、总结
unique_device_identifier 2.1.0 在鸿蒙上已经接入真实系统能力,Dart 侧 API 与上游保持一致。本文的验证结果全部来自 HarmonyOS API 26 真机,没有占位返回值;平台本身不具备的能力在正文和 README 中都标注为边界。
十三、参考资料和仓库地址
- 适配仓库主页
- 适配分支
- 适配 TAG
- ArkTS 通道实现
- 鸿蒙适配说明
- pub.dev 包页面
- pub.dev 2.1.0 版本页
- 上游仓库
- Flutter 官方平台通道文档
- Flutter OH 鸿蒙 SDK 仓库
- HarmonyOS 开发者官方文档
欢迎加入CPF-Flutter 鸿蒙社区:https://atomgit.com/CPF-Flutter
更多推荐

所有评论(0)