Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the SDK version of the application and not of the device?

Tags:

java

android

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.

like image 955
Ron Tesler Avatar asked Nov 13 '22 04:11

Ron Tesler


1 Answers

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
             * */
        }
    }
like image 153
gunar Avatar answered Nov 14 '22 21:11

gunar