Source:
static boolean upgradePrefs(Context context, String sourcePkgName, String targetPkgName) { try { if (sourcePkgName == null) { sourcePkgName = Constants.PACKAGE_NAME; try { sourcePkgName = Constants.class.getPackage().getName(); } catch (Exception e) { } } if (targetPkgName == null) { targetPkgName = Constants.PACKAGE_NAME; } if (targetPkgName.equals(sourcePkgName)) { return false; } String sourcePrefsName = sourcePkgName + "." + context.getPackageName(); SharedPreferences source = context.getSharedPreferences(sourcePrefsName, Context.MODE_PRIVATE); if (source.getAll().size() == 0) { return false; } String prefsName = targetPkgName + "." + context.getPackageName(); SharedPreferences targetPrefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE); SharedPreferences.Editor target = targetPrefs.edit(); if (source.contains(sourcePkgName + ".previousSessionId")) { target.putLong(Constants.PREFKEY_PREVIOUS_SESSION_ID, source.getLong(sourcePkgName + ".previousSessionId", -1)); } if (source.contains(sourcePkgName + ".deviceId")) { String deviceId = source.getString(sourcePkgName + ".deviceId", null); if (!TextUtils.isEmpty(deviceId)) { DatabaseHelper.getDatabaseHelper(context).insertOrReplaceKeyValue( DEVICE_ID_KEY, deviceId ); } } if (source.contains(sourcePkgName + ".userId")) { target.putString(Constants.PREFKEY_USER_ID, source.getString(sourcePkgName + ".userId", null)); } if (source.contains(sourcePkgName + ".optOut")) { target.putBoolean(Constants.PREFKEY_OPT_OUT, source.getBoolean(sourcePkgName + ".optOut", false)); } target.apply(); source.edit().clear().apply(); Log.i(TAG, "Upgraded shared preferences from " + sourcePrefsName + " to " + prefsName); return true; } catch (Exception e) { Log.e(TAG, "Error upgrading shared preferences", e); return false; } }}

Target:
static boolean upgradePrefs(Context context, String sourcePkgName, String targetPkgName) { try { if (sourcePkgName == null) { sourcePkgName = Constants.PACKAGE_NAME; try { sourcePkgName = Constants.class.getPackage().getName(); } catch (Exception e) { } } if (targetPkgName == null) { targetPkgName = Constants.PACKAGE_NAME; } if (targetPkgName.equals(sourcePkgName)) { return false; } String sourcePrefsName = sourcePkgName + "." + context.getPackageName(); SharedPreferences source = context.getSharedPreferences(sourcePrefsName, Context.MODE_PRIVATE); if (source.getAll().size() == 0) { return false; } String prefsName = targetPkgName + "." + context.getPackageName(); SharedPreferences targetPrefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE); SharedPreferences.Editor target = targetPrefs.edit(); if (source.contains(sourcePkgName + ".previousSessionId")) { target.putLong(Constants.PREFKEY_PREVIOUS_SESSION_ID, source.getLong(sourcePkgName + ".previousSessionId", -1)); } if (source.contains(sourcePkgName + ".deviceId")) { target.putString(Constants.PREFKEY_DEVICE_ID, source.getString(sourcePkgName + ".deviceId", null)); } if (source.contains(sourcePkgName + ".userId")) { target.putString(Constants.PREFKEY_USER_ID, source.getString(sourcePkgName + ".userId", null)); } if (source.contains(sourcePkgName + ".optOut")) { target.putBoolean(Constants.PREFKEY_OPT_OUT, source.getBoolean(sourcePkgName + ".optOut", false)); } target.apply(); source.edit().clear().apply(); Log.i(TAG, "Upgraded shared preferences from " + sourcePrefsName + " to " + prefsName); return true; } catch (Exception e) { Log.e(TAG, "Error upgrading shared preferences", e); return false; } }