Getting Device Build Number in Android
This article will guide you on how to obtain the Android device’s build number programmatically. This number, also known as the AOSP build number, is essential for various purposes, such as:
- Identifying device software version
- Debugging and troubleshooting issues
- Retrieving system information
Methods to Get Build Number
1. Using Build.VERSION.INCREMENTAL
The most direct way to access the build number is using the Build.VERSION.INCREMENTAL
constant provided by the Android SDK. This constant holds the incremental build number, which is often formatted as a string like “XXXXXXX” or “YYYYMMDDHHMMSS” (e.g., “20230815170000”).
import android.os.Build;
// ...
String buildNumber = Build.VERSION.INCREMENTAL;
System.out.println("Build Number: " + buildNumber);
Build Number: 20230815170000
2. Using Build.DISPLAY
Another option is to use the Build.DISPLAY
constant. This constant provides a more user-friendly string representation of the build number. It often includes additional information, such as the Android version, security patch level, and other relevant details.
import android.os.Build;
// ...
String displayString = Build.DISPLAY;
System.out.println("Display String: " + displayString);
Display String: Android 13 - 2023-08-15 - Security Patch Level 2023-08-01
3. Using Build.VERSION_CODES
If you need the Android API level, you can use Build.VERSION_CODES
.
import android.os.Build;
// ...
int apiLevel = Build.VERSION_CODES.S;
System.out.println("API Level: " + apiLevel);
API Level: 31
Comparison of Methods
Method | Description | Output |
---|---|---|
Build.VERSION.INCREMENTAL |
Raw incremental build number | XXXXXXX or YYYYMMDDHHMMSS |
Build.DISPLAY |
Human-readable string with build information | Android 13 – 2023-08-15 – Security Patch Level 2023-08-01 |
Build.VERSION_CODES |
API level | 31 (for Android 12) |
Choosing the Right Method
The best method for obtaining the build number depends on your specific requirements.
- If you need the raw build number, use
Build.VERSION.INCREMENTAL
. - For a user-friendly display of build details, use
Build.DISPLAY
. - To determine the Android API level, use
Build.VERSION_CODES
.