From 7f77422a1f01f6a9f4a11fd17738059b1f655c97 Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Wed, 10 Jan 2024 19:41:09 +1100 Subject: [PATCH 1/4] revert: remove gson for now. Other error arise so people with over big big library can't use it at the moment. Since OOM issue. Signed-off-by: KaiserBh --- app/proguard-rules.pro | 5 +---- .../sync/service/GoogleDriveSyncService.kt | 19 +++++++------------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 31e2c39fd..a4eb2c039 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -80,7 +80,4 @@ -keep class com.google.api.services.** { *; } # Google OAuth --keep class com.google.api.client.** { *; } - -# Keep backup models, otherwise gson fails on release builds. --keep class eu.kanade.tachiyomi.data.backup.models.** { *; } \ No newline at end of file +-keep class com.google.api.client.** { *; } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index 145375e68..94700cbab 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -18,11 +18,10 @@ import com.google.api.client.json.jackson2.JacksonFactory import com.google.api.services.drive.Drive import com.google.api.services.drive.DriveScopes import com.google.api.services.drive.model.File -import com.google.gson.Gson -import com.google.gson.stream.JsonWriter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.withContext +import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import logcat.LogPriority import logcat.logcat @@ -35,7 +34,6 @@ import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.IOException import java.io.InputStreamReader -import java.io.OutputStreamWriter import java.time.Instant import java.util.zip.GZIPInputStream import java.util.zip.GZIPOutputStream @@ -139,12 +137,11 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync return withContext(Dispatchers.IO) { try { val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray())) - val gson = Gson() - val syncData = gson.fromJson(gzipInputStream.reader(Charsets.UTF_8), SyncData::class.java) - logcat(LogPriority.DEBUG) { "JSON deserialized successfully" } + val syncData = json.decodeFromString(gzipInputStream.reader(Charsets.UTF_8).readText()) + logcat(LogPriority.DEBUG) { "JSON deserialized successfully with kotlinx.serialization" } syncData } catch (e: Exception) { - logcat(LogPriority.ERROR) { "Failed to convert json to sync data: ${e.message}" } + logcat(LogPriority.ERROR) { "Failed to convert json to sync data with kotlinx.serialization: ${e.message}" } throw Exception(e.message) } } @@ -159,12 +156,10 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync withContext(Dispatchers.IO) { val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream) - val jsonWriter = JsonWriter(OutputStreamWriter(gzipOutputStream, Charsets.UTF_8)) - val gson = Gson().newBuilder().serializeNulls().create() - - jsonWriter.use { jWriter -> - gson.toJson(syncData, SyncData::class.java, jWriter) + gzipOutputStream.writer(Charsets.UTF_8).use { writer -> + writer.write(json.encodeToString(syncData)) } + logcat(LogPriority.DEBUG) { "JSON serialized successfully" } } val byteArrayContent = ByteArrayContent("application/octet-stream", byteArrayOutputStream.toByteArray()) From 542ad22c93f07ad1478e0c919b94ed0298558c55 Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Wed, 10 Jan 2024 19:41:34 +1100 Subject: [PATCH 2/4] chore: lint Signed-off-by: KaiserBh --- .../tachiyomi/data/sync/service/GoogleDriveSyncService.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index 94700cbab..7843d0a1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -141,7 +141,9 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync logcat(LogPriority.DEBUG) { "JSON deserialized successfully with kotlinx.serialization" } syncData } catch (e: Exception) { - logcat(LogPriority.ERROR) { "Failed to convert json to sync data with kotlinx.serialization: ${e.message}" } + logcat( + LogPriority.ERROR, + ) { "Failed to convert json to sync data with kotlinx.serialization: ${e.message}" } throw Exception(e.message) } } From 205d34358c9c7314fe0cbebcdca873e80868d125 Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Wed, 10 Jan 2024 19:41:44 +1100 Subject: [PATCH 3/4] chore: lint Signed-off-by: KaiserBh --- .../tachiyomi/data/sync/service/GoogleDriveSyncService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index 7843d0a1b..5e6fba545 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -138,7 +138,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync try { val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray())) val syncData = json.decodeFromString(gzipInputStream.reader(Charsets.UTF_8).readText()) - logcat(LogPriority.DEBUG) { "JSON deserialized successfully with kotlinx.serialization" } + logcat(LogPriority.DEBUG) { "JSON deserialized successfully" } syncData } catch (e: Exception) { logcat( From f02a9de4db3265d561105efcc89c2ad6915fd498 Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Wed, 10 Jan 2024 19:59:01 +1100 Subject: [PATCH 4/4] fix: decoding and encoding. Signed-off-by: KaiserBh --- .../data/sync/service/GoogleDriveSyncService.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index 5e6fba545..48f10d7d4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -137,7 +137,8 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync return withContext(Dispatchers.IO) { try { val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray())) - val syncData = json.decodeFromString(gzipInputStream.reader(Charsets.UTF_8).readText()) + val jsonString = gzipInputStream.bufferedReader(Charsets.UTF_8).use { it.readText() } + val syncData = json.decodeFromString(SyncData.serializer(), jsonString) logcat(LogPriority.DEBUG) { "JSON deserialized successfully" } syncData } catch (e: Exception) { @@ -157,10 +158,10 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync val byteArrayOutputStream = ByteArrayOutputStream() withContext(Dispatchers.IO) { + val jsonData = json.encodeToString(syncData) val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream) - gzipOutputStream.writer(Charsets.UTF_8).use { writer -> - writer.write(json.encodeToString(syncData)) - } + gzipOutputStream.write(jsonData.toByteArray(Charsets.UTF_8)) + gzipOutputStream.close() logcat(LogPriority.DEBUG) { "JSON serialized successfully" } }