SoFunction
Updated on 2025-04-10

How to get a unique device ID on Android

1. Let’s briefly summarize the disadvantages of several common solutions:

1. IMEI

Android 10 official states that third-party applications cannot obtain IMEI code:Privacy Changes in Android 10

For versions below Android 10, you need to apply for READ_PHONE_STATE permission.

2. Android ID

Android ID does not have true uniqueness.

ROOT, flashing, factory reset, applications with different signatures, etc. will cause the obtained Android ID to change.

And the bugs of the system customized by different manufacturers will cause different devices to produce the same Android ID.

3. MAC address

In Android 10, MAC addresses have the characteristics of randomization:Privacy Change in Android 10—MAC Address

Although most mobile phones do not support this feature at present, as manufacturers follow up, this solution will gradually become invalid

When the logos provided by these devices do not meet the needs, we need to adopt another method.

2. uuid + local files to implement a general solution

1. Ideas

When starting the APP, check and read the file with uuid saved in the root directory. If the file is not available, it is considered a new device, create the file and write it to uuid.

And make sure that when uninstalling the application, the file will not be deleted by the system (this is why it is created in the root directory).

2. Solve the problem of accessing SDK permissions on mobile phones

Android 6 below, add permissions:

<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".READ_EXTERNAL_STORAGE" />

Android 6 and above need to apply for dynamic permissions on this basis.

Android 10 and above, the file storage mechanism has been modified into sandbox mode, that is, the application can only access files and public media files under its own sandbox.

"Benefits from the sandbox mechanism, the files created by the application belong to their own sandbox, and they will also be deleted when the application is uninstalled.

Currently, the following line of code can be added to solve the sandbox problem:

<application
  ...
  android:requestLegacyExternalStorage="true">

This way we can create our own files in the "root directory".

3. Adapt to Android 11

Android 11 will enforce sandbox mode. Before this, storage permissions can be simply divided into "prohibit" and "allow", and after that, storage permissions can be simply divided into "prohibit", "allow access to media files" and "allow access to all files".

"Allow access to media files" is something that most applications can apply for, while "Allow access to all files" can only be applied for by file management applications. If you are not this type of application but apply for this permission, you will not be able to pass the review of Google Play.

Allow access to all files: .MANAGE_EXTERNAL_STORAGE

Having introduced this, a solution has actually come out: directly apply for "allow access to all files" permission, the consequence is that it cannot pass the review of Google Play.

There is another way: we will not upgrade the SDK for the time being, and develop applications for Android 10 (SDK 29). In this way, due to the "backward compatibility mechanism", our applications can run normally on the Android 11 system.

4. Flutter Code Practice

import 'dart:io';
import 'package:uuid/';

// Local persistent storage uuid code practiceclass Storage {
 static File file;

 // Entrance static Future&lt;String&gt; init() async {
  bool boolCreateFile = await createFile();
  if (boolCreateFile) {
   String uuid = await readData();
   return uuid;
  } else {
   await writeData();
   String uuid = await readData();
   return uuid;
  }
 }

 // Create a file static Future&lt;bool&gt; createFile() async {
  file = File('/storage/emulated/0/'); // Point to the file uuid in the root directory  bool exists = await ();
  return exists;
 }

 // Write data static writeData() async {
  // If the file exists, the original content will be overwritten. If it does not exist, the file will be created  String uuid = await getUuid();
  ('$uuid');
 }

 // Read the file static Future&lt;String&gt; readData() async {
  try {
   String uuid = await ();
   return uuid;
  } catch (e) {
   return null;
  }
 }

 // Get uuid, plugin used: uuid static Future&lt;String&gt; getUuid() async {
  Uuid uuidObj = Uuid();
  String uuid = uuidObj.v1();
  return uuid;
 }
}

The above is the detailed content of how Android obtains the unique device identification. For more information about Android obtaining the unique device identification, please follow my other related articles!