0

I recently started learning Android to port my iOS app to Android.

User registration in my app is optional, hence the user can decide to get started right away without any delay. However I still need a form of identification against fraud and if the user has passed the first week of free trial. Otherwise the user can just keep deleting and reinstalling the app to use it for free, forever.

On iOS I have solved the problem through the keychain. Any values stored in there remain there even after the app has been uninstalled.

import KeychainAccess

let keychain = Keychain(service: Constants.keychainServiceID).synchronizable(false).accessibility(.alwaysThisDeviceOnly).accessibility(.alwaysThisDeviceOnly)
let deviceId = UUID().uuidString.lowercased()
keychain["DEVICE_ID"] = deviceId

I don't seem to find anything like that on Android.

A unique Device ID doesn't seem to be available either based on this answer

Hence is there a way to achieve this or do I have to make user registration mandatory?

UPDATE:

In iOS when a keyChain value is set, the user can't ever update or delete it. Even after the app is uninstalled. During unit testing I can delete the keychain entry like this:

let keychain = Keychain(service: Constants.keychainServiceID).synchronizable(false).accessibility(.alwaysThisDeviceOnly)
do {
    try keychain.remove("DEVICE_ID")
} catch let error {
    print("error: \(error)")
}

Can I do that in Android?

Houman
  • 64,245
  • 87
  • 278
  • 460
  • Have a look at https://developer.android.com/training/articles/user-data-ids – OMi Shah Sep 20 '20 at 08:40
  • Thank you, that's useful. However every suggestion they make is reset-able by the user, and not what I was looking for. – Houman Sep 20 '20 at 08:55

1 Answers1

0

More easily you can do like this, this is a wrapper on shared preference. https://github.com/kishandonga/EasyPrefs

public static void setUniqueId(){
    if(!Prefs.read().content("is_unique_id_set", false)){
        String id = UUID.randomUUID().toString();
        Prefs.write().content("unique_id", id)
                .content("is_unique_id_set", true)
                .commit();
    }
}

public static String getUniqueId(){
    return Prefs.read().content("unique_id", "");
}

Cases when unique id changed

  1. App uninstalled and then reinstalled
  2. Clear cache or reset
Kishan Donga
  • 2,851
  • 2
  • 23
  • 35
  • Sorry, not sure if I follow. If I install the app and run `getUniqueId()`. Then I uninstall it and install it again and then do `getUniqueId()`. Are both GUIDs the same or are they different? – Houman Sep 21 '20 at 13:53
  • setUniqueId() this method called when your app opens like in the splash screen and when you need this unique id you can use the getUniqueId() method for example pass this id to the server. – Kishan Donga Sep 21 '20 at 14:50
  • What happens when I delete app and reinstall it? Do I get the same value as before when calling getUniqueId()? – Houman Sep 21 '20 at 19:31
  • It is working the same as the firebase token when you delete the app and reinstall then value changes similar to the token changed in firebase. – Kishan Donga Sep 22 '20 at 04:33
  • Well, then it's not what I asked for, correct? My requirement is that the ID doesn't change after a reinstall. – Houman Oct 03 '20 at 06:23
  • for the same you can check this answer https://stackoverflow.com/a/2785493/6370015 and replace String id = UUID.randomUUID().toString(); with Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); this will give you not changable id. – Kishan Donga Oct 03 '20 at 09:40