Reputation: 49
I'm getting an error:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Profile:
Below is the method where we are getting the exception, and we have tried implementing ProGuard rules for Gson, but still the production app is crashing. The crash we have is on a for loop
Caused by: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.xxx.Profile
at com..CDManagmentHelper.getProfileLisWOHomeUnmanaged(SourceFile:1901)
Below is the class where we use the Profile in the for loop
@NonNull
public static List<Profile> getProfileLisWOHomeUnmanaged(@NonNull RouterStatusModel routerStatusModel) {
List<Profile> filteredPCProfileList = new ArrayList<>();
for (Profile profile : routerStatusModel.getPcProfileList()) {
Log.log(CLASS_NAME, "getProfileLisWOHomeUnmanaged--> profile id: " + profile.getId());
if (!profile.getId().contains(PROFILE_TYPE_HOME) && !profile.getId().contains(PROFILE_TYPE_UNMANAGED)) {
filteredPCProfileList.add(profile);
}
}
//Here we are sorting filtered profile list alphabetically
Collections.sort(filteredPCProfileList, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
Log.log(CLASS_NAME, " getProfileLisWOHomeUnmanaged returning data : :" + filteredPCProfileList.size());
return filteredPCProfileList;
}
Here is Profile class:
public class Profile implements Serializable {
@SerializedName("id")
private String id = null;
@SerializedName("name")
private String name = null;
...
}
Here is RouterStatusModel which is used in for loop
public class RouterStatusModel {
private List<Profile> pcProfileList = new ArrayList<>();
@NonNull
public List<Profile> getPcProfileList() {
return pcProfileList;
}
}
Here are the ProGuard rules added for Gson
# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
-keep class com.google.gson.reflect.TypeToken
-keep class * extends com.google.gson.reflect.TypeToken
-keep public class * implements java.lang.reflect.Type
# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.gson.examples.android.model.** { <fields>; }
##---------------End: proguard configuration for Gson ----------
Upvotes: 0
Views: 45