0

There are a variety of variables stored in the android.os.Build class that you can check for the current version of the given Android platform. Of course, these only work in an emulator or device as the SDK returns null or 0 during compilation

Is there any other variables available within the Android SDK that represents the current SDK version that is available during compilation via the Java API?

FYI, I am trying to implement this feature: https://github.com/johncarl81/transfuse/issues/18

Thanks.

John Ericksen
  • 10,995
  • 4
  • 45
  • 75
  • 1
    What do you mean by 'available within the complier/Java VM' as on Android you are using the Davlik VM? – Morrison Chang Nov 11 '12 at 00:38
  • You compile Android Java code in the regular Java compiler first. This generates Java Bytecode which is then turned into Dalvik Bytecode by the Android sdk. I mean is there a version number available during the compilation phase when the Android Java is being turned into Java bytecode. – John Ericksen Nov 11 '12 at 00:41
  • Sorry I'm not understanding your question. I'm not aware of a major differences in bytecode between different Davik versions (or at least that matter to a App developer). Is there a reason why you want to know? Or am I missing your point. – Morrison Chang Nov 11 '12 at 00:47
  • I am looking for the version number in the Android Java API. Build.Version.SDK_INT returns 0 in the Java API as it is just a stub in the API. What I am trying to do is use the API version during compilation (see my feature reference). – John Ericksen Nov 11 '12 at 00:53
  • Its a stub as it will only work in a valid environment, the code you reference is relatively standard in Android. See second answer to: http://stackoverflow.com/questions/12551629/different-java-methods-for-different-api-levels – Morrison Chang Nov 11 '12 at 00:57
  • Right, and what I am trying to do is write a feature that implements this SDK switching feature at compile time, avoiding the need to switch at runtime with reflection. – John Ericksen Nov 11 '12 at 00:59
  • The Lint documentation yields [this](http://tools.android.com/recent/lintapicheck) about API checks. I would try to use the `@TargetApi(...)` annotation. – opatut Nov 11 '12 at 01:01

1 Answers1

0

If you want to set a minimum SDK level, that can be done in the AndroidManifest.xml file. If you want to support multiple SDK levels, you'll have to do that at runtime since its implied that you won't know if you are running at SDK 8 or SDK 15 until you are actually running your app. If you want to avoid lint warning, I would use @TargetApi(...) that opatut referenced......

D.J
  • 1,439
  • 1
  • 12
  • 23
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
  • So, here's the idea. Since I can upload multiple support versions to the market, I can compile a different version for each API level. If I can compile a different version for each API level then I can use different code or classes for each version I upload. What I would like to do is leverage the API level in my custom Annotation processor library (compiler extension) to reference one component class or another based on the API level. I bet I could set a variable in Maven, but I was wondering if there was a more build-system-agnostic way to do so. Seems like the answer is probably no. – John Ericksen Nov 11 '12 at 01:13
  • 1
    You would still need to do a runtime check to determine which OS level you are on since you don't know if the device got upgraded. Any build system/framework would need to take that into account. – Morrison Chang Nov 11 '12 at 01:20
  • You're right about that. I hadn't considered upgrades. I wonder if this feature would be still be handy with just a runtime if-statement. Maybe I could generate this based on annotations. as described in the other question you pointed out. – John Ericksen Nov 11 '12 at 01:27