GeorgeYang'Blog

my technology blog


Gson StackOverflowError

Gson Types.canonicalize StackOverflowError com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:537)

如果你的项目使用RealmObject,或者使用类似我出错的这段代码:

 public class MyFriend {
     public String  userName,userIcon,userId;
     public AVIMConversation avimConversation;
 }

将string转成实体的时候出现StackOverflowError错误,那么你可以参考github上的这段代码

使用GsonBuilder跳过导致死循环的field

 GsonBuilder gsonBuilder = new GsonBuilder()
         .setExclusionStrategies(new ExclusionStrategy() {
             @Override
             public boolean shouldSkipField(FieldAttributes f) {
                 return f.getDeclaringClass().equals(RealmObject.class);
             }

             @Override
             public boolean shouldSkipClass(Class<?> clazz) {
                 return false;
             }
         });

property 'resourceFile' not exist.

stupid bug!

三个步骤解决: - 关闭Instant Run - 修改build.gradle

 buildTypes {
     release {
         minifyEnabled true
         shrinkResources true
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
     }
     debug {
         minifyEnabled false
         shrinkResources false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
     }
 }
  • 重新编译