46

How can I get the current android device version (1.5, 1.6, 2.0, etc.) programmatically? i.e I have installed my apk on an Android 2.2 device. I need to get the device version (android emulator version in which my application is currently running ).

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
user629872
  • 591
  • 2
  • 5
  • 9

5 Answers5

106

Something like these:

String myVersion = android.os.Build.VERSION.RELEASE; // e.g. myVersion := "1.6"
int sdkVersion = android.os.Build.VERSION.SDK_INT; // e.g. sdkVersion := 8; 

You can retrieve all SDK codes from Build.VERSION_CODES

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
  • What is recommended to use for representing os, Build.VERSION.RELEASE or Build.VERSION.SDK.INT? Both get the job done, but is there an industry standard or more popular choice? – portfoliobuilder Feb 27 '15 at 19:51
  • @portfoliobuilder `Build.VERSION.SDK_INT` is good at comparisons between versions (less than/greater than, etc.) – Andrey Atapin Mar 02 '15 at 05:20
  • s described in the android documentation, the SDK level (integer) the phone is running is available in: android.os.Build.VERSION.SDK_INT; The enum corresponding to this int is in the android.os.Build.VERSION_CODES class. Code example: 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 } – hitesh141 May 21 '15 at 09:45
  • 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:46
14
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}

By this code we can execute separate code for Pre Lollipop devices.

Vignesh KM
  • 1,979
  • 1
  • 18
  • 24
5

If you want to see the firmware version number then you can use

Build.VERSION.SDK_INT

But if you want firmware version name then you should use something like this

Build.VERSION_CODES
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
4

Try This,

    String deviceOs = android.os.Build.VERSION;

I hope this will help.

Ashish Gupta
  • 737
  • 11
  • 18
1

Take a look at the Build.VERSION version class. Depending on what you want to do, you might want to use the RELEASE or SDK_INT attribute.

Julian
  • 2,051
  • 2
  • 22
  • 30