androidandroid-identifiers

How to persist data between app installs on Android?


I thought there would be some straight forward solution to this.

Requirements:

Options:

  1. Use some kind of Android's device-identifier-API each time when needed (read it every time from Android's API). According to Identififying-app-installations blog post this is not recommended and not reliable solution.

  2. Generate UUID once (on first app start) and persist it somewhere somehow that it would be preserved across multiple app installs/uninstalls. This "somewhere somehow" part is the mystery. Solutions like storing onto the SD card or Cloud are not an option. iOS has keychain that can be used for this kind of stuff but I didn't find Android's equivalent of it.

What are my other options here? I prefer going the (2) route because of my server implementation (server is generating UUID for the first time if not present). But if it is not an option, I can fallback to (1) and modify the server.

Thanks.


Solution

  • To uniquely identify an application between application installs/reinstalls, you need to get it's hardware ID and use that as your credentials/key.

    To fetch the hardwareID, you can use the following method:

    public static String getHardwareId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
    

    it's partially equivallent to a UUID with the following exception: The value may change if a factory reset is performed on the device.

    The reason why i call it "partially equivallent" is this: The HardwareID is a 64-bit number (as a hex string) that is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device. The value may change if a factory reset is performed on the device. Note: When a device has multiple users (available on certain devices running Android 4.2 or higher), each user appears as a completely separate device, so the ANDROID_ID value is unique to each user.

    But this hits the #2 problem: where and how to store it; storing it in SharedPreferences is useless as that is wiped if the app is uninstalled. Same for /data/data/your.package.name/my_stored_keys folder as that one gets deleted from the phone during uninstallation as well.

    You will need to save it server-side if you wish to persist between uninstalling and reinstalling the app.