PlayerPrefs persist between installs on Android

I’ve been running into an extremely weird bug that loads old data into the PlayerPrefs on a fresh install.

The bug happens as follows:

  • A phone has my game installed

  • PlayerPrefs has the key “Animal” stored with the value “Dog”

  • I set the value of “Animal” to “Cat” and save the PlayerPrefs

  • I uninstall game from the phone

  • I install the game onto the phone again

  • Expected: PlayerPrefs does not contain the “Animal” key

  • Actual:* PlayerPrefs contains key “Animal” with the value set to “Dog”

  • This happens even if I manually clear the app cache and data before uninstalling.

This however only happens on a few devices and seems to be be fixed with a restart of those devices.

I am concerned that this will happen in production. Has anyone here encountered this before?

More Info:

  • I have confirmed that my code is not in any way populating the PlayerPrefs

  • I have seen this in the adb log-cat of affected devices:

But I am unsure if that has anything to do with this bug.

  • There have been 3 affected devices, two Nexus 6p (Android 7.0) and one Nexus 7 2013 (Android 6.0.1), a reboot fixed the issue in each case.

  • I am using Unity 5.4.1f and android sdk version: 23

  • This issue has never happened on iOS

  • I also tried to clear the cache of the Play Store app since it might store something for the game.

3 Likes

Edit: sorry I can’t read… My response did not address your problem.

Deleted response.

1 Like

I am having the exact same issue as OP (for Android at least). One of my test devices persists the playerprefs on uninstall/reinstall (Samsung S7), all other devices do not do this.

Please help!

1 Like

Hello, you’ve said that it seems to be be fixed with a restart of those devices. Does it mean that once you restart the device, the issue is not reproducible on it in any way?

@Sir-Thorgeir : awesome issue description - could you also submit a bug report (with attached project)?

1 Like

You might want to hold on the bug submission. I’m sure it won’t happen with Unity 5.5 anyway (once it’s released). I’m still curious if restarting the device fully solves the issue.

JuliusM, yes it appears that the issue did go away after a reboot of the device when I deployed from my local machine.

However I have launched my game and seen this happen on a apk from the play store. In that case it was not enough to restart the device, the issue persisted.

Thanks for the interest :wink:

Well, this happens because on the first start of the new app we copy data from internal path to external. This was done to avoid regressions when we’ve started to return external path instead of internal on devices with KitKat and newer Android versions.

You could solve this issue by clearing the internal persistent data path when your app is started. You can get the path like this:

string GetInternalPath()
{
    AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");

    return activity.Call<AndroidJavaObject>("getFilesDir").Call<string>("getPath");
}

As I’ve said before, this should not happen with Unity 5.5 anymore, because we won’t be copying data from internal to external paths anymore.

Hope this helps.

1 Like

JuliusM, this happens to me and I am using Unity 5.5.0f3. My test device is an Nexus 5X with Android 7.1.1.

DarkBloodStudios, I’ve just tried to reproduce this with the Nexus 5X device, with steps provided in the original post, but the issue did not happen for me. Could you submit a bug report and provide a minimal project as well as detailed reproduction steps? Please specify which Unity version do you use when you install the app for the first time and which Unity version do you use when you reinstall the app? When you’ll submit the bug, please paste the case number (it’s 6 numbers) here as well.

1 Like

I was having this error with 5.4.1 version and after i saw this thread i have updated Unity to 5.5.0.f3. I have deleted the application from my device and built again.
And i can confirm this problem persists.
Still the playerprefs are getting persisted between reinstalls. @JuliusM

I dont think it is a device problem by the way. One of the device is S7 Edge and other one is Samsung J1 Ace (5.1.1)

Any update on this?
5.5.2p3 and its still happening.
Consistently on Google Pixel.

I have seen this before, where the data is getting pushed to the cloud automatically via the OS and persists there.
You may want to check that your data isn’t being sync’d to w/e cloud service is used by the device.

Still nothing?

No - I haven’t seen a bug report coming to us.

Hi, we are having the same issue only on this two devices: Samsung S6 and S7, both with Android 7.0. We are using Unity 5.5.2f1. As additional info, this happens only when we export the app from Unity Pro, the issue doesn’t appear if it is exported from the Personal Edition.

Although it is not directly related to this post, i would like to mention that we were having issues with the persistentDataPath, due to it returned different paths everytime, sometimes the external, other times the internal path, so we had to use reflexion instead, to point to the internal path (the same code you are using here), where we are currently saving all the data.

Folks, I didn’t see a bug report so far. Sorry we can’t fix anything without knowing what’s wrong. Can someone please report a bug and attach the project? Thanks!

2 Likes

This is still happening in 2017.2.0p2 on Android / Google Pixel. Doesn’t seem to matter how many times I uninstall/reinstall it it’s always pulling in the same, ancient copy of a player prefs string. Uninstalling and restarting the device also doesn’t fix this.

Have submitted a bug report with my project, case 991544.

2 Likes

i am on unity 5.6.4 , but on some devices this issue is still there