@SerialVersionUID and Android

I was writing a piece of code in Scala, which was supposed to de-serialize some Java object, previously serialized elsewhere. However, I kept seeing exceptions like these:

  1. java.io.InvalidClassException: com.example.MyStuff; Incompatible class (SUID): com.example.MyStuff: static final long serialVersionUID =10L; but expected com.example.MyStuff: static final long serialVersionUID =-7513795898815927590L;

In my code I did have classes declared with the @SerialVersionUID attribute. The same code worked fine on my development machine. So it looked like it got lost from the class bytecode by the time that I ran it on Android. I initially suspected, that there could be a discrepancy in serialization representation between Dalvik (Android's JVM) and the classic JVM released by Sun, now Oracle. However, after some research, I reached a conclusion that it was overzealous proguard tool which simply stripped @SerialVersionUID from my classes. I fixed the issue using the following config:

  1. -keepclassmembers class com.example.full.class.Name {
  2. static final long serialVersionUID;
  3. java.lang.Object writeReplace();
  4. java.lang.Object readResolve();
  5. private static final java.io.ObjectStreamField[] serialPersistentFields;
  6. }

and now everything works fine.

Add new comment