views:

79

answers:

2

Hello,

I have an app which I want to run on as many Android phones as possible. My current solution is to just compile it with the newest version and doing the critical code in sections that will only be used on devices with the correct Android version. This worked fine until I started using Android 2.2. Now running the app on older phones gives me an error.

So I am thinking about using reflections, this would mean however that I would be compiling for version 1.5. So my question is will I suffer from any disadvantages? Apparantly compiling for a different version makes a difference as I can see at my error. So would I for example loose the JIT support or something?

Some insight from you guys on this topic would be great! Thanks in advance.

+2  A: 

I am sorry if I'm telling you obvious things, but are you sure that you really need reflections?

You will have run-time exceptions when you try to instantiate class that uses higher version of SDK then one installed on device/emulator.

To avoid it you need to check for SDK version in run-time. Example:

//check for multitouch support (Android 2.0+) 
inputSystem = android.os.Build.VERSION.SDK_INT < 5 ? new InputSystem() : new MultitouchInputSystem();
cement
Yes I am well aware of that. Currently I am using certain camera methodes which are only available in higher SDK versions. I only do this after I checked which SDK version is installed on the device. If it is not the needed version I switch to different options. My question was however, will I have disadvantages when using reflections ?
Pandoro
Ok, just wanted to make sure that you check SDK version to define which class to instantiate, but NOT which API to call.
cement
+1  A: 

There are no inherent disadvantages of using Java reflection to support different SDK's. However, there is a more elegant way described here that uses lazy class loading that is more maintainable. This I feel is a more robust way of handling different SDK support.

omermuhammed
If I understand the lazy class loading correctly I would still be building for Android 2.2 right? At least the example states : <uses-sdk android:minSdkVersion="8" />
Pandoro
No, you would use targetSdkVersion="8" but minSdkVersion set to the lowest version you support. Just make sure you perform your own version checks where needed at runtime.
adamp