I am trying Kryo+Chill (com.twitter:chill_2.11:0.8.0
and Scala version is 2.11.8
). Following code works fine on desktop, but on Android it crashes.
case class TestDogRef(id: Int)
case class TestDog(ref: TestDogRef, name: String, friends: Seq[TestDogRef])
class TestHouse[T](val data: Seq[T])
case class TestDogHouse(override val data: Seq[TestDog]) extends TestHouse[TestDog](data)
def serialize[A](data: A): Array[Byte] = {
val instantiator = new ScalaKryoInstantiator
instantiator.setRegistrationRequired(false)
val kryo = instantiator.newKryo()
val bao = new ByteArrayOutputStream
val output = new Output(bao)
kryo.writeObject(output, data)
output.close()
bao.toByteArray
}
def deserialize[A](ser: Array[Byte], clazz: Class[A]): A = {
val instantiator = new ScalaKryoInstantiator
instantiator.setRegistrationRequired(false)
val kryo = instantiator.newKryo()
val input = new Input(new ByteArrayInputStream(ser))
val deserData = kryo.readObject(input, clazz)
deserData
}
override def run(): String = {
val orig = TestDogHouse(Seq(TestDog(TestDogRef(4), "Doggy", Seq(TestDogRef(1)))))
val serialized = serialize(orig)
val deserialized = deserialize(serialized, classOf[TestDogHouse])
deserialized.toString
}
Here is the crash:
07-14 11:55:42.053 6744-6764/x.y E/AndroidRuntime: FATAL EXCEPTION: GLThread 137
java.lang.ExceptionInInitializerError
at com.twitter.chill.java.PackageRegistrar.all(Unknown Source)
at com.twitter.chill.AllScalaRegistrar.apply(Unknown Source)
at com.twitter.chill.ScalaKryoInstantiator.newKryo(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.serialize(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.run(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at scala.collection.immutable.List.foreach(Unknown Source)
at x.y.LibraryTests$.run(Unknown Source)
at x.y.a.a(Unknown Source)
at com.badlogic.gdx.backends.android.j.onSurfaceChanged(Unknown Source)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: com.esotericsoftware.kryo.KryoException: Error while getting field 'words' of bitSet
at com.twitter.chill.java.BitSetSerializer.<clinit>(Unknown Source)
at com.twitter.chill.java.PackageRegistrar.all(Unknown Source)
at com.twitter.chill.AllScalaRegistrar.apply(Unknown Source)
at com.twitter.chill.ScalaKryoInstantiator.newKryo(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.serialize(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.run(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at scala.collection.immutable.List.foreach(Unknown Source)
at x.y.LibraryTests$.run(Unknown Source)
at x.y.a.a(Unknown Source)
at com.badlogic.gdx.backends.android.j.onSurfaceChanged(Unknown Source)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: java.lang.NoSuchFieldException: words
at java.lang.Class.getDeclaredField(Class.java:631)
at com.twitter.chill.java.BitSetSerializer.<clinit>(Unknown Source)
at com.twitter.chill.java.PackageRegistrar.all(Unknown Source)
at com.twitter.chill.AllScalaRegistrar.apply(Unknown Source)
at com.twitter.chill.ScalaKryoInstantiator.newKryo(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.serialize(Unknown Source)
at x.y.LibraryTests$KryoWithChillTest$.run(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at x.y.LibraryTests$$anonfun$run$1.apply(Unknown Source)
at scala.collection.immutable.List.foreach(Unknown Source)
at x.y.LibraryTests$.run(Unknown Source)
at x.y.a.a(Unknown Source)
at com.badlogic.gdx.backends.android.j.onSurfaceChanged(Unknown Source)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
I suspect it is because of ProGuard - something gets mangled or dropped, but I have no idea what else I can do.
Relevant proguard rules:
-keep class scala.collection.BitSet { *; }
-keepclassmembers class scala.collection.BitSet { *; }
-keep class scala.collection.immutable.BitSet { *; }
-keepclassmembers class scala.collection.immutable.BitSet { *; }
-keep class scala.collection.BitSetLike { *; }
-keepclassmembers class scala.collection.BitSetLike { *; }
-keep class scala.collection.immutable.BitSet.** { *; }
-keepclassmembers class scala.collection.immutable.BitSet.** { *; }
-keepnames class scala.** { *; }
I would appreciate any help.
This problem is not related to ProGuard. The chill
java library has a BitSetSerializer
for java.util.BitSet
classes that accesses a field (named words
) via reflection. Now the java.util.BitSet
implementation that is included in Android (derived from the Apache Harmony project upto Android 6, Android N will be based on OpenJDK) does not have such a field and you will get the runtime error as seen in the question.
Looking at the source code of the chill-scala library, using a ScalaKryoInstantiator
will also register all Java related serializers. You might be able to circumvent the issue by using an EmptyScalaKryoInstantiator
instead and registering all the needed serializers:
val instantiator = new EmptyScalaKryoInstantiator
instantiator.setRegistrationRequired(false)
val kryo = instantiator.newKryo()
val col = new ScalaCollectionsRegistrar
col(kryo)
ScalaTupleSerialization.register(kryo)
....
// others as needed
See also ScalaKryoInstantiator.scala for reference which serializers to include.