Android: How to programmatically access the device serial number shown in the AVD manager (API Version 8)

how to get device serial number in android studio
android serial number format
android 10 imei alternative
android 10 location permission
android get serial number adb
android device id
xamarin android device serial number
android q device id

How do I programmatically access the value shown in the image below ?

How to programmatically access the device serial number , Android: How to programmatically access the device serial number shown in the AVD manager (API Version 8) - android. Android: How to programmatically access the device serial number shown in the AVD manager(API Version 8) (2) This is the SERIAL of android.os.Build. Log.i("TAG","android.os.Build.SERIAL: " + Build.SERIAL); It's unique for any device.

Up to Android 7.1 (SDK 25)

Until Android 7.1 you will get it with:

Build.SERIAL
From Android 8 (SDK 26)

On Android 8 (SDK 26) and above, this field will return UNKNOWN and must be accessed with:

Build.getSerial()

which requires the dangerous permission android.permission.READ_PHONE_STATE.

From Android Q (SDK 29)

Since Android Q using Build.getSerial() gets a bit more complicated by requiring:

android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE (which can only be acquired by system apps), or for the calling package to be the device or profile owner and have the READ_PHONE_STATE permission. This means most apps won't be able to uses this feature. See the Android Q announcement from Google.

See Android SDK reference


Best Practice for Unique Device Identifier

If you just require a unique identifier, it's best to avoid using hardware identifiers as Google continuously tries to make it harder to access them for privacy reasons. You could just generate a UUID.randomUUID().toString(); and save it the first time it needs to be accessed in e.g. shared preferences. Alternatively you could use ANDROID_ID which is a 8 byte long hex string unique to the device, user and (only Android 8+) app installation. For more info on that topic, see Best practices for unique identifiers.

Privacy changes in Android 10, Android: How to programmatically access the device serial number shown in the AVD manager (API Version 8). android serial-number. How do I  Launch the Android Emulator without first running an app. To start the emulator: Open the AVD Manager. Double-click an AVD, or click Run. The Android Emulator loads. While the emulator is running, you can run Android Studio projects and choose the emulator as the target device.

Build.SERIAL can be empty or sometimes return a different value (proof 1, proof 2) than what you can see in your device's settings.

If you want a more complete and robust solution, I've compiled every possible solution I could found in a single gist. Here's a simplified version of it :

public static String getSerialNumber() {
    String serialNumber;

    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);

        serialNumber = (String) get.invoke(c, "gsm.sn1");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "ril.serialnumber");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "ro.serialno");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "sys.serialnumber");
        if (serialNumber.equals(""))
            serialNumber = Build.SERIAL;

        // If none of the methods above worked
        if (serialNumber.equals(""))
            serialNumber = null;
    } catch (Exception e) {
        e.printStackTrace();
        serialNumber = null;
    }

    return serialNumber;
}

I try to update the gist regularly whenever I can test on a new device or Android version. Contributions are welcome too.

Build, External storage access scoped to app files and media; Access to device location in the Android 10 (API level 29) introduces a number of features and behavior SDK version and its defined permissions, as shown in the following table: read the serial number until the user has granted your app permission to access​  The imei number is used to manage and identify the mobile phone. On android app we can easily get the current mobile device imei number using TelephonyManager class. So here is the complete step by step tutorial for Find / Get imei number in android programmatically. How to Find / Get imei number in android programmatically.

Android 8.0 Behavior Changes, This field was deprecated in API level 26. Use getSerial() instead. * @apiSince 9. A hardware serial number, if available. Alphanumeric only, case-insensitive. This​  Android 9 (API level 28) introduces a number of changes to the Android system. The following behavior changes apply exclusively to apps that are targeting API level 28 or higher. Apps that set targetSdkVersion to API level 28 or higher must modify their apps to support these behaviors properly, where applicable to the app.

Best practices for unique identifiers, As one of the changes that Android 8.0 (API level 26) introduces to improve In addition, to improve device performance, the system limits certain now have limits on how freely they can access background services. Apps needing to know the hardware serial number should instead use the new Build. Detect android phone is connected to WiFi or Mobile data Network programmatically. Android SQLite Insert Update Delete Display data example tutorial with source code download. Enable Disable WiFi programmatically in android on button click. Check internet connection in android programmatically.

DevicePolicyManager getParentProfileInstance, Play Install Referrer API Android 10 (API level 29) adds restrictions for non-​resettable identifiers, which include both IMEI and serial number. from the Google Analytics SDK, make sure to review and adhere to all applicable so that multiple devices with the same model appear for every timestamp. In addition to installing an app through Android Studio or the emulator UI, you can install your app on a virtual device by using the adb utility. To install an app by using adb, and then run and test the app, follow these general steps: Build and package your app into an APK as described in Build and Run Your App.

Comments
  • possible duplicate of How to find serial number of Android device?
  • Why do you say that it changes on factory reset? I know that is true for Settings.Secure.ANDROID_ID, but I hadn't heard about that for Build.Serial.
  • Tom you are right! I mixed up ANDROID_ID and SERIAL. I edited my answer.
  • Is this usually the same serial number that the manufacturer physically prints on the device itself or is it a software-only serial number?
  • Is it change in future anyhow?
  • this answer should be edited as with Android 8 (API26) and above the Build.SERIAL will not work. The only way to get the serial number is to call method Build.getSerial() that requires READ_PHONE_STATE permissions
  • Can the downvoter please explain, why he/she believes this is incorrect?
  • I voted down probably by mistake, this is clearly the correct answer now. I can not remove my downvote because it is locked until the answer is edited.
  • if the phone is reset to factory, or escape privilege, the ID get from UUID.randomUUID().toString() and ANDROID_ID will change?
  • UUID.randomUUID().toString() is always differnet -> thats why you have to persist, but factory reset of course deletes it. ANDROID_ID is not really defined, but expect it to be also different.
  • it finally solves my problem, the accepted answer should be this
  • I think this is the best answer here. As note, if I am using this code in my android studio and ran 2 instances of Simulator, it will return same ID.