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
Can Not Set Debuggable Flag in Androidmanifest.Xml
Retrieve Contact Phone Number from Uri in Android
Gradle 0.9.+/0.9.2 Error: Unable to Load Class 'Com.Android.Builder.Testing.Api.Deviceprovider'
How to Calculate Phone's Movement in the Vertical Direction from Rest
Force a Camera to Always Open in Portrait Mode in Android
Custom Translucent Android Actionbar
Adjust Icon Size of Floating Action Button (Fab)
How to Use Roboto Font in Android Project
Android Mediaplayer/Videoview Error (1, -2147483648)
How Are Android Activities Handled with Jetpack Compose and Compose Navigation
Firebase Sort by Points Depending on Date
Properly Using Asynctask Get()
Populating Spinner Directly in the Layout Xml
How to Provide Different Android App Icons for Different Gradle Buildtypes