在 ContentProvider 中对 android:authorities 使用 @string

发布于 2024-11-16 16:51:34 字数 635 浏览 1 评论 0原文

我的清单中有一个 ContentProvider,当我使用硬编码字符串完全定义它们时,它就可以工作。例如,

<provider android:name="com.myprovider" android:authorities="com.myprovider"/>

工作完美,但是 ContentProviders 位于一个被多个项目使用的库中,并且我不希望权限冲突,所以我尝试执行以下操作。

<provider android:name="com.myprovider" android:authorities="@string/myProviderAuthority">

这样,我应该能够在单个 strings.xml 文件中定义所有权限,并且应用程序之间不会发生冲突,因为我应该能够使用每个应用程序资源覆盖系统来更改它们。

但是,当我尝试使用 @string 进行构建时,它似乎给出了格式错误的明显错误,并显示“提供商不包含(是的,它说包含)当局贡品”

我可以不使用资源字符串来表示当局贡品吗?每当我需要在两个地点保持恒定时,我都会感到不舒服。我们的质量保证部门很难发现权限冲突,我不希望事情变得不同步,否则可能会导致混乱。有人知道为什么我的代码不起作用吗?

I have a ContentProvider in my manifest, when I define them fully with hardcoded strings it works. E.g.

<provider android:name="com.myprovider" android:authorities="com.myprovider"/>

Works perfect, however the ContentProviders are in a library that gets used by multiple projects, and I don't want authority conflicts, so I attempted to do the following.

<provider android:name="com.myprovider" android:authorities="@string/myProviderAuthority">

This way I should be able to define all my authorities in a single strings.xml file and not have conflicts between apps since I should be able to change them using each apps resource override system.

However, it appears that when I try to build with @string, it gives me a malformed manifest error and says "Provider does not INCUDE (yes it says INCUDE) authorities tribute"

Can I not use a resource string for the authorities tribute, I feel sick everytime I need to maintain constants in two locations. Authority conflicts can be hard to catch by our QA dept, and I don't want things to become out of sync or it could cause confusion. Anybody have any ideas why my code isn't working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-11-23 16:51:34

我遇到了类似的问题,但是使用了 android:versionCode 属性。当我尝试在资源中定义版本代码并在清单中使用对其的引用时,Android Market 甚至禁止我发布该应用程序。事实证明,这种行为的原因相当简单。资源可能会根据当前配置而变化,并且该值在任何情况下都必须相同。

也许,这就是具有权威引用的内容提供者也不起作用的原因。在我看来,使用这样的引用并不是一个好主意,因为不能保证应用程序中的权限资源只有一个值。我知道您可以足够小心地保留此资源的单个实例,但没有对此进行特殊的编译器或系统检查,因此它不可信。

I faced a similar problem but with the android:versionCode attribute. When I tried to define the version code in resources and use a reference to it in the manifest Android Market even forbade me to publish the application. The reason of such behavior turned out to be rather simple. Resources can change depending on current configuration and this value have to be the same in any case.

Probably, this is the reason why content providers with authority references do not work too. And it seems to me that it's not a good idea to use such a reference because there's no guarantee that there will be the only one value for an authority resource in an app. I understand that you can be careful enough to keep a single instance of this resource but there's no special compiler or system checks for this so it cannot be trusted.

败给现实 2024-11-23 16:51:34

许多清单属性不能指定为对字符串的引用——它们必须指定为显式字符串值。

解析清单的代码位于:frameworks/base/core/java/android/content/pm/PackageParser.java。该类调用 getNonConfigurationString() 和 getNonResourceString()(在以下位置实现:frameworks/base/core/java/android/content/res/TypedArray.java)。

getNonConfigurationString() 将自身描述为:

Retrieve the string value of an attribute that is not allowed to change with the given configurations.

getNonResourceString() 将自身描述为:

Retrieve the string value for an attribute, but only if that string comes from an immediate value in an XML file.  That is, this does not allow references to string resources, string attributes, or conversions from other types.  As such, this method will only return strings that come from attributes in an XML file.

下面列出了 PackageParser 不允许从资源或不同配置中获取的清单属性。

这些属性在 com.android.internal.R.styleable 中定义manifest.xml 元素属性名称通常是正式名称中最后一个“_”之后的部分。例如,manifest.xml 中元素中的 android:authorities 属性为 AndroidManifestProvider_authorities 或 com.android.internal.R.styleable.AndroidManifestProvider_authorities。 (下面属性名列表中的数字是PackageParser.java 4.1.1版本中相关代码的行号)

getNonConfigurationString读取的属性:

