Andre
Andre

Reputation: 647

@SerialVersionUID "lost" on Android?

I have an sbt Scala project:

  1. Subproject lib contains some classes with @SerialVersionUID, such as in:

    @SerialVersionUID(10L) class MyStuff extends Serializable { ... }

  2. Project app is an Android application (uses android-sdk-plugin), it has lib as its dependency

  3. I have a file, into which I serialized a number of MyStaff objects using local JVM on my dev machine (1.7.0_75)

  4. When I try to deserialize this file using the same JVM, all works fine

  5. When I try to deserialize this file on Android, using exactly the same code, I get the following error message:

    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;

So, looks like my serialVersionUID was lost while in transition to Android. Any ideas why? Some proguard settings? Something funky with Dalvik? Me being somehow stupid (most likely :) )

Upvotes: 2

Views: 337

Answers (1)

Andre
Andre

Reputation: 647

Looks like I can answer my own question. After some research I figured, that

  • There was nothing wrong with my classes
  • There seem to be no binary discrepancy between binary format used by Dalvik and "regular" JVM
  • It's Proguard's "fault"

A SO discussion: Serialization / Deserialization & Proguard

And a blog post with concrete details and a workaround:

http://littlepancake.com/2011/09/proguard-and-serialized-java-objects/

It boils down to the fact, that you need to guard serialization-related stuff from overzealous Proguard elimination, like this:

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

Note, Proguard understands wildcards as well :)

Upvotes: 1

Related Questions