tags:

views:

2110

answers:

3

I get the impression that Android supports reflection. But does it really? How sensible would it be to rely on reflection heavily? What's the penalty?

A: 

Android supports reflection.

Once you've got a prototype running, you can benchmark and determine your bottlenecks.

If its reflection, then consider trying to cache interfaces and such to make it a one-off cost, rather than continually resolving the same interfaces from the same instances repeatedly.

Will
I guess the question is really if somebody already did the benchmarks on various devices.
Wilfred Springer
+5  A: 

It is supported, and even recommended in the situation where you want compatibility with multiple versions of the Android OS in one apk file. This article from the official Android Developers Blog describes how to build an app that requires only some early version of the API, and uses reflection to invoke new APIs if they are available:

Backward compatibility for Android applications

Chris Boyle
+1  A: 

There is a nice example of reflection in the sample code as well, in BusinessCard. This method wont result in a bunch of expections being thrown, so it should be much more performance friendly. It is also, in my opinion, easier to implement. Especially if it concerns a previously unimplemented method.

Here is where it is used: http://developer.android.com/resources/samples/BusinessCard/src/com/example/android/businesscard/ContactAccessor.html

sandis