InvalidClassException不兼容的类(SUID)

时间:2016-06-01 17:57:35

标签: java android serialization deserialization

我正在开发一个Android应用程序,其中我必须保留一些数据。为了坚持它们,我使用java序列化机制。

这里是我正在序列化的类的结构:

public final class A
    implements Serializable
{

  public static final class B
      implements Serializable
  {

    public final String field1;

    public final String field2;

    public B(String field1, String field2)
    {
      this.field1= field1;
      this.field2= field2;
    }
  }

  public static final class C
      implements Serializable
  {

    public long field1;

    public final B field2;

    public C(B field2)
    {
      this.field2 = field2;
    }

    public void setField1(long field1)
    {
      this.field1= field1;
    }

  }

  private final List<C> list1= new ArrayList<>();

  private final List<C> list2= new ArrayList<>();

  public A()
  {

  }

  public List<C> getList1()
  {
    return list1;
  }

  public List<C> getList2()
  {
    return list2;
  }

  //some other methods

}

如您所见,类实现Serializable但我没有指定serialVersionUID静态字段。

我必须对此应用进行大量更新,并从课程AC中删除一些方法。

正如预期的那样,当我尝试将旧类反序列化为新类时,我有以下例外:

  

引起:java.io.InvalidClassException:com.my.package.A;   不兼容的类(SUID):com.my.package.A:静态最终长   serialVersionUID = 9103658319690261655L;但是期待com.my.package.A:   static final long serialVersionUID = 7863476896806312319L;                                                                      at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336)                                                                      at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643)                                                                      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)                                                                      at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)                                                                      at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)                                                                      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)                                                                      在java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

因此,在我的班级A中,我尝试使用预期值指定serialVersionUID静态字段:

private static final long serialVersionUID = 7863476896806312319L;

但我的应用程序仍然崩溃......我现在有以下堆栈跟踪:

  

引起:java.io.InvalidClassException:com.my.package.A;   不兼容的类(SUID):com.my.package.A:静态最终长   serialVersionUID = 7863476896806312319L;但是期待com.my.package.A:   static final long serialVersionUID = 9103658319690261655L;                                                                      at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336)                                                                      at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643)                                                                      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)                                                                      at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)                                                                      at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)                                                                      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)                                                                      在java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

正如您所看到的,旧serialVersionUID字段现在具有预期值的值,而预期的字段具有旧serialVersionUID字段的值...

所以问题是:如何恢复我的数据并强制将旧类反序列化为新类?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

查看密切相关的问题here,我认为您应该尝试将serialVersionUID设置为预期值9103658319690261655L

请尝试设置:

private static final long serialVersionUID = 9103658319690261655L;

我希望这会有所帮助,试一试让我们知道。