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?
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.
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.
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.
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!
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);
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 }
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With