From 351d57b786821aedd0b497451fc06ae9ac76052d Mon Sep 17 00:00:00 2001 From: Gautier de Lataillade Date: Wed, 28 Aug 2024 11:05:30 +0200 Subject: [PATCH] Fix android native GSON parsing error --- android/build.gradle | 6 +++ android/proguard-rules.pro | 24 ++++++++++++ .../alarm/models/AlarmSettings.kt | 38 ++++++++----------- 3 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 android/proguard-rules.pro diff --git a/android/build.gradle b/android/build.gradle index d6c45a70..fc27b0e2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -45,6 +45,12 @@ android { defaultConfig { minSdkVersion 19 } + + buildTypes { + release { + consumerProguardFiles 'proguard-rules.pro' + } + } } dependencies { diff --git a/android/proguard-rules.pro b/android/proguard-rules.pro new file mode 100644 index 00000000..43cd48db --- /dev/null +++ b/android/proguard-rules.pro @@ -0,0 +1,24 @@ +# Keep all classes in your plugin's package +-keep class com.gdelataillade.alarm.** { *; } + +# Keep all classes related to Gson and prevent them from being obfuscated +-keep class com.google.gson.** { *; } +-keep class sun.misc.Unsafe { *; } +-keepattributes Signature +-keepattributes *Annotation* + +# Prevent stripping of methods/fields annotated with specific annotations, if needed. +-keepclassmembers class * { + @com.google.gson.annotations.SerializedName ; +} + +# Preserve classes that might be used in reflection or through indirect means +-keepclassmembers class **.R$* { + ; +} + +# Avoid stripping enums, if your plugin uses them +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} \ No newline at end of file diff --git a/android/src/main/kotlin/com/gdelataillade/alarm/models/AlarmSettings.kt b/android/src/main/kotlin/com/gdelataillade/alarm/models/AlarmSettings.kt index c678699b..86417ddc 100644 --- a/android/src/main/kotlin/com/gdelataillade/alarm/models/AlarmSettings.kt +++ b/android/src/main/kotlin/com/gdelataillade/alarm/models/AlarmSettings.kt @@ -21,43 +21,35 @@ data class AlarmSettings( ) { companion object { fun fromJson(json: Map): AlarmSettings? { - try { - val modifiedJson = json.toMutableMap() + return try { + val gson = Gson() // Convert dateTime from microseconds to Date + val modifiedJson = json.toMutableMap() val dateTimeMicroseconds = modifiedJson["dateTime"] as? Long - if (dateTimeMicroseconds == null) { + if (dateTimeMicroseconds != null) { + val dateTimeMilliseconds = dateTimeMicroseconds / 1000 + modifiedJson["dateTime"] = Date(dateTimeMilliseconds) + } else { Log.e("AlarmSettings", "dateTime is missing or not a Long") return null } - val dateTimeMilliseconds = dateTimeMicroseconds / 1000 - val date = Date(dateTimeMilliseconds.toLong()) - modifiedJson["dateTime"] = date - - // Convert notificationActionSettings from Map to NotificationActionSettings object + + // Deserialize NotificationActionSettings val notificationActionSettingsMap = modifiedJson["notificationActionSettings"] as? Map - if (notificationActionSettingsMap == null) { + if (notificationActionSettingsMap != null) { + modifiedJson["notificationActionSettings"] = NotificationActionSettings.fromJson(notificationActionSettingsMap) + } else { Log.e("AlarmSettings", "notificationActionSettings is missing or not a Map") return null } - val notificationActionSettings = NotificationActionSettings.fromJson(notificationActionSettingsMap) - if (notificationActionSettings == null) { - Log.e("AlarmSettings", "Failed to parse notificationActionSettings") - return null - } - modifiedJson["notificationActionSettings"] = notificationActionSettings - + // Convert the modified map to JSON string and deserialize it to an AlarmSettings object - val gson = Gson() val jsonString = gson.toJson(modifiedJson) - val result = gson.fromJson(jsonString, AlarmSettings::class.java) - if (result == null) { - Log.e("AlarmSettings", "Failed to deserialize JSON to AlarmSettings") - } - return result + gson.fromJson(jsonString, AlarmSettings::class.java) } catch (e: Exception) { Log.e("AlarmSettings", "Error parsing JSON to AlarmSettings: ${e.message}") - return null + null } } }