📜 ⬆️ ⬇️

Application Tracking

In the official Google group dedicated to Android, developers sometimes complain that it is difficult to find unique, reliable and stable identifiers for Android devices.
A small topic was posted on the android-developers blog on how to track individual application installations. I think someone will be useful.


Tracking Installations


There are many reasons why developers want to keep track of individual installations of their applications. Some simply call the TelephonyManager.getDeviceId () method and use this value to identify a particular installation. There are some problems with this: Firstly, the uniqueness of the ID is not guaranteed. Secondly, even if it works, the value can survive the so-called “Factory Reset” (reset all settings to the initial ones), which ultimately can lead to an unpleasant error if one of the buyers / installers clears their device, and then sends him to someone else.

To track the settings, you can use the UUID as an identifier, and then simply create a new one each time the first time you start the application. Below is a sketch of the “Installation” class with one static method, Installation.id (Context Context) . Then everything depends on your imagination - you can add various device-specific information to the INSTALLATION file.
')
public class Installation { private static String sID = null; private static final String INSTALLATION = "INSTALLATION"; public synchronized static String id(Context context) { if (sID == null) { File installation = new File(context.getFilesDir(), INSTALLATION); try { if (!installation.exists()) writeInstallationFile(installation); sID = readInstallationFile(installation); } catch (Exception e) { throw new RuntimeException(e); } } return sID; } private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); } private static void writeInstallationFile(File installation) throws IOException { FileOutputStream out = new FileOutputStream(installation); String id = UUID.randomUUID().toString(); out.write(id.getBytes()); out.close(); } } 


Device identification


Imagine that you want to get the unique identifier of a separate device. Anyway, this task is not easy.

Earlier, when every Android device was a phone, everything was simple: the TelephonyManager.getDeviceId () method returned IMEI , MEID , or ESN of the phone, which was guaranteed to be unique.
However, with this approach a number of problems arise:

Mac address


You can try to return the mac address for devices that have Bluetooth or Wi-Fi. This is also not recommended, because, firstly, not all devices have Wi-Fi. Secondly, if the Wi-Fi module is not turned on, it will not be possible to get the mac address.

Serial number


From the version of Android 2.3 (Gingerbread) you can get the serial number through android.os.Build.SERIAL . For versions below 2.3, lopatoid described the method in the comments .

ANDROID_ID


More precisely, it is Settings.Secure.ANDROID_ID . This is a 64-bit value that is generated when the device is first loaded, it is reset when the device is cleared (Factory Reset, etc.)

In principle, ANDROID_ID is well suited for device identification. However, it has some drawbacks: firstly, it is not 100% reliable on Android versions up to 2.2 (Froyo). Secondly, there is one well-known bug of one of the phone manufacturers, due to which the same ANDROID_ID was generated for all devices.

Conclusion


For most applications, the key is to uniquely identify the individual installation, not the device. Fortunately, it is not difficult.

There are many reasons to avoid identifying individual devices. For those who still want to try, ANDROID_ID may be the best choice, with a small heuristic for outdated devices.

Source: https://habr.com/ru/post/116719/


All Articles