diff --git a/platform_device_id/android/build.gradle b/platform_device_id/android/build.gradle index 1ab78e3..1935340 100644 --- a/platform_device_id/android/build.gradle +++ b/platform_device_id/android/build.gradle @@ -1,15 +1,17 @@ +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" } } @@ -17,7 +19,7 @@ buildscript { rootProject.allprojects { repositories { google() - jcenter() + mavenCentral() } } @@ -25,13 +27,23 @@ 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' @@ -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' } diff --git a/platform_device_id/android/gradle.properties b/platform_device_id/android/gradle.properties index 38c8d45..94adc3a 100644 --- a/platform_device_id/android/gradle.properties +++ b/platform_device_id/android/gradle.properties @@ -1,4 +1,3 @@ org.gradle.jvmargs=-Xmx1536M -android.enableR8=true android.useAndroidX=true android.enableJetifier=true diff --git a/platform_device_id/android/gradle/wrapper/gradle-wrapper.properties b/platform_device_id/android/gradle/wrapper/gradle-wrapper.properties index 01a286e..5d6560a 100644 --- a/platform_device_id/android/gradle/wrapper/gradle-wrapper.properties +++ b/platform_device_id/android/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/platform_device_id/example/linux/flutter/generated_plugins.cmake b/platform_device_id/example/linux/flutter/generated_plugins.cmake index 4c3abd8..50ddc85 100644 --- a/platform_device_id/example/linux/flutter/generated_plugins.cmake +++ b/platform_device_id/example/linux/flutter/generated_plugins.cmake @@ -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}) @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST}) list(APPEND PLUGIN_BUNDLED_LIBRARIES $) 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) diff --git a/platform_device_id/example/windows/flutter/generated_plugin_registrant.h b/platform_device_id/example/windows/flutter/generated_plugin_registrant.h index 9846246..dc139d8 100644 --- a/platform_device_id/example/windows/flutter/generated_plugin_registrant.h +++ b/platform_device_id/example/windows/flutter/generated_plugin_registrant.h @@ -2,6 +2,8 @@ // Generated file. Do not edit. // +// clang-format off + #ifndef GENERATED_PLUGIN_REGISTRANT_ #define GENERATED_PLUGIN_REGISTRANT_ diff --git a/platform_device_id/example/windows/flutter/generated_plugins.cmake b/platform_device_id/example/windows/flutter/generated_plugins.cmake index 7a26568..839b431 100644 --- a/platform_device_id/example/windows/flutter/generated_plugins.cmake +++ b/platform_device_id/example/windows/flutter/generated_plugins.cmake @@ -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}) @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST}) list(APPEND PLUGIN_BUNDLED_LIBRARIES $) 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) diff --git a/platform_device_id/lib/platform_device_id.dart b/platform_device_id/lib/platform_device_id.dart index b650093..2eed1c6 100644 --- a/platform_device_id/lib/platform_device_id.dart +++ b/platform_device_id/lib/platform_device_id.dart @@ -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 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; } } diff --git a/platform_device_id/pubspec.yaml b/platform_device_id/pubspec.yaml index 02a55af..eae9e77 100644 --- a/platform_device_id/pubspec.yaml +++ b/platform_device_id/pubspec.yaml @@ -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: