device_id_manager | Flutter 包 原文标题:device_id_manager | Flutter package
内容概要总结
这是 pub.dev 上 device_id_manager(v1.0.0)开源 Flutter 插件的主页。该插件用于生成并持久化唯一设备 ID,即使在 App 重装后也能保留,且无需任何权限。工作原理:iOS 在 Keychain 中查找或生成 UUID v4 并存入 Keychain(AES-256),Keychain 数据跨重装持久;Android 通过 MediaDrm API 获取硬件设备 ID,与 App 包名组合后应用 SHA-256 哈希,得到每 App 唯一的 ID,当 MediaDrm 不可用(已 root 设备、模拟器、自定义 ROM)时回退到 UUID v4。插件还通过 logIn()/logOut() 支持“已识别用户”,并提供同步 getter。文档列出平台对比表、安装方式、iOS(需在 Info.plist 添加 NSFaceIDUsageDescription)与 Android(最低 SDK 18)设置、基础用法与登录/登出示例、日志开关、完整 API 参考,以及局限(MediaDrm 仅 API ≥ 18、部分自定义/root ROM 不可靠、恢复出厂会移除 Keychain ID 等)。依赖 crypto、flutter_secure_storage、package_info_plus、persistent_device_id、uuid,许可证 MIT。
翻译内容
原文内容(English)
device_id_manager 1.0.0
Metadata
跨平台的 Flutter 持久设备 ID 管理器。iOS 使用 Keychain,Android 使用 MediaDrm + SHA-256 哈希。可在 App 重装后存活。
📱 device_id_manager
一个跨平台的 Flutter 插件,用于生成并持久化唯一设备 ID——即使在重装 App 之后。为需要可靠、持久设备标识符且零权限的应用而构建。
✨ 特性
- 🔒 在 iOS 和 Android 上均可跨 App 重装持久
- 🏷️ Android 上为每 App 唯一的 ID(同一设备上的不同 App 得到不同的 ID)
- 🚫 无需任何权限
- 👤 通过 logIn() / logOut() 支持已识别用户
- ⚡ 初始化后可同步访问——getter 无需 await
- 🛡️ 安全存储——iOS Keychain(AES-256)、Android MediaDrm(硬件支撑)
- 💥 故障安全——在 initialize() 之前访问会抛出 StateError
🧠 工作原理
🍏 iOS
- 在 Keychain 中查找已存在的 ID(user_device_id 键)
- 如果找不到,生成一个 UUID v4 并存入 Keychain
- Keychain 数据在 App 重装后持久保留
🤖 Android
- 通过 MediaDrm API 获取硬件设备 ID
- 与 App 包名组合:"$mediaDrmId:$packageName"
- 应用 SHA-256 哈希 → 每 App 唯一的 ID
- 如果 MediaDrm 不可用(已 root 设备、模拟器、自定义 ROM),回退到 UUID v4
📊 平台对比
| 🍏 iOS | 🤖 Android | |
|---|---|---|
| 生成 | UUID v4 | MediaDrm + SHA-256 |
| 存储 | Keychain | 硬件(MediaDrm API) |
| 重装后存活 | ✅ 是 | ✅ 是 |
| 每 App 唯一 | ✅ 是(以 bundle ID 为作用域) | ✅ 是(包名哈希) |
| 权限 | 🚫 无 | 🚫 无 |
📦 安装
添加到你的 pubspec.yaml:
dependencies:
device_id_manager: ^1.0.0
flutter pub get
🍏 iOS 设置
如果尚未存在,把以下内容添加到你的 ios/Runner/Info.plist(flutter_secure_storage 需要):
<key>NSFaceIDUsageDescription</key>
<string>We use Face ID to protect your data</string>
🤖 Android 设置
需要最低 SDK 18。在 android/app/build.gradle 中:
android {
defaultConfig {
minSdkVersion 18
}
}
🛠️ 用法
基本
import 'package:device_id_manager/device_id_manager.dart';
// Initialize once at app startup
await DeviceIdManager.initialize();
// Access device ID (synchronous, non-null)
String id = DeviceIdManager.deviceId;
print('Device ID: $id');
👤 已识别用户(登录 / 登出)
// Log in with a custom user ID
await DeviceIdManager.logIn('user_12345');
print(DeviceIdManager.isAnonymous); // false
print(DeviceIdManager.appUserId); // user_12345
print(DeviceIdManager.customUserId); // user_12345
// Log out — reverts to anonymous device ID
await DeviceIdManager.logOut();
print(DeviceIdManager.isAnonymous); // true
print(DeviceIdManager.appUserId); // null
print(DeviceIdManager.customUserId); // <device_id>
🔇 日志
// Disable logs
DeviceIdLogger.setEnabled(false);
// Enable logs (default)
DeviceIdLogger.setEnabled(true);
📚 API 参考
DeviceIdManager
| 属性 / 方法 | 类型 | 说明 |
|---|---|---|
| initialize() | Future<void> | 必须在其他任何操作之前调用 |
| deviceId | String | 持久设备 ID |
| isAnonymous | bool | 如果未设置自定义用户 ID 则为 true |
| customUserId | String | 若已登录则为 App 用户 ID,否则为设备 ID |
| appUserId | String? | 自定义用户 ID,匿名为 null |
| isInitialized | bool | initialize() 是否已被调用 |
| logIn(String) | Future<void> | 设置一个已识别用户 ID |
| logOut() | Future<void> | 清除用户 ID,恢复为匿名 |
| clearUserId() | Future<void> | 清除所有已存储的 ID 并重置状态 |
| hasUserId() | Future<bool> | 检查设备 ID 是否存在 |
⚠️ 重要:在 initialize() 之前访问任何 getter 都会抛出 StateError。
🚧 局限
- MediaDrm 仅在 Android API ≥ 18 上可用
- 在某些自定义或已 root 的 ROM 上,MediaDrm 可能不可靠
- 恢复出厂设置会移除 iOS Keychain ID
- 在 iOS 上,如果 iCloud Keychain 被禁用,或设备在无备份的情况下被恢复,基于 Keychain 的 ID 可能会重置
🔍 示例
克隆仓库并运行示例 App:
cd example
flutter run
📄 许可证
← 元数据
文档
发布者
每周下载量
元数据
跨平台的 Flutter 持久设备 ID 管理器。iOS 使用 Keychain,Android 使用 MediaDrm + SHA-256 哈希。可在 App 重装后存活。
主题
#device-id #keychain #persistent #identity
许可证
MIT(license)
依赖
crypto、flutter、flutter_secure_storage、package_info_plus、persistent_device_id、uuid
更多
device_id_manager 1.0.0
device_id_manager: ^1.0.0 copied to clipboard
Metadata
Cross-platform persistent device ID manager for Flutter. iOS uses Keychain, Android uses MediaDrm + SHA-256 hash. Survives app reinstalls.
📱 device_id_manager
A cross-platform Flutter plugin that generates and persists unique device IDs — even after reinstalling the app. Built for apps that need a reliable, persistent device identifier with zero permissions.
✨ Features
- 🔒 Persistent across app reinstalls on both iOS and Android
- 🏷️ Per-app unique IDs on Android (different apps on the same device get different IDs)
- 🚫 No permissions required
- 👤 Identified user support via logIn() / logOut()
- ⚡ Synchronous access after initialization — no await needed for getters
- 🛡️ Secure storage — iOS Keychain (AES-256), Android MediaDrm (hardware-backed)
- 💥 Fail-safe — throws StateError if accessed before initialize()
🧠 How It Works
🍏 iOS
- Checks Keychain for an existing ID (user_device_id key)
- If not found, generates a UUID v4 and stores it in Keychain
- Keychain data persists across app reinstalls
🤖 Android
- Gets hardware device ID via MediaDrm API
- Combines with app package name: "$mediaDrmId:$packageName"
- Applies SHA-256 hash → per-app unique ID
- Falls back to UUID v4 if MediaDrm is unavailable (rooted devices, emulators, custom ROMs)
📊 Platform Comparison
| 🍏 iOS | 🤖 Android |
|---|---|
| Generation | UUID v4 |
| MediaDrm + SHA-256 | Storage |
| Keychain | Hardware (MediaDrm API) |
| Survives reinstall | ✅ Yes |
| ✅ Yes | Per-app unique |
| ✅ Yes (bundle ID scoped) | ✅ Yes (package name hashed) |
| Permissions | 🚫 None |
| 🚫 None |
📦 Installation
Add to your pubspec.yaml:
dependencies:
device_id_manager: ^1.0.0
[flutter](https://api.flutter.dev/) pub get
🍏 iOS Setup
Add the following to your ios/Runner/Info.plist if not already present (required by flutter_secure_storage):
<key>NSFaceIDUsageDescription</key>
<string>We use Face ID to protect your data</string>
🤖 Android Setup
Minimum SDK 18 is required. In android/app/build.gradle:
android {
defaultConfig {
minSdkVersion 18
}
}
🛠️ Usage
Basic
import 'package:device_id_manager/device_id_manager.dart';
// Initialize once at app startup
await DeviceIdManager.initialize();
// Access device ID (synchronous, non-null)
String id = DeviceIdManager.deviceId;
print('Device ID: $id');
👤 Identified Users (Login / Logout)
// Log in with a custom user ID
await DeviceIdManager.logIn('user_12345');
print(DeviceIdManager.isAnonymous); // false
print(DeviceIdManager.appUserId); // user_12345
print(DeviceIdManager.customUserId); // user_12345
// Log out — reverts to anonymous device ID
await DeviceIdManager.logOut();
print(DeviceIdManager.isAnonymous); // true
print(DeviceIdManager.appUserId); // null
print(DeviceIdManager.customUserId); // <device_id>
🔇 Logging
// Disable logs
DeviceIdLogger.setEnabled(false);
// Enable logs (default)
DeviceIdLogger.setEnabled(true);
📚 API Reference
DeviceIdManager
| Property / Method | Type | Description |
|---|---|---|
| initialize() | Future<void> | Must be called before anything else |
| deviceId | String | Persistent device ID |
| isAnonymous | bool | true if no custom user ID is set |
| customUserId | String | App user ID if logged in, otherwise device ID |
| appUserId | String? | Custom user ID, null if anonymous |
| isInitialized | bool | Whether initialize() has been called |
| logIn(String) | Future<void> | Set an identified user ID |
| logOut() | Future<void> | Clear user ID, revert to anonymous |
| clearUserId() | Future<void> | Clear all stored IDs and reset state |
| hasUserId() | Future<bool> | Check if a device ID exists |
⚠️ Important: Accessing any getter before initialize() throws a StateError.
🚧 Limitations
- MediaDrm only available on Android API ≥ 18
- On some custom or rooted ROMs, MediaDrm may be unreliable
- Factory reset will remove the iOS Keychain ID
- On iOS, Keychain-based ID may reset if iCloud Keychain is disabled or device is restored without backup
🔍 Example
Clone the repository and run the example app:
cd example
flutter run
📄 License
← Metadata
Documentation
Publisher
Weekly Downloads
Metadata
Cross-platform persistent device ID manager for Flutter. iOS uses Keychain, Android uses MediaDrm + SHA-256 hash. Survives app reinstalls.
Repository (GitHub)
View/report issues
Topics
#device-id #keychain #persistent #identity
License
MIT (license)
Dependencies
crypto, flutter, flutter_secure_storage, package_info_plus, persistent_device_id, uuid