device_id_manager 1.0.0
device_id_manager: ^1.0.0 copied to clipboard
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
awaitneeded for getters - π‘οΈ Secure storage β iOS Keychain (AES-256), Android MediaDrm (hardware-backed)
- π₯ Fail-safe β throws
StateErrorif accessed beforeinitialize()
π§ How It Works #
π iOS #
- Checks Keychain for an existing ID (
user_device_idkey) - 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
Then run:
flutter 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 aStateError.
π§ 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 #
MIT License. Β© 2025