Some time ago I published an app that serialized/deserialized an user object.
public String serializeUser(final User user) {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(user);
objectOutputStream.close();
} catch (final IOException exception) {
...
}
return new String(Base64.encode(byteArrayOutputStream.toByteArray(), DEFAULT));
}
public User deserializeString(final String userString) {
final byte userBytes[] = Base64.decode(userString.getBytes(), DEFAULT);
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(userBytes);
final ObjectInputStream objectInputStream;
final User user;
try {
objectInputStream = new ObjectInputStream(byteArrayInputStream);
user = (User) objectInputStream.readObject();
objectInputStream.close();
} catch (final IOException | ClassNotFoundException exception) {
...
}
return user;
}
The object was implemented this way:
public class User implements Serializable {
private String email;
private String name;
...
}
Then, after modifying my object (I added a new field), I learned the hard way that one has to set the serialVersionUID
in case the object definition ever changes, otherwise the deserializer won't be able to recognize the stored object (as it will autogenerate the serialVersionUID
). So I went ahead and did just that:
public class User implements Serializable {
private static final long serialVersionUID = 123L;
...
}
But now that I've republished the app with these changes, I keep getting error reports indicating the object could not be deserialized:
Caused by: java.io.InvalidClassException: com.myproject.h.e; local class incompatible: stream classdesc serialVersionUID = 184861231695454120, local class serialVersionUID = -2021388307940757454
I'm very aware that setting a new serial version would invalidate any previous serial version (link1, link2), but this isn't the case. As you can see the error log points to a totally different serialVersionUID
(18486...
and -20213...
) than the one I manually set to my User
class (123L
).
What am I missing?
If it is of any relevance, I'm using Proguard with the default configs.