5

I'm trying to get the SDK version of an application, but if I test

android.os.Build.VERSION.SDK_INT

in an application targeted for 2.3.3, I get the device's version which is 17 instead of 10.

I don't know the SDK version that my code will be compiled with, because I'm writing a code snippet that developers can add to their own application.

Thanks.

Patrick
  • 16,618
  • 5
  • 65
  • 82
Ron Tesler
  • 1,096
  • 1
  • 11
  • 23
  • Am being curios.. But y do you need to know the sdk version. please give a senario. – amalBit Jun 05 '13 at 10:39
  • I'm writing a code snippet that developers can add to their own application. – Ron Tesler Jun 05 '13 at 10:49
  • ... and why do you need the SDK version there? – laalto Jun 05 '13 at 11:04
  • to know if I can use certain functions that are only supported in 4.2 – Ron Tesler Jun 05 '13 at 11:06
  • The `android.os.Build` is the mechanism for that - to determine underlying platform version at runtime. Developers should in any case be using the latest SDK android.jar that contains the stubs for all functions up to that API level. It's not a problem to require SDK 17 or later from people using your code. – laalto Jun 05 '13 at 11:10
  • 2
    But if you really really really need to be able to build with older SDK and still be able to call new functions when running on newer API levels, use java reflection. – laalto Jun 05 '13 at 11:13
  • Reflection is what I did, but I thought there's a better way, saving the need to try and call an unsupported method and then act differently if an exception is thrown. – Ron Tesler Jun 05 '13 at 11:26
  • Welcome to Stackoverflow! When asking questions, you don't have to put tags in your titles, you use tags instead. Please read http://meta.stackexchange.com/q/19190/147072 for more information. – Patrick Jun 05 '13 at 11:48

3 Answers3

1

You should always target for latest version. That will bring you the latest API. I guess you need to know the current version of the API available on the device. For that you would do something like this (to check if current API is 2.3):

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static void someMethod() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            /**
             * Call code accessible from Gingerbread onwards
             * */
        } else {
            /**
             * Call code from previous API
             * */
        }
    }
gunar
  • 14,392
  • 7
  • 54
  • 83
  • The problem is that I don't know the SDK version that my code will be compiled with. I want to write a code that developers will use in their application. – Ron Tesler Jun 05 '13 at 10:37
  • Then above method should be exactly what you need ... in first if statement you'll use APIs available from Gingerbread and so on. – gunar Jun 05 '13 at 11:08
0

if I have not misunderstood

you need android.os.Build.VERSION.RELEASE;

Blackbelt
  • 148,780
  • 26
  • 271
  • 285
0

Have a look at this.. targetSdkVersion.

Use this code:

    ApplicationInfo info=getApplicationInfo();// this class give info from the application tag in the manifest file
    final int target=info.targetSdkVersion;

Also go through this discussion.

amalBit
  • 11,489
  • 6
  • 71
  • 89
  • I get "1.0". Your code gives me the application's version, not the SDK's version being used.. – Ron Tesler Jun 05 '13 at 10:32
  • After much googling.. i seem to have found what u are looking for @RonTesler – amalBit Jun 05 '13 at 11:07
  • Thanks @amal, but the targetSdkVersion in my case has a value of 0, because it wasn't stated in the manifest. I can't control the hosting application's manifest file, as it's not mine. – Ron Tesler Jun 05 '13 at 11:18
  • But the application will have the target version set when its running right?.. your question is more complex than i thought.. – amalBit Jun 05 '13 at 11:28
  • I tested it, and the 0 I got was at run-time. – Ron Tesler Jun 05 '13 at 11:36