How can I share a SharedPreferences file across two different android apps?

It is better to set private mode for the file. App needs to be signed with same set of certificates to share this file.

Set sharedUserId in both apps to be same.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" 
android:sharedUserId="com.example">
....

Get Context from other package:

mContext = context.createPackageContext(
                        "com.example.otherapp",
                        Context.MODE_PRIVATE);
 mPrefs = mContext.getSharedPreferences("sameFileNameHere", Activity.MODE_PRIVATE);

Get items as usual from SharedPreference. You can access it now.

Leave a Comment