Problem with com.google.gson after upgrading gradle?

61 Views Asked by At

After upgrading Gradle to 8.2.1, Kotlin 1.9.21 and Java 17 I have an error like:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to cj.a
                                                                                                        

This is my BaseModel:

import com.google.gson.Gson

abstract class BaseModel {

    inline fun <reified T: BaseModel> parseToJsonString(): String? {
        return try {
            parseToString<T>()
        } catch (e: Exception) {
            null
        }
    }

    inline fun <reified T: BaseModel> parseToString(): String {
        return Gson().toJson(this as T)
    }

    companion object {

        inline fun <reified T: BaseModel> fromJsonString(json: String): T? {
            return try {
                decodeFromString(json)
            } catch (e: Exception) {
                null
            }
        }

        inline fun <reified T: BaseModel> decodeFromString(json: String): T {
            return Gson().fromJson(json, T::class.java)
        }

    }
}

I also receive that exception

Exception while processing periodic Task TaskWrapper{task=class y6.g, processed=false}
                                                                                                    java.lang.NoSuchMethodException: y6.g.<init> [class android.content.Context, class u6.a]

Why? I have read some solutions to add in pro guard

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
#-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

-keep class com.google.** { *; }

-keep class sun.misc.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

-keep class * implements com.google.gson.*

##---------------End: proguard configuration for Gson  ----------

but it doesn't work. Thanks for any help and suggesstions.

0

There are 0 best solutions below