39

Possible Duplicate:
Retrieving Android API version programmatically

I am trying to retrieve the current API Version of the device my Application is Running. Is there a way i can get that and Store it on a String. This needs to Work on 1.5 Version and Up. Examples would be greatly appreciated. Thank you.

Community
  • 1
  • 1
rbz
  • 759
  • 1
  • 8
  • 17

3 Answers3

72

You can get it by calling Build.VERSION.SDK.

From 1.6 on, you should use Build.VERSION.SDK_INT instead
because Build.VERSION.SDK is deprecated.

iandisme
  • 6,346
  • 6
  • 44
  • 63
  • Awesome Thanks A Bunch for the quick response – rbz Aug 17 '10 at 21:22
  • 2
    Deprecated - yes. But how should I determine I got Cupcake or lower? Accessing SDK_INT fails on devices prior to Donut, as it was defined in API Level 4! – Zordid Mar 23 '12 at 11:47
  • Check out my answer on other question for code - http://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically/11683754#11683754 – nikib3ro Jul 27 '12 at 08:03
  • The enum corresponding to this int is in the android.os.Build.VERSION_CODES class. int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){ // Do something for froyo and above versions } else{ // do something for phones running an SDK before froyo } Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision). – hitesh141 May 21 '15 at 09:44
21
Build.VERSION.RELEASE;

That will give you the actual numbers of your version; aka 2.3.3 or 2.2.

The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a none standard OS (aka my android is running 2.3.5) and that will return a NULL when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter what!

Falcon165o
  • 2,875
  • 1
  • 20
  • 31
20

It is defined on the android.os.Build.VERSION.SDK constant, just use it.

AKS
  • 4,618
  • 2
  • 29
  • 48
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93