Export to GitHub

kryo - issue #94

InstantiatorStrategy won't work in DalvikVM (Android)


Posted on Oct 18, 2012 by Grumpy Monkey

What steps will reproduce the problem? 1. Deserialize a third-party class without a no-arg constructor. I.e. android.content.Intent 2. Call kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()) so it can create the object regardless

What is the expected output? What do you see instead? Expected a working object, an exception is thrown instead: java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactory, the LogCat shows "Could not find method sun.reflect.ReflectionFactory.getReflectionFactory, referenced from method org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>"

An approach to achieve this on Dalvik is shown here, would it work for Kryo? http://stackoverflow.com/questions/3121515/is-it-possible-to-bypass-constructors-when-instantiating-objects-in-android

What version of the Kryo are you using? 2.20

Please provide any additional information below. Thanks for the awesome library!

Comment #1

Posted on Oct 22, 2012 by Grumpy Monkey

I'm sorry, this turned out more of a usage problem rather than a bug. I created a custom InstantiatorStrategy using the hint from that question, it works in android 2.x but no 4.x. Then I tried creating custom serializer for ComponentName class, but now it gets stuck serializing Uri.StringUri (a private static inner class), how am I supposed to write a serializer for a private class? These classes have real simple constructors, can't these somehow be inferred from the ivars ? Or constructed with empty values, followed by fieldserializer filling in the ivars ?

Comment #2

Posted on Dec 4, 2012 by Grumpy Camel

You can't rely on a private class having the fields you expect. You can use reflection to create what you need. Though, I suggest doing serialization differently. Perhaps the objects already have a serialization mechanism (this is the whole purpose of Intent) that you can use, then just send those bytes. Or maybe you can get the necessary data and use a factory whatever API you are using provides to obtain an instance of the private class.

Status: Invalid

Labels:
Type-Defect Priority-Medium