917:  AndroidManifest_versionName
922:  AndroidManifest_sharedUserId 
2057: AndroidManifestActivity_parentActivityName
2071: AndroidManifestActivity_permission
2079: AndroidManifestActivity_taskAffinity
2247: AndroidManifestActivityAlias_targetActivity
2330: AndroidManifestActivityAlias_permission
2336: AndroidManifestActivityAlias_parentActivityName
1672: AndroidManifestApplication_name
1683: AndroidManifestApplication_manageSpaceActivity 
1697: AndroidManifestApplication_backupAgent 
1795: AndroidManifestApplication_permission 
1800: AndroidManifestApplication_taskAffinity
1815: AndroidManifestApplication_process
3005: AndroidManifestData_mimeType
3017: AndroidManifestData_scheme
3023: AndroidManifestData_host
3025: AndroidManifestData_port
3031: AndroidManifestData_path
3037: AndroidManifestData_pathPrefix
3043: AndroidManifestData_pathPattern
2527: AndroidManifestGrantUriPermission_path
2533: AndroidManifestGrantUriPermission_pathPrefix
2539: AndroidManifestGrantUriPermission_pathPattern
2579: AndroidManifestPathPermission_permission
2581: AndroidManifestPathPermission_readPermission
2586: AndroidManifestPathPermission_writePermission
2615: AndroidManifestPathPermission_path
2622: AndroidManifestPathPermission_pathPrefix
2629: AndroidManifestPathPermission_pathPattern
2434: AndroidManifestProvider_authorities
2441: AndroidManifestProvider_permission
2443: AndroidManifestProvider_readPermission
2454: AndroidManifestProvider_writePermission
2713: AndroidManifestService_permission
2832: AndroidManifestMetaData_name
1225: AndroidManifestOriginalPackage_name
1981: (parsePackageItemInfo -- I can't tell list of all names)
3258: (Component constructor args.nameres -- I can't tell list of all names)

getNonResourceString读取的属性:

1806: AndroidManifestApplication_taskAffinity
1821: AndroidManifestApplication_process
1632: AndroidManifestInstrumentation_targetPackage
2891: AndroidManifestPackageVerifier_name
2894: AndroidManifestPackageVerifier_publicKey
1512: AndroidManifestPermission_permissionGroup
1200: AndroidManifestProtectedBroadcast_name
1927: AndroidManifestUsesLibrary_name
1054: AndroidManifestUsesFeature_name
1004: AndroidManifestUsesPermission_name
3308: (Component constructor  args.processRes -- I can't tell list of all names)

所以,manifest.xml文件中的很多属性必须指定为显式字符串值(即在引号中),而不是对 strings.xml 中字符串的引用。

Many manifest attributes cannot be specified as a reference to a string -- they must be specified as explicit string values.

The code that parses the manifest is in: frameworks/base/core/java/android/content/pm/PackageParser.java. That class calls, among others, getNonConfigurationString() and getNonResourceString() (which are implemented in: frameworks/base/core/java/android/content/res/TypedArray.java).

getNonConfigurationString() describes itself as:

Retrieve the string value of an attribute that is not allowed to change with the given configurations.

getNonResourceString() describes itself as:

Retrieve the string value for an attribute, but only if that string comes from an immediate value in an XML file.  That is, this does not allow references to string resources, string attributes, or conversions from other types.  As such, this method will only return strings that come from attributes in an XML file.

The manifest attributes that PackageParser doesn't allow to be taken from resources or from different configurations are listed below.

These attributes are defined in com.android.internal.R.styleable The manifest.xml element attribute name is usually the part of the name after the last '_' in the formal name. For example, the android:authorities attribute in a element in manifest.xml is AndroidManifestProvider_authorities, or com.android.internal.R.styleable.AndroidManifestProvider_authorities. (The number in the lists of attribute names below are the line number of the relevant code in version 4.1.1 of PackageParser.java)

Attributes read by getNonConfigurationString:

917:  AndroidManifest_versionName
922:  AndroidManifest_sharedUserId 
2057: AndroidManifestActivity_parentActivityName
2071: AndroidManifestActivity_permission
2079: AndroidManifestActivity_taskAffinity
2247: AndroidManifestActivityAlias_targetActivity
2330: AndroidManifestActivityAlias_permission
2336: AndroidManifestActivityAlias_parentActivityName
1672: AndroidManifestApplication_name
1683: AndroidManifestApplication_manageSpaceActivity 
1697: AndroidManifestApplication_backupAgent 
1795: AndroidManifestApplication_permission 
1800: AndroidManifestApplication_taskAffinity
1815: AndroidManifestApplication_process
3005: AndroidManifestData_mimeType
3017: AndroidManifestData_scheme
3023: AndroidManifestData_host
3025: AndroidManifestData_port
3031: AndroidManifestData_path
3037: AndroidManifestData_pathPrefix
3043: AndroidManifestData_pathPattern
2527: AndroidManifestGrantUriPermission_path
2533: AndroidManifestGrantUriPermission_pathPrefix
2539: AndroidManifestGrantUriPermission_pathPattern
2579: AndroidManifestPathPermission_permission
2581: AndroidManifestPathPermission_readPermission
2586: AndroidManifestPathPermission_writePermission
2615: AndroidManifestPathPermission_path
2622: AndroidManifestPathPermission_pathPrefix
2629: AndroidManifestPathPermission_pathPattern
2434: AndroidManifestProvider_authorities
2441: AndroidManifestProvider_permission
2443: AndroidManifestProvider_readPermission
2454: AndroidManifestProvider_writePermission
2713: AndroidManifestService_permission
2832: AndroidManifestMetaData_name
1225: AndroidManifestOriginalPackage_name
1981: (parsePackageItemInfo -- I can't tell list of all names)
3258: (Component constructor args.nameres -- I can't tell list of all names)

Attributes read by getNonResourceString:

1806: AndroidManifestApplication_taskAffinity
1821: AndroidManifestApplication_process
1632: AndroidManifestInstrumentation_targetPackage
2891: AndroidManifestPackageVerifier_name
2894: AndroidManifestPackageVerifier_publicKey
1512: AndroidManifestPermission_permissionGroup
1200: AndroidManifestProtectedBroadcast_name
1927: AndroidManifestUsesLibrary_name
1054: AndroidManifestUsesFeature_name
1004: AndroidManifestUsesPermission_name
3308: (Component constructor  args.processRes -- I can't tell list of all names)

So, alot of attributes in the manifest.xml file must be specified as explicit string values (ie in quotes) rather than references to strings in strings.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文