views:

70

answers:

2

Hi,

I would like to make a single Android app for multiple Android versions (possibly every one of them) My problem is that I want to check what is the version of Android the app is currently running on, and dynamically load a class which is version dependent. This part should be ok.

I just wonder how I can achieve that without compilation errors in my Eclipse project. I mean, the project is configured for a particular target (1.5, 2.1 ...), so if a class in my project is not compatible wich the selected target, it will result in errors.

Is there a way to export this classes even if they are not fit for the platform (I thought about a separated lib, but then again : how to compile theses classes into a lib without compilation pbs?) ? This should be ok since they won't be loaded until I ask them to after having checked Android version.

Thanks!

A: 

If you compile you project with latest target, i.e. 2.2 it still will run on 1.5.

Konstantin Burov
This isn't even remotely true. There are a number of methods (specifically in the telephony package for example) that are in 2.2 but not 1.5. The opposite _might_ be true but then you wouldn't be able to use the new features.
Chris Thompson
Well I haven't say you can use any 2.2 functionality and run on 1.5. I said that you can compile 1.5 compatible code with 2.2 and it will run on 1.5. This guy wants to write such a code which is truly possible.
Konstantin Burov
Well I just don't want to make the hypothesis of backward compatibility, I mean, imagine a deprecated field in 1.5 finally removed in 2.2? If I built my project in 2.2, I could not see it, I guess. My goal is a class 100% compatible per version. But I'm not saying your solution won't work between 1.5 and 2.2... But maybe between 1.0 and 3.0+... But thanks!
jillemash
A: 

You could use Class.forName to load different classes depending on different conditions:

public interface MyType {}
public class MyTypeOn15 implements MyType {}
public class MyTypeOn16 implements MyType {}
public class MyTypeOn20 implements MyType {}
// ...

and somewhere else in your code:

MyType myType = null;
if (getActualTarget().equals("1.5") {
   myType = Class.forName("MyTypeOn15").newInstance();
} else if (getActualTarget().equals("1.6") {
   myType = Class.forName("MyTypeOn16").newInstance();
} // ...

Note that you need an accessible empty constructor in your implementations. And getActualTarget() is your magic method that's returns the target as a String identifier...

Andreas_D
Thanks for your reply!By magic method, you mean it does not exist, and I have to write it myself? :)That is what I am trying to do by accessing android.os.Build.VERSION_CODES, but this does not exist in 1.5... So I need a way to check platform version for older versions (maybe the lack of this field can be a criteria?). I wish this magic method existed :).But my problem was how do you have the MyTypeOn15.java files in my project without having compilation errors due to the fact I selected an uncompatible Android target? Thank you
jillemash
Oh, that's not a big deal. Create separate projects where the targets and the classes match and when you create the deployable, just copy all those classes in one library. The library then contains classes that won't run in the actual environment, but this doesn't matter because they're not used and loaded.
Andreas_D
Ah ok, I was thinking to something like it, but I wish I could have done it with one project ^^. But I don't know yet how to make a library for an Android project (:s) . Can I just "jar" the .class files of my several platform specific projects and add this jar in buildPath->libraries in Eclipse for my "all-in-one" project? I'll try this, seems to be the solution to my problem, thank you!
jillemash
Well I get a ClassNotFoundException:Class<?> clazz = Class.forName("fully.qualified.name.MyTypeOn16");MyTypeOn16 has been put in a jar, which is added to project classpath.Is there something special to do with android? Maybe get a classloader?
jillemash
Nope, sorry, I just made a mistake while building my jar file.Thanks again, it works ;)
jillemash