在 ContentProvider 中对 android:authorities 使用 @string
我的清单中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,但是使用了
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.
许多清单属性不能指定为对字符串的引用——它们必须指定为显式字符串值。
解析清单的代码位于:frameworks/base/core/java/android/content/pm/PackageParser.java。该类调用 getNonConfigurationString() 和 getNonResourceString()(在以下位置实现:frameworks/base/core/java/android/content/res/TypedArray.java)。
getNonConfigurationString() 将自身描述为:
getNonResourceString() 将自身描述为:
下面列出了 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读取的属性:
getNonResourceString读取的属性:
所以,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:
getNonResourceString() describes itself as:
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:
Attributes read by getNonResourceString:
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.