Identify Android API version, Device, Build Information programmatically

When you are developing an application which is dependent on device / platform hardware you will need to know device information based on which you can implement program’s logic. Also, when you are porting some applications from one android version to another version you will have to take care of backword compatibility due to Android’s change in API’s for the same, android provides ways to get SDK information, API version etc.

In this post, We take the refernece from our previously developed sample Application available, and will modify the activity code to display the information in Logcat. You can modify the same code to display this information using text view.

Get Device Information

        String board = android.os.Build.BOARD;
        String bootloader = android.os.Build.BOOTLOADER;
        String brand = android.os.Build.BRAND;
        String device = android.os.Build.DEVICE;
        String display = android.os.Build.DISPLAY;
        String hardware = android.os.Build.HARDWARE;
        String manufacturer = android.os.Build.MANUFACTURER;
        String model = android.os.Build.MODEL;
        String product = android.os.Build.PRODUCT;

Get Build Information like API level, SDK version

        String baseOs = android.os.Build.VERSION.BASE_OS;
        String codeName = android.os.Build.VERSION.CODENAME;
        String versionRelease = android.os.Build.VERSION.RELEASE;
        String sdk = android.os.Build.VERSION.SDK;
        int buildVersion = android.os.Build.VERSION.SDK_INT;

The reference code which uses all this to display information in logcat is as below, [ Since we have imported android.os.Build in application, we may not need to add “android.os” with every constants as shown below]

public class MainActivity extends AppCompatActivity {

        public static String TAG = "DeviceInfo";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String board = Build.BOARD;
        String bootloader = Build.BOOTLOADER;
        String brand = Build.BRAND;
        String device = Build.DEVICE;
        String display = Build.DISPLAY;
        String hardware = Build.HARDWARE;
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        String product = Build.PRODUCT;
        String serial = Build.SERIAL;

        Log.d(TAG, "Board: " + board + " Bootloader: " + bootloader + " Brand: " + brand + " Display: " + display);
        Log.d(TAG, "Hardware: " + hardware + " Manufacturer: " + manufacturer + " Model: " + model + " Product: " + product + " Serial: " + serial);

        // Refer for version codes : https://developer.android.com/reference/android/os/Build.VERSION_CODES.html
        String baseOs = Build.VERSION.BASE_OS;
        String codeName = Build.VERSION.CODENAME;
        String versionRelease = Build.VERSION.RELEASE;
        String sdk = Build.VERSION.SDK;
        int buildVersion = Build.VERSION.SDK_INT;

        Log.d(TAG, "BaseOs: " + baseOs + " CodeName: " + codeName + " Build Release Version: " + versionRelease + " Sdk: " + sdk + " Build SDK Version: " + buildVersion);

        if (buildVersion >= android.os.Build.VERSION_CODES.Q) {
                Log.d(TAG, "Android Version of This device is : Android Q : " + Build.VERSION.SDK_INT);
        } else if (buildVersion >= android.os.Build.VERSION_CODES.P) {
                Log.d(TAG, "Android Version of This device is : Android Pie : " + Build.VERSION.SDK_INT);
        } else {
                Log.d(TAG, "Android Version of This device is : " + Build.VERSION.SDK_INT);
        }

    }
}

Now, when you compile this code and run the application on your mobile / android device, you will get message’s like below in logcat,

$ logcat | grep DeviceInfo
DeviceInfo: Board: SDM660 Bootloader: unknown Brand: Nokia Display: 00WW_3_54J
DeviceInfo: Hardware: qcom Manufacturer: HMD Global Model: Nokia 7 plus Product: Onyx_00WW Serial: unknown
DeviceInfo: BaseOs: Nokia/Onyx_00WW/B2N_sprout:9/PPR1.180610.011/00WW_3_54H:user/release-keys CodeName: REL Build Release Version: 9 Sdk: 28 Build SDK Version: 28
DeviceInfo: Android Version of This device is : Android Pie : 28

You can download complete source code from https://github.com/lynxbee/android_device_information

Follow Lynxbee

Leave a Reply / Ask Question