Is it possible to save SharedPreferences files into custom dir? Let’s say into /data/data/package.name/my_prefs/.
Or is it possible to retrieve the directory SharedPreferences are saved by default to?
P.S. Hardcoding the path /data/data/package.name/shared_prefs/ is not the solution.
Yes.
This is basically the dataDir /shared_prefs which you can get from the ApplicationInfo object (which in turn you can get from the PackageManager). (Also, it might be the same as the “getFilesDir” dir you can get easily from Context itself? Not sure, didn’t check that.)
From the source, starting with Context.getSharedPreferences (ContextImpl source):
And “mPackageInfo” is an instance of LoadedApk:
And that brings us back to ApplicationInfo.
I’d say if you don’t want to rely on the convention /data/data/<package_name>/shared_prefs then it should be safe to get the “dataDir” and rely on “shared_prefs” from there?