可以在同一个 APK 中使用两个包吗?

可以在同一个 APK 中使用两个包吗?

问题描述:

我正在尝试实施这项伟大的技术,在两个不同的应用程序之间共享首选项,同时仍然是 MODE_PRIVATE.

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);

必须替换为:

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

但这要求真正添加 com.app.shared,这会转化为第二个 APK?

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

换句话说,每个 APK 只有一个 AndroidManifest.xml 并且只有一个 每个清单文件.因此每个 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?

我不希望每个应用程序有 2 个 APK,我只希望每个应用程序有一个 APK.

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

有没有办法做到这一点?

Is there a way to achieve this?

我正在尝试实现这种在两个不同应用程序之间共享首选项的伟大技术,同时仍然是 MODE_PRIVATE.

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

其实就是这么反大的技巧,我刚刚删了我的回答.使用 android:sharedUserId 是有风险的,对于已经部署的应用程序来说是完全不可能的.自从我在那里发布我的答案后的两年里,我意识到了我的方式错误.

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.

此外,该技术不能可靠地工作,因为您无法控制用户安装和卸载哪些应用程序.如果 App A 和 App B 都通过使用 App A 的副本共享首选项,那么当用户卸载 App A 时,App B 就会被冲洗掉.

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.

因此每个 APK 只有一个实际包?

Therefore only one actual package per APK?

正确.

我不希望每个应用程序有 2 个 APK,我只希望每个应用程序有一个 APK.

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

对于两个应用程序,您有两个 APK 文件,每个文件都有自己的包名.

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