Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions platform_device_id/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group 'com.di1shuai.platform_device_id'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.9.20'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:8.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}

namespace "com.di1shuai.platform_device_id"
compileSdk 34

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
minSdkVersion 24
}
lintOptions {
disable 'InvalidPackage'
Expand All @@ -40,4 +52,5 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
}
1 change: 0 additions & 1 deletion platform_device_id/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ list(APPEND FLUTTER_PLUGIN_LIST
platform_device_id_linux
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
)

set(PLUGIN_BUNDLED_LIBRARIES)

foreach(plugin ${FLUTTER_PLUGIN_LIST})
Expand All @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST})
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)

foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/linux plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ list(APPEND FLUTTER_PLUGIN_LIST
platform_device_id_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
)

set(PLUGIN_BUNDLED_LIBRARIES)

foreach(plugin ${FLUTTER_PLUGIN_LIST})
Expand All @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST})
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)

foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
75 changes: 70 additions & 5 deletions platform_device_id/lib/platform_device_id.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,98 @@
import 'dart:async';
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';
import 'package:device_info/device_info.dart';
import 'package:platform_device_id_platform_interface/platform_device_id_platform_interface.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter_udid/flutter_udid.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// Provides device id information.
class PlatformDeviceId {
/// Provides device and operating system information.
static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

/// Key for storing the generated device ID in SharedPreferences
static const String _deviceIdKey = 'platform_device_id_key';

/// Information derived from `android`-`androidId` or `ios`-`identifierForVendor`
/// Information derived from `android`-`flutter_udid` or `ios`-`identifierForVendor`
/// Falls back to SharedPreferences for persistence
static Future<String?> get getDeviceId async {
String? deviceId;
try {
if (kIsWeb) {
deviceId = await PlatformDeviceIdPlatform.instance.getDeviceId();
} else if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
deviceId = androidInfo.androidId;
// Use flutter_udid directly for Android instead of androidInfo.id
try {
deviceId = await FlutterUdid.udid;
} catch (e) {
print('Error getting Android UDID: $e');
deviceId = null; // Will trigger the fallback logic below
}
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
deviceId = iosInfo.identifierForVendor;
} else {
deviceId = await PlatformDeviceIdPlatform.instance.getDeviceId();
}

// If deviceId is null, empty, or consists only of whitespace, use fallback mechanisms
if (deviceId == null || deviceId.trim().isEmpty) {
// For non-Android platforms, try flutter_udid as first fallback
if (!Platform.isAndroid) {
try {
deviceId = await FlutterUdid.udid;
} catch (e) {
print('Error getting UDID: $e');
}
}

// If still empty, check SharedPreferences for a previously stored ID
if (deviceId == null || deviceId.trim().isEmpty) {
final prefs = await SharedPreferences.getInstance();
deviceId = prefs.getString(_deviceIdKey);

// If no ID in SharedPreferences, generate one using flutter_udid's consistentUdid
// and store it for future use
if (deviceId == null || deviceId.trim().isEmpty) {
try {
deviceId = await FlutterUdid.consistentUdid;
// Store the generated ID for future use
await prefs.setString(_deviceIdKey, deviceId ?? '');
} catch (e) {
print('Error getting consistent UDID: $e');
// If all else fails, return an empty string
deviceId = '';
}
}
} else {
// Store the flutter_udid value in SharedPreferences for future use
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_deviceIdKey, deviceId);
}
}
} on PlatformException {
deviceId = '';
// Try flutter_udid as fallback for platform exception
try {
deviceId = await FlutterUdid.udid;
} catch (e) {
// If flutter_udid also fails, try to get from SharedPreferences
final prefs = await SharedPreferences.getInstance();
deviceId = prefs.getString(_deviceIdKey);

// If still no ID, generate one
if (deviceId == null || deviceId.trim().isEmpty) {
try {
deviceId = await FlutterUdid.consistentUdid;
await prefs.setString(_deviceIdKey, deviceId ?? '');
} catch (e) {
deviceId = '';
}
}
}
}

return deviceId;
}
}
4 changes: 3 additions & 1 deletion platform_device_id/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ dependencies:
platform_device_id_linux: ^1.0.0
platform_device_id_web: ^1.0.0
platform_device_id_windows: ^1.0.0
device_info: ^2.0.0
device_info_plus: ^10.1.0
flutter_udid: ^2.0.1
shared_preferences: ^2.2.2

dev_dependencies:
flutter_test:
Expand Down