Unity PlayerPrefs does not match Android SharedPreferences

We recently encountered a problem. Currently we have two projects using an SDK. One of the projects is Android, and the other is Unity. We are trying to upgrade our users from our native Android app to the Unity version of the app.

The Android project is live and we were upgrading to the Unity project. The problem we have is that users who had the Android project and are moving to Unity have different user ids (although it should be the same), so they are showing up as new users. In the Android project we save the user ID in SharedPreferences. When we tried to retrieve that user ID from PlayerPrefs we did not find any key.

Searching the forums the closest we found is this:

From the Unity documentation:

On Android data is stored (persisted) on the device. The data is saved in SharedPreferences. C#/JavaScript, Android Java and Native code can all access the PlayerPrefs data. The PlayerPrefs data is physically stored in /data/data/pkg-name/shared_prefs/pkg-name.xml.

But he solves it by finding the userid in his Android build. How do we do the opposite (Access the correct user ID from SharedPreferences using Unity’s PlayerPrefs). The problem is we don’t want to push an update to our native Android build, and then have the users update to our Unity build. It seems PlayerPrefs does not match.

Thanks!

I’m guessing pkg-name and pkg-name.xml are not identical to what the Android project used.

You’re probably going to need to make a plugin to do this.

https://stackoverflow.com/questions/27682977/using-shared-preferences-between-unity-and-native-android-sdk

Both use the same package name as they both upload to the same Google Play application; so I assume that isn’t the problem, but thank you for trying to help.

I was able to use ADB shell to discover what the problem is (and I guess I’ll need to write a plug in for this). Joe-Censored is mostly correct.

Unity is looking in:
/data/data/com../shared_prefs/com...xml
but the SDK saved the SharedPrefs to
/data/data/com../shared_prefs/sdk_prefs.xml

Any idea how to write a plugin to grab the prefs from sdk_prefs and then store that in com...xml (PlayerPrefs)? I’m looking into the plugin links that Joe_Censored put now.

Here is my code:

package com.**.**;

import android.content.Context;

public class SDKUserIdUtil
{
    private Context context;
    private static SDKUserIdUtil instance;

    public static final String PREFS_NAME = "sdk_prefs";


    public SDKUserIdUtil() {
        this.instance = this;
    }

    public static SDKUserIdUtil instance() {
        if(instance == null) {
            instance = new SDKUserIdUtil();
        }
        return instance;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getPreferenceString (String prefKey) {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
        String userId = preferences.getString(prefKey, "");
        return userId;
    }
}