How to persist data between app installs on Android?
Asked Answered
B

1

6

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

Requirements:

  • Uniquely identify device across app install/uninstall sessions.

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.

Bunghole answered 4/12, 2015 at 12:32 Comment(3)
"iOS has keychain that can be used for this kind of stuff but I didn't find Android's equivalent of it" -- anything you put in the keystore will be removed when your app is uninstalled. "This "somewhere somehow" part is the mystery. Solutions like storing onto the SD card or Cloud are not an option" -- there is no place other than external storage where you can place files that will survive an uninstall, and the user can get rid of files from external storage if the user so chooses.Tanberg
Just to note that iOS preservers its data in keychain even if app is uninstalled and later on installed. I wonder if I should go the (1) road with Android.Bunghole
You can use the Android device id to create a session. The device id is unique for every phone and won't change. This way you can save the session as long as the user doesn't change his device.Vala
V
7

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.

Velma answered 4/12, 2015 at 12:56 Comment(1)
Note to present readers - avoid using this (and HARDWARE_ID) in general, and simply generate a UUID that you will persist on your client/server.Velma

© 2022 - 2024 — McMap. All rights reserved.