How to Find the Android Version Name Programmatically

How to find the Android version name programmatically?

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!

Return Android OS name

I played around with an answer from the suggested question, found here, and came up with this one liner that returns the "codename" of the currently running OS version:

Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName();

How do I get my application Version in Android

This page has a tips on how to do it from java:

PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(
context.getPackageName(), 0);
String version = info.versionName;

Also, this link has official information on how to properly set up your application versioning.

How can I check the system version of Android?

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.

Android versionCode programmatically

If you're using gradle (default in AndroidStudio) you can:

BuildConfig.VERSION_CODE


Related Topics



Leave a reply



Submit