How to get device (AOSP) Build Number in Android devices programmatically?

Learn how to get device (aosp) build number in android devices programmatically? with practical examples, diagrams, and best practices. Covers android, version-numbering development techniques with...

Programmatically Accessing Android Build Numbers (AOSP)

Android device displaying build number information

Learn how to retrieve the device's build number on Android (AOSP) programmatically, a crucial identifier for debugging, version tracking, and device management.

The build number on an Android device is a unique identifier that specifies a particular software build. It's especially important in AOSP (Android Open Source Project) environments for tracking specific firmware versions, debugging issues, and ensuring compatibility. This article will guide you through various methods to programmatically obtain this build number within your Android applications.

Understanding Android Build Information

Android provides a Build class within the android.os package that exposes various device and build-related properties. These properties are static fields that can be accessed directly. The build number itself is typically found in Build.DISPLAY or Build.ID, depending on the specific AOSP customization and how the build system is configured. It's important to note that while Build.VERSION.INCREMENTAL provides a build increment, it's often not the full, user-facing build number.

flowchart TD
    A[Start Application] --> B{Access android.os.Build Class}
    B --> C{Retrieve Build.DISPLAY}
    B --> D{Retrieve Build.ID}
    B --> E{Retrieve Build.VERSION.INCREMENTAL}
    C --> F[Display/Log Build Number]
    D --> F
    E --> G[Display/Log Incremental Version]
    F --> H[End]
    G --> H

Flowchart of retrieving Android build information

Retrieving the Build Number

The most common and reliable way to get the user-visible build number is by accessing android.os.Build.DISPLAY. This field usually contains the full build string as seen in the 'About phone' section of the device settings. For more granular or internal build IDs, android.os.Build.ID can be useful. It's good practice to check both, as their content can vary slightly across different manufacturers or AOSP derivatives.

import android.os.Build;

public class DeviceInfo {

    public static String getBuildNumber() {
        // Build.DISPLAY typically contains the full, user-visible build string
        String buildDisplay = Build.DISPLAY;
        
        // Build.ID can also contain a unique build identifier
        String buildId = Build.ID;
        
        // Build.VERSION.INCREMENTAL provides an incremental build version
        String buildIncremental = Build.VERSION.INCREMENTAL;

        // You might choose one over the other based on your specific needs
        // For a general 'build number', Build.DISPLAY is often preferred.
        return buildDisplay;
    }

    public static String getAllBuildInfo() {
        return "Build.DISPLAY: " + Build.DISPLAY + "\n" +
               "Build.ID: " + Build.ID + "\n" +
               "Build.VERSION.INCREMENTAL: " + Build.VERSION.INCREMENTAL;
    }
}

Java code to retrieve various Android build properties

Integrating into Your Application

To use this information within your Android application, you can call the getBuildNumber() method (or similar) from an Activity or Service. For example, you might display it in an 'About' screen, send it as part of crash reports, or use it to conditionally enable features based on specific firmware versions.

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView buildNumberTextView = findViewById(R.id.buildNumberTextView);
        if (buildNumberTextView != null) {
            String buildInfo = DeviceInfo.getAllBuildInfo(); // Using the helper method
            buildNumberTextView.setText("Device Build Info:\n" + buildInfo);
        }
    }
}

Example of displaying build information in an Android Activity