Search code examples
androidandroid-source

Where and when is generated android.Build.SERIAL in AOSP?


I know, that android.Build.SERIAL is generated at first device boot, but I can't locate where and when exactly. I'm building AOSP Jelly Bean, Android tablet, nosdcard.

2nd question: is this serial number really unique for all Android devices?


Solution

  • According to this thread, it clearly says that it's unique, but added since API 9 and may be not present on all devices.

    If you're writing your app for a specific device's model, you could direclty check if it has an IMEI. Otherwise, as you said, I recommend you to write a custom ID generator module.
    You will be sure that your ID will be unique and available for all devices.

    IMEI represents the serial number of the device. It's sure it's unique. Two different devices can't have the same serial number.

    To get the serial number of the device you just have to call :

    String serial =  Build.SERIAL;
    

    It exists another approach. You can get the id by calling Secure.ANDROID_ID.

    A 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)

    private final String ANDROID_ID = Secure.getString(getContext().getContentResolver(),
                                                            Secure.ANDROID_ID);
    

    Take care because it says that the value MAY change if a factory reset is performed.