Android 自定义权限与应用签名

Android custom permission vs app signature

我已经阅读了关于这两个主题(自定义权限和应用程序签名)的可用文档和 SO 帖子,我的理解是:

  1. 使用相同密钥签名的应用可以使用签名级权限
  2. 自定义权限只能在一处定义,需要先安装声明该权限的应用
  3. 如果应用共享相同的签名,可以在多个应用中定义自定义权限

现在我需要在一个应用程序中定义一些组件,我将在另一个使用相同密钥签名的应用程序中使用这些组件,但是根据 this 我不需要定义自定义权限并且可以放置签名检查

If the functionality is only available to apps signed with the same signature as the providing app, you may be able to avoid defining custom permissions by using signature checks. When one of your apps makes a request of another of your apps, the second app can verify that both apps are signed with the same certificate before complying with the request.

我不明白签名检查是什么意思,有没有办法在该组件上放置清单检查,或者我是否必须以编程方式检查它?如果是后者,在两个应用程序中定义自定义权限不是更好吗(否定安装顺序问题)?

is there a way to place a check in manifest on that component

不,抱歉。

do I have to check it programmatically?

是的。它所依赖的 This pair of old projects demonstrate a bound service and a client that checks the signature of the service. Those projects are covered in a chapter in a chapter of this free old book. That sample has problems in modern Android, as an app can have multiple signatures. But, the SiganturesUtil class 展示了如何从 PackageManager 获取已安装应用程序的签名。然后,您可以将它们与预期值进行比较,以确定您是否确实在与您预期的应用对话。

If latter, isn't it better to define a custom permission in both apps instead (to negate installation order issue)?

签名检查更加灵活——它们不限于由同一个签名密钥签名的两个应用程序。这一点,加上自定义权限的历史问题,可能就是 Google 建议对该页面上的自定义权限进行签名检查的原因。