Patches to Objenesis for Android/Dalvik

60 views
Skip to first unread message

Ian Parkinson

unread,
Dec 14, 2012, 12:12:35 PM12/14/12
to objene...@googlegroups.com
Hi objenesis-dev,

I've been trying to get Objenesis running on Android/Dalvik; attached are some patches which I think makes it work at least on recent releases of Android. You've a bug for this in your bug tracker (http://code.google.com/p/objenesis/issues/detail?id=13) which I'll update too.

I'd like to include Objenesis itself in the Android Open Source Project, for which we need to build from source. As things stand, the explicit references to sun.reflect.ReflectionFactory fail to build in the Android environment (and, I assume, would cause Objenesis's build to fail if using anything other than Sun's JDK?) I've also attached a patch which will fix this for me, and I'd be grateful if you could review it.

The attached patches are:

buildOnNonSun.diff - changes the SunReflectionFactory instantiators to use reflection, like all of the other instantiators, so that it can build in environments which don't have sun.reflect.ReflectionFactory.

androidInstantiator.diff - adds support for Android/Dalvik.

tckOnAndroid.diff - a little Android app which wraps the TCK so that it can be run on Android devices or emulators.

readme.diff - readme for the TCK app. There are no other readmes in the project, so I'm not sure if you'd rather have this sort of information on the project website?

It won't surprise you to know that the technique I've used in the two Android Instantiators is unsupported; like all the others it relies on internal methods which are subject to change. Using the TCK I've verified that it works on Android 4.0 and 4.1 (Ice Cream Sandwich and Jelly Bean; API levels 14 through 17). It doesn't work on Android 2.3 (Gingerbread; tested at API level 10). I haven't been able to test it on Android 3.0 (Honeycomb).

Many thanks for your help,

Ian Parkinson

--

Google UK Limited

Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902
buildOnNonSun.diff
androidInstantiator.diff
tckOnAndroid.diff
readme.diff

Henri Tremblay

unread,
Dec 14, 2012, 1:01:10 PM12/14/12
to objene...@googlegroups.com
Hello,

Thanks a lot. This issue is planned to be nailed ASAP.

Cheers,
Henri

Ian Parkinson

unread,
Dec 17, 2012, 9:57:52 AM12/17/12
to objene...@googlegroups.com
Hi Henri,

Thanks for getting back to me so promptly! Just for my own planning, do you have any sort of timescale in mind? And please let me know if I can help further.

Cheers,
Ian

Henri Tremblay

unread,
Dec 20, 2012, 3:07:07 PM12/20/12
to objene...@googlegroups.com
I'm aiming end of January.

brice....@gmail.com

unread,
Jan 3, 2013, 6:24:24 AM1/3/13
to objene...@googlegroups.com
Cool, we want to upgrade our versions at mockito as well, thanks Ian :)


-- Brice

Henri Tremblay

unread,
Jan 7, 2013, 7:21:55 PM1/7/13
to objene...@googlegroups.com
Hi,

Getting to it. One thing. Compiling on the Sun JDK was by design. Because it means using static compilation for it. Other instantiators are using reflection. Since the Sun JDK is the most wildly used, it makes sense.

If I understood correctly, to be used on Dalvik, it needs to be compiled on Dalvik. So I'm required to remove this?

Thanks
Henri

Ian Parkinson

unread,
Jan 8, 2013, 9:14:56 AM1/8/13
to objene...@googlegroups.com
Hi Henri,

Thanks again for taking a look at this!

I see your point about the Sun JDK. Strictly speaking, Objenesis should work on Dalvik even with the static references - most users can just drop the pre-built Objenesis .jar into their Android app and that class will simply never be loaded. (You could verify that easily enough with the Instrumentation I sent you, I think.)

Unfortunately, for the Android Open Source Project, we need to be able to build Objenesis from source; and when we do, we build using the Android SDK, which compiles against the Dalvik libraries.

One alternative would be for us to substitute in a replacement implementation of StdInstantiatorStrategy and SerializingInstantiatorStrategy customized to just refer to the Dalvik Instantiators; I'm hoping that we don't have to start maintaining AOSP's copy as a fork, though.

Cheers,

Ian

lhda...@gmail.com

unread,
Apr 21, 2013, 7:05:24 AM4/21/13
to objene...@googlegroups.com
Hello Ian,

Do you see any chance to make objenesis compatible with Android Gingerbread / API 10 and upwards ? I am working with the java deep-cloning library (https://code.google.com/p/cloning/) which relies on objenesis, and it fails to run on Gingerbread. Even when there's no fix planned, could you please explain how I can workaround this problem ?

On a nother note: in your original post, you mentioned that the fixes are "unsupported" as they use reflection on Android internal methods, which can change. How would you rate the complexity for fixes for upcoming Android versions ? 

Thanks and best regards

Hai Dang Le
 

Ian Parkinson

unread,
Apr 22, 2013, 11:36:32 AM4/22/13
to objene...@googlegroups.com, lhda...@gmail.com
Hi,

I haven't looked closely at how to make Objenesis work on Gingerbread - we'd need to find some private methods that happen to do the right thing, and write appropriate Instantiators to invoke them. One of the existing instantiators may well work.

For future versions of Android, there was a change a couple of months back to introduce Unsafe.allocateInstance, (see https://android-review.googlesource.com/#/c/52622). Objenesis can switch to this at least for standard allocation, and I'd say it's unlikely to break in the future - it's still not strictly speaking documented or supported, but it's at least well-defined and a method that shared with other VMs. I don't know of an equivalent for serialization-like instantiation.

Ian

(Obvious disclaimer: I work for Google, but I don't have any particular insider knowledge of the Dalvik VM.)


 

--
 
---
You received this message because you are subscribed to the Google Groups "Objenesis developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objenesis-de...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Henri Tremblay

unread,
Jul 21, 2013, 9:47:34 AM7/21/13
to objene...@googlegroups.com, lhda...@gmail.com
A dumb question here.

Can I rely on any system property to make the difference between the Dalvik versions? Maybe the java.vm.version? (which is 1.2.0 on API8 and 1.6.0 on API17.. but it's probably a bad idea)

I know there is android.os.Build.VERSION.RELEASE but it would be a bit more painful to use.
Reply all
Reply to author
Forward
0 new messages