Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the Android version name programmatically?

I write code for finding the Android version like this

String version=Build.VERSION.RELEASE;

by using this code I am get the version number but I want version name. how to get the version name?

like image 551
Venkat Avatar asked May 11 '12 08:05

Venkat


People also ask

How can I get Android OS programmatically?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view to show device version number.

How do I know my version of name?

VersionName is a string and can be anything you want it to be. This is where you define your app as "1.0" or "2.5" or "2 Alpha EXTREME!" or whatever. Android's official description of android:versionCode and android:versionName can be found here: developer.android.com/tools/publishing/… @RomanGherta It is as of API 28.

What is version code and version name?

The version code is an incremental integer value that represents the version of the application code. The version name is a string value that represents the “friendly” version name displayed to the users.


5 Answers

As suggested earlier, reflection seems to be the key to this question. The StringBuilder and extra formatting is not required, it was added only to illustrate usage.

import java.lang.reflect.Field;
...

StringBuilder builder = new StringBuilder();
builder.append("android : ").append(Build.VERSION.RELEASE);

Field[] fields = Build.VERSION_CODES.class.getFields();
for (Field field : fields) {
    String fieldName = field.getName();
    int fieldValue = -1;

    try {
        fieldValue = field.getInt(new Object());
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    if (fieldValue == Build.VERSION.SDK_INT) {
        builder.append(" : ").append(fieldName).append(" : ");
        builder.append("sdk=").append(fieldValue);
    }
}

Log.d(LOG_TAG, "OS: " + builder.toString());

On my 4.1 emulator, I get this output:

D/MainActivity( 1551): OS: android : 4.1.1 : JELLY_BEAN : sdk=16

Enjoy!

like image 169
Kevin Grant Avatar answered Oct 13 '22 13:10

Kevin Grant


Optimized code, this will work:

import java.lang.reflect.Field;

Field[] fields = Build.VERSION_CODES.class.getFields();
String osName = fields[Build.VERSION.SDK_INT + 1].getName();
Log.d("Android OsName:",osName);
like image 19
Shyam Kumar Avatar answered Oct 13 '22 12:10

Shyam Kumar


After API 28 (Android Pie), Build.VERSION_CODES were changed some fields.

So, if you using:

Field[] fields = Build.VERSION_CODES.class.getFields();
String osName = fields[Build.VERSION.SDK_INT + 1].getName();

will cause your app crash immediately because of Out Of Range Exception.

The solution for all API level is:

Field[] fields = Build.VERSION_CODES.class.getFields();
String codeName = "UNKNOWN";
for (Field field : fields) {
    try {
        if (field.getInt(Build.VERSION_CODES.class) == Build.VERSION.SDK_INT) {
            codeName = field.getName();
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Or in Kotlin with Java 8's style:

val fields = Build.VERSION_CODES::class.java.fields
var codeName = "UNKNOWN"
fields.filter { it.getInt(Build.VERSION_CODES::class) == Build.VERSION.SDK_INT }
      .forEach { codeName = it.name }
like image 18
Trần Leo Avatar answered Oct 13 '22 11:10

Trần Leo


http://developer.android.com/reference/android/os/Build.VERSION_CODES.html contains fields that have the name you're looking for. So you could use reflexion to find which field corresponds to the "version" value.

Why do you want the name ?

like image 5
Philippe Girolami Avatar answered Oct 13 '22 11:10

Philippe Girolami


You will get the information from these

android.os.Build.VERSION_CODES 

android.os.Build.VERSION.SDK_INT

More information can be had from this link:

Retrieving Android API version programmatically

Hope this will help you.

like image 4
UVM Avatar answered Oct 13 '22 11:10

UVM