2

I am trying to implement this great technique of sharing preferences across two different applications while still MODE_PRIVATE.

My problem is that the first application cannot assume that the first application is installed (and vice versa) and so:

 Context c = createPackageContext("com.app.first", MODE_PRIVATE);

Must be replaced by:

 Context c = createPackageContext("com.app.shared", MODE_PRIVATE);

But that mandates adding that com.app.shared for real, which translates into a second APK?

In other words, there is only one AndroidManifest.xml per APK and only one <manifest package= > per manifest file. Therefore only one actual package per APK?

I don't want 2 APKs per application, I only want a single APK per application.

Is there a way to achieve this?

Community
  • 1
  • 1
ef2011
  • 10,431
  • 12
  • 49
  • 67

1 Answers1

2

I am trying to implement this great technique of sharing preferences across two different applications while still MODE_PRIVATE.

Actually, that is such an anti-great technique that I just deleted my answer. Using android:sharedUserId is risky, and downright impossible for already-deployed apps. I learned the error of my ways in the two years since I had posted my answer there.

Also, the technique doesn't work reliably, because you have no control over which apps the user installs... and uninstalls. If App A and App B share preferences by means of both using App A's copy, then when the user uninstalls App A, App B is hosed.

Therefore only one actual package per APK?

Correct.

I don't want 2 APKs per application, I only want a single APK per application.

For two applications, you have two APK files, each with their own package name.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for saving me from a disastrous approach. I now realize that despite the "advertised" availability of "shared" preferences, it takes so many workarounds and tricks that this must be somewhat unsafe. I now believe that the right approach to share anything between applications is via [Content Providers](http://developer.android.com/guide/topics/providers/content-providers.html). Am I in the right direction? – ef2011 Aug 03 '12 at 23:06
  • 2
    @ef2011: There are many ways of sharing information between apps -- a `ContentProvider` is one possibility. Personally, in this case, I would send permission-secured broadcasts when an app's preferences change, to be picked up by your other app and applied to its own copy. That way, with both apps mirroring the settings, you don't have to worry about one app being uninstalled and taking both apps' settings with it. Also, you don't care which app gets installed first. And, as a side benefit, it seamlessly extends to three or more apps, should the need arise. – CommonsWare Aug 03 '12 at 23:22
  • Unfortunately, ContentProvider is not even a possibility, due to the [INSTALL_FAILED_CONFLICTING_PROVIDER](http://stackoverflow.com/q/6273592/722603) restriction (i.e. two different packages cannot share the same content provider). So, it seems that **the only** way to go is the "permission-secured broadcast" you suggested. Any link where I can learn more about this? – ef2011 Aug 07 '12 at 18:08