0

I found a problem with Shared Preferences which happens only for some devices and only with Android 9 (Pie) o above.

To encrypt my database I create a key and store it to my sharedPreferences.

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String content = sharedPreferences.getString(-nameOfTheKey-, "");

if (content == null || content.isEmpty()){
    // Create and store the new key
    content = restoreApplicationKey(context);
}

As you can see, first I check for the availability then I create if not exist or empty. This key isn't created in any other place. That is the only point.

When I try to first install the app on my device, the condition return false because the key-value is already present in my SharedPreferences. This is impossibile!

I found online that the solution is to set allowBackup="false" into the Manifest and reinstall the app but I don't understand this behavior. How is it possible that the kay-value is pre setted?

Also, before setting allowBackup to false, I noticed that if forcing the initialization and uninstall the app, after the reinstall the key is still present but the value is changed. Every time with the same string but different from the saved one. All others key are fine, except this one.

As I said, the above code is the only entry point and it's called only at the app launch. Also the restoreApplicationKey(context) is called only at this point.

Lorenzo Vincenzi
  • 1,153
  • 1
  • 9
  • 26
  • allowBackup="true" allows your application to make a backup for the data stored by your application so if I set data locally and my phone have WIFI connection and charging so that data will be sent to Google Drive as backup, when I delete the application and reinstall it it will load the data again, disabling is the easy solution beside creating your own backup agent – Oussema Aroua Oct 01 '19 at 13:17
  • Ok, I know but the problem is that the key-value was setted with the first installation. This is what I don't understand – Lorenzo Vincenzi Oct 01 '19 at 13:23
  • with new device that never have installed the application in ? Go to your drive in the left side you will see backup click on it and you will see the backups of the application delete it and reinstall the application and retest, else check if the method is called somewhere with debugging or just logging inside the methode – Oussema Aroua Oct 01 '19 at 13:27
  • Yes, never installed on this device because it's from a colleague of mine who had never installed it. I have already checked in debug if the method is accessed from other parts. Also Android Studio confirm that the method is not invoked anywhere else – Lorenzo Vincenzi Oct 01 '19 at 13:49

0 Answers0