简体   繁体   中英

How can I specify a SerialVersionUID for a scala.collection.mutable.Hashmap?

I have a game with saves serialized with java.io. The saves are Hashmap[String, Any] . The classes contained in the Any s all have SerialVersionUID(1L). I use sbt with this configuration https://github.com/ajhager/libgdx-sbt-project.g8 .

When I launch the project with desktop/run it works just fine, but the jar produced by assembly throws this error:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.io.InvalidClassException: scala.collection.mutable.HashMap; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = -1796852918056492754
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.io.InvalidClassException: scala.collection.mutable.HashMap; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = -1796852918056492754
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at ntn.castersrun.IntoFileSaver$.getSave(IntoFileSaver.scala:34)
    at ntn.castersrun.IntoFileSaver$.load(IntoFileSaver.scala:24)

I probably should save as Array of Tuple instead, but I can't figure out what causes this error or how I could fix it otherwise.

I still don't know how to set the SerialVersionUID of Scala Tuples, or why the Scala Hashmap had UID 1, unlike the tuple.

However I managed to get assembly to produce acceptable output by telling ProGuard not to obfuscate any Serializable. Just Tuple2s and my own Serializable classes would have sufficed, but I didn't want to learn all about ProGuard -keep syntax.

http://proguard.sourceforge.net/manual/examples.html#serializable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM