593

Does anyone know how to check the system version (e.g. 1.0, 2.2, etc.) programmatically ?

2
  • is there shortcut (code snippet or live template) to add this check in android studio Commented Apr 14, 2016 at 17:14
  • How to get the above information in C/C++ code ? Commented Apr 29 at 16:08

14 Answers 14

929

Example how to use it:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
}
5
  • 24
    I totally ignore any older version than Eclair (7), Android 2.1 and very soon we stop support for the 2.1 also. But you can use the SDK instead of SDK_INT.
    – ATom
    Commented May 3, 2012 at 4:29
  • 15
    SDK was depecated in API level 4. Use SDK_INT instead.
    – erdomester
    Commented Mar 24, 2014 at 6:44
  • 1
    I have a question: if it is running on a system older than GINGERBREAD, then we can not get android.os.Build.VERSION_CODES.GINGERBREAD, will the app crash? Commented Oct 24, 2016 at 2:56
  • 1
    No, because GINGERBREAD will by replaced by just the number during the java build. It is only necessary to use bigger compile version of Android.
    – ATom
    Commented Jan 27, 2017 at 21:42
  • I get Unresolved reference: Manifest for some reason Commented Jul 28, 2020 at 12:37
463

Check android.os.Build.VERSION.

  • CODENAME: The current development codename, or the string "REL" if this is a release build.
  • INCREMENTAL: The internal value used by the underlying source control to represent this build.
  • RELEASE: The user-visible version string.
6
  • 92
    any examples on how to use it?
    – Jono
    Commented Jul 6, 2011 at 9:13
  • 1
    The hard part about this is that SDK_INT has been defined in API Level 4 and using it fails on 1-3. Does anybody know how to nicely deal with that?
    – Zordid
    Commented Mar 23, 2012 at 11:45
  • SDK is available since API 1, also INCREMENTAL is available for all versions.
    – nuala
    Commented Jun 20, 2012 at 12:40
  • 1
    Build.VERSION.RELEASE is a String, therefore you can use this String however you like.
    – paiego
    Commented Sep 10, 2012 at 17:58
  • 13
    Don't worry, not even cavemen are using android APIs 1-3 today. @Zordid
    – Josh
    Commented Nov 19, 2015 at 10:43
135

Build.Version is the place go to for this data. Here is a code snippet for how to format it.

public String getAndroidVersion() {
    String release = Build.VERSION.RELEASE;
    int sdkVersion = Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
}

Looks like this "Android SDK: 19 (4.4.4)"

86

For checking device version which is greater than or equal to Marshmallow ,use this code.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){

    }

for ckecking others just change the VERSION_CODES like,
K for kitkat,
L for loolipop N for Nougat and so on...

70
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!

4
  • From SDK: "The user-visible version string. E.g., "1.0" or "3.4b5"." .... "3.4b5" how can I determine which version number is it ?
    – davs
    Commented Mar 7, 2012 at 15:17
  • The whole answer 2.3.3 is the version number, or 2.2, or 2.3.5 (custom rom in my case). That is the OS Version number as a whole.
    – Falcon165o
    Commented Mar 7, 2012 at 17:15
  • It returns the same thing Menu >> Settings >> About Phone. It should be labeled Firmware Version or something to that affect.
    – Falcon165o
    Commented Mar 7, 2012 at 17:21
  • 62
    How the hell could an int return a null? SDK_INT is a primitive int. Commented Feb 5, 2013 at 13:36
50

You can find out the Android version looking at Build.VERSION.

The documentation recommends you check Build.VERSION.SDK_INT against the values in Build.VERSION_CODES.

This is fine as long as you realise that Build.VERSION.SDK_INT was only introduced in API Level 4, which is to say Android 1.6 (Donut). So this won't affect you, but if you did want your app to run on Android 1.5 or earlier then you would have to use the deprecated Build.VERSION.SDK instead.

37

I can't comment on the answers, but there is a huge mistake in Kaushik's answer: SDK_INT is not the same as system version but actually refers to API Level.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    //this code will be executed on devices running ICS or later
}

The value Build.VERSION_CODES.ICE_CREAM_SANDWICH equals 14. 14 is the API level of Ice Cream Sandwich, while the system version is 4.0. So if you write 4.0, your code will be executed on all devices starting from Donut, because 4 is the API level of Donut (Build.VERSION_CODES.DONUT equals 4).

if(Build.VERSION.SDK_INT >= 4.0){
    //this code will be executed on devices running on DONUT (NOT ICS) or later
}

This example is a reason why using 'magic number' is a bad habit.

2
  • 8
    Actually, Build.VERSION.SDK_INT is the API level, not the version code. So the proper line would be if(Build.VERSION.SDK_INT >= 15){
    – erdomester
    Commented Mar 24, 2014 at 6:42
  • 1
    What if i told you version code is the same as API level and the SDK_INT of ICE_CREAM_SANDWICH is 14 not 15? @erdomester
    – Beyondo
    Commented Dec 18, 2017 at 10:31
29

Be aware that Build.VERSION.SDK_INT isn't reliable, it's mentioned by @Falcon165o and recently I ran into that one too.

So to get the String data (based on Android version list) of currently installed android, I made a code like this:

Java

//Current Android version data
public static String currentVersion(){
    double release=Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\\d+[.]\\d+)(.*)","$1"));
    String codeName="Unsupported";//below Jelly Bean
    if(release >= 4.1 && release < 4.4) codeName = "Jelly Bean";
    else if(release < 5)   codeName="Kit Kat";
    else if(release < 6)   codeName="Lollipop";
    else if(release < 7)   codeName="Marshmallow";
    else if(release < 8)   codeName="Nougat";
    else if(release < 9)   codeName="Oreo";
    else if(release < 10)  codeName="Pie";
    else if(release >= 10) codeName="Android "+((int)release);//since API 29 no more candy code names
    return codeName+" v"+release+", API Level: "+Build.VERSION.SDK_INT;
}

Kotlin

fun currentVersion(): String {
    val release = java.lang.Double.parseDouble(java.lang.String(Build.VERSION.RELEASE).replaceAll("(\\d+[.]\\d+)(.*)", "$1"))
    var codeName = "Unsupported"//below Jelly Bean
    if (release >= 4.1 && release < 4.4)  codeName = "Jelly Bean"
    else if (release < 5)   codeName = "Kit Kat"
    else if (release < 6)   codeName = "Lollipop"
    else if (release < 7)   codeName = "Marshmallow"
    else if (release < 8)   codeName = "Nougat"
    else if (release < 9)   codeName = "Oreo"
    else if (release < 10)  codeName = "Pie"
    else if (release >= 10) codeName = "Android "+(release.toInt())//since API 29 no more candy code names
    return codeName + " v" + release + ", API Level: " + Build.VERSION.SDK_INT
}

Example of an output it produce:

Marshmallow v6.0, API Level: 23

2
  • 1
    This code crash at Double.parseDouble(Build.VERSION.RELEASE) when the release contains more than one dot. For example 7.1.1 Commented Oct 4, 2017 at 20:36
  • @MiklósKeresztes Thank you for informative comment - I've fixed my answer. Commented Dec 19, 2017 at 15:14
27

For example, a feature only works for api21 up the following we fix bugs in api21 down

    if(Build.VERSION.SDK_INT >= 21) {
    //only api 21 above
    }else{
   //only api 21 down
    }
7
if (Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.HONEYCOMB_MR2) {
//do anything you  like.
}
1
  • 1
    Could you please elaborate more your answer adding a little more description about the solution you provide?
    – abarisone
    Commented Jun 22, 2015 at 12:42
6

Use This method:

 public static String getAndroidVersion() {
        String versionName = "";

        try {
             versionName = String.valueOf(Build.VERSION.RELEASE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }
1
  • 1
    How will this help? Commented Nov 13, 2018 at 9:26
5

use this class

import android.os.Build;

/**
 * Created by MOMANI on 2016/04/14.
 */
public class AndroidVersionUtil {
    public static int getApiVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    public static boolean isApiVersionGraterOrEqual(int thisVersion) {
        return android.os.Build.VERSION.SDK_INT >= thisVersion;
    }
}
1

Given you have bash on your android device, you can use this bash function :

function androidCodeName {
    androidRelease=$(getprop ro.build.version.release)
    androidCodeName=$(getprop ro.build.version.codename)

    # Time "androidRelease" x10 to test it as an integer
    case $androidRelease in
        [0-9].[0-9]|[0-9].[0-9].|[0-9].[0-9].[0-9])  androidRelease=$(echo $androidRelease | cut -d. -f1-2 | tr -d .);;
        [0-9].) androidRelease=$(echo $androidRelease | sed 's/\./0/');;
        [0-9]) androidRelease+="0";;
    esac

    [ -n "$androidRelease" ] && [ $androidCodeName = REL ] && {
    # Do not use "androidCodeName" when it equals to "REL" but infer it from "androidRelease"
        androidCodeName=""
        case $androidRelease in
        10) androidCodeName+=NoCodename;;
        11) androidCodeName+="Petit Four";;
        15) androidCodeName+=Cupcake;;
        20|21) androidCodeName+=Eclair;;
        22) androidCodeName+=FroYo;;
        23) androidCodeName+=Gingerbread;;
        30|31|32) androidCodeName+=Honeycomb;;
        40) androidCodeName+="Ice Cream Sandwich";;
        41|42|43) androidCodeName+="Jelly Bean";;
        44) androidCodeName+=KitKat;;
        50|51) androidCodeName+=Lollipop;;
        60) androidCodeName+=Marshmallow;;
        70|71) androidCodeName+=Nougat;;
        80|81) androidCodeName+=Oreo;;
        90) androidCodeName+=Pie;;
        100) androidCodeName+=ToBeReleased;;
        *) androidCodeName=unknown;;
        esac
    }
    echo $androidCodeName
}
0

According to Android documentation:

Build.VERSION.RELEASE – "The user-visible version string. E.g., "1.0" or "3.4b5" or "bananas""

In order to get a plain Android version (2.3, 5.1, 12.0, etc) I use this function:

private fun getVersion(): Float {
    val release = Build.VERSION.RELEASE
    val parsedVersion = "\\d+(\\.\\d+)?".toRegex()
        .find(release)?.value
    if (parsedVersion.isNullOrBlank()) return 0f
    return try {
        parsedVersion.toFloat()
    } catch (e: Exception) {
        0f
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.