Skip to content

fix(android): Replace deprecated SharedPreferences API - #23693

Merged
Xiaoy312 merged 1 commit into
feature/breakingchangesfrom
dev/xygu/20260708/bc55-android-sharedprefs
Jul 10, 2026
Merged

Xiaoy312 merged 1 commit into
feature/breakingchangesfrom
dev/xygu/20260708/bc55-android-sharedprefs

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #1833

PR Type:

🐞 Bugfix

What changed? 🚀

On Android, ApplicationDataContainer backs ApplicationData.Current.LocalSettings / RoamingSettings with Android SharedPreferences. It obtained the store via Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(context), which has been deprecated since API 29 (Android 10) — issue #1833.

This replaces that call with the explicit, non-deprecated equivalent:

private ISharedPreferences CreatePreferences()
{
    var context = ApplicationData.GetAndroidAppContext();
    return context.GetSharedPreferences(context.PackageName + "_preferences", FileCreationMode.Private)!;
}

This is byte-for-byte the store the deprecated API resolves to internally — Android's getDefaultSharedPreferences(context) is implemented as context.getSharedPreferences(context.getPackageName() + "_preferences", MODE_PRIVATE). Consequences:

  • Data is preserved. The on-disk backing file (<package>_preferences.xml) and all keys are unchanged, so existing users' persisted settings survive an app upgrade — no silent orphaning.
  • Both LocalSettings and RoamingSettings are covered, since both resolve to the same SharedPreferencesPropertySet.
  • No new dependency. The AndroidX.Preference alternative was intentionally avoided; the explicit call needs nothing beyond Android.Content (already imported).

The now-unused using Android.Preferences; was removed.

Validation:

  • Compile: Uno.UWP net10.0-android target builds clean — 0 warnings, 0 errors.
  • Runtime: the existing (non-gated) Given_ApplicationDataContainer runtime-test suite exercises set/get/remove/enumerate through the changed SharedPreferencesPropertySet on native Android. No new test was added — the swap is a framework-contract equivalence, so continuity does not require a dedicated test.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable) — covered by existing Given_ApplicationDataContainer; no new test needed for a like-for-like API swap.
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features) — n/a, internal implementation only.
  • 🖼️ Validated PR Screenshots Compare Test Run results. — n/a, no visual change.
  • ❗ Contains NO breaking changes — CreatePreferences() is a private implementation detail; no public API surface changes.
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

Android.Preferences.PreferenceManager.GetDefaultSharedPreferences is
deprecated since API 29. Replace it with the explicit
Context.GetSharedPreferences(<package>_preferences, MODE_PRIVATE), which
is byte-for-byte the store the deprecated API resolves to internally, so
existing users' persisted LocalSettings/RoamingSettings are preserved on
upgrade. Adds no new dependency.

fixes #1833

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions github-actions Bot added the platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform label Jul 8, 2026
@unodevops

Copy link
Copy Markdown
Contributor

@mergify

mergify Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@MartinZikmund

MartinZikmund commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

@Xiaoy312 For existing apps, will preserving already existing preferences require a migration? If yes, we should some common approach, as I have a very similar change for iOS as well - #23705 . Currently that PR migrates automatically on start, but I think we might want to have some helper API users can use when they know they are going to migrate existing pre-7 apps

@Xiaoy312

Xiaoy312 commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Nope, there is nothing to do/change on the consumer side. We just shuffled the api used, but they are still pointing to the same physical file.
Both approach returned the same ISharedPreferences (same @hash suffix) and identical keys/values.

@Xiaoy312
Xiaoy312 merged commit 9569e3e into feature/breakingchanges Jul 10, 2026
47 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20260708/bc55-android-sharedprefs branch July 10, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants