如何在卸载/重装非Play商店版应用时保留偏好设置?
Hey there! Let's tackle your two Android preference persistence issues with practical, actionable solutions:
When an app is uninstalled, Android wipes all its private data (including default SharedPreferences and internal storage files). To get around this, you need to store preferences outside the app's private sandbox:
External Storage (Public Directory)
Save your preference data to a public folder on the device's external storage (e.g.,/sdcard/YourAppName/preferences.json). This folder won't be deleted when the app is uninstalled.
Example approach:- Serialize your preference data (using Gson, Moshi, or even plain JSON) into a file.
- Request the necessary storage permissions (for Android 10+, you'll need
MANAGE_EXTERNAL_STORAGEif using public directories; for older versions,WRITE_EXTERNAL_STORAGE). - On app launch, check if the file exists in the public directory and load the data from it.
Cloud Sync
Implement a cloud backup system using a backend service (like Firebase Realtime Database, your own API, or even a simple cloud storage bucket). Require users to create an account (or use anonymous auth) to sync their preferences to the cloud. When they reinstall the app, they can log in and restore their settings from the cloud. This is the most reliable method for cross-device persistence too.Third-Party Cloud Storage SDKs
Let users back up preferences to services they already use, like Dropbox or OneDrive. Integrate their SDKs to save/load preference files directly to the user's cloud storage. This avoids building your own backend but relies on the user having an account with that service.
The issue here is that default SharedPreferences and internal storage are tied to the app's private data, which gets cleared during a fresh install (even if it's a newer version from a non-Play source). Here's how to fix it:
Store Preferences in External Storage
Same as the first solution—use a public external directory or a non-deletable external path to save your preferences. When you install the new version, the app can read the existing preference file from external storage and apply the settings automatically.
Pro tip: Use a unique filename (likeyour_app_unique_id_preferences.xml) to avoid conflicts with other apps.Custom SharedPreferences Path
Instead of using the default privateSharedPreferenceslocation, create aSharedPreferencesinstance pointing to a file in external storage. Example code (Java):File prefsDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "YourApp"); if (!prefsDir.exists()) { prefsDir.mkdirs(); } File prefsFile = new File(prefsDir, "app_preferences.xml"); SharedPreferences customPrefs = getSharedPreferences(prefsFile.getAbsolutePath(), Context.MODE_PRIVATE);Note: For Android 11+, you'll need to handle scoped storage rules or request the
MANAGE_EXTERNAL_STORAGEpermission to access public directories.SQLite Database in External Storage
If your preferences are complex (not just key-value pairs), store them in an SQLite database located in external storage. The database file will survive reinstalls, and you can query it on app launch to restore settings.
Important Notes
- Always encrypt sensitive preference data when storing it in external storage—use libraries like Android Keystore to encrypt/decrypt values.
- Make sure to handle permission gracefully: if the user denies storage access, provide a fallback (like default settings) and explain why the permission is needed.
- For cloud sync, handle offline scenarios by caching preferences locally first, then syncing when connectivity is restored.
内容的提问来源于stack exchange,提问作者Gajanand Swamy

