Skip to content

Commit d02075f

Browse files
#Bugfix, proguard obfuscation of gson might be the cause for aimodels not getting parsed.
1 parent dbac48e commit d02075f

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

core/ai/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id 'org.jetbrains.kotlin.plugin.compose'
55
id 'com.google.devtools.ksp'
66
id 'com.google.dagger.hilt.android'
7+
id 'org.jetbrains.kotlin.plugin.serialization'
78
}
89

910
android {
@@ -65,7 +66,7 @@ dependencies {
6566

6667
// AI & Network
6768
implementation("com.squareup.okhttp3:okhttp:5.3.2")
68-
implementation("com.google.code.gson:gson:2.13.2")
69+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3"
6970

7071
implementation("com.google.ai.client.generativeai:generativeai:0.9.0")
7172
implementation("com.google.mlkit:common:18.11.0")

core/ai/src/main/java/com/mintanable/notethepad/feature_ai/data/repository/AiModelRepositoryImpl.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package com.mintanable.notethepad.feature_ai.data.repository
22

33
import android.content.Context
44
import android.util.Log
5-
import com.google.gson.Gson
65
import com.mintanable.notethepad.core.common.DispatcherProvider
76
import com.mintanable.notethepad.core.model.ai.AiModel
87
import com.mintanable.notethepad.core.model.ai.AiModelCatalog
98
import com.mintanable.notethepad.feature_ai.BuildConfig
9+
import kotlinx.serialization.Serializable
10+
import kotlinx.serialization.json.Json
1011
import com.mintanable.notethepad.feature_ai.domain.repository.AiModelRepository
1112
import dagger.hilt.android.qualifiers.ApplicationContext
1213
import kotlinx.coroutines.flow.Flow
@@ -36,7 +37,10 @@ class AiModelRepositoryImpl @Inject constructor(
3637
.readTimeout(30, TimeUnit.SECONDS)
3738
.writeTimeout(30, TimeUnit.SECONDS)
3839
.build()
39-
private val gson = Gson()
40+
private val json = Json {
41+
ignoreUnknownKeys = true
42+
coerceInputValues = true
43+
}
4044

4145
override fun getModels(): Flow<List<AiModel>> = flow {
4246
if (_cachedModels.value.isNotEmpty()) {
@@ -121,8 +125,8 @@ class AiModelRepositoryImpl @Inject constructor(
121125
val gistResponse = response.body.string()
122126

123127
if (!gistResponse.isNullOrEmpty()) {
124-
val wrapper = gson.fromJson(gistResponse, GistWrapper::class.java)
125-
val content = wrapper?.files?.get(MODEL_CATALOG_FILENAME)?.content
128+
val wrapper = json.decodeFromString<GistWrapper>(gistResponse)
129+
val content = wrapper.files?.get(MODEL_CATALOG_FILENAME)?.content
126130

127131
if (content != null) {
128132
val internalFile = File(context.filesDir, MODEL_CATALOG_FILENAME)
@@ -141,7 +145,7 @@ class AiModelRepositoryImpl @Inject constructor(
141145
return try {
142146
val file = File(context.filesDir, MODEL_CATALOG_FILENAME)
143147
if (file.exists()) {
144-
file.readText().let { gson.fromJson(it, AiModelCatalog::class.java) }
148+
file.readText().let { json.decodeFromString<AiModelCatalog>(it) }
145149
} else null
146150
} catch (e: Exception) {
147151
Log.e("kptest", "Failed to parse internal catalog", e)
@@ -154,18 +158,20 @@ class AiModelRepositoryImpl @Inject constructor(
154158
context.assets.open(MODEL_CATALOG_FILENAME)
155159
.bufferedReader()
156160
.use { it.readText() }
157-
.let { gson.fromJson(it, AiModelCatalog::class.java) }
161+
.let { json.decodeFromString<AiModelCatalog>(it) }
158162
} catch (e: Exception) {
159163
Log.e("kptest", "Failed to parse assets catalog", e)
160164
null
161165
}
162166
}
163167
}
164168

169+
@Serializable
165170
data class GistWrapper(
166171
val files: Map<String, GistFile>? = null
167172
)
168173

174+
@Serializable
169175
data class GistFile(
170176
val content: String? = null
171177
)

0 commit comments

Comments
 (0)