Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ContentProvider中使用@string for android:authorities_Android_Android Contentprovider - Fatal编程技术网

在ContentProvider中使用@string for android:authorities

在ContentProvider中使用@string for android:authorities,android,android-contentprovider,Android,Android Contentprovider,我的清单中有一个ContentProvider,当我用硬编码字符串完全定义它们时,它就可以工作了。例如 <provider android:name="com.myprovider" android:authorities="com.myprovider"/> 工作非常完美,但是ContentProviders位于一个库中,可供多个项目使用,我不希望出现权限冲突,因此我尝试执行以下操作 <provider android:name="com.myprovider" and

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

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

工作非常完美,但是ContentProviders位于一个库中,可供多个项目使用,我不希望出现权限冲突,因此我尝试执行以下操作

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

通过这种方式,我应该能够在单个strings.xml文件中定义我的所有权限,并且应用程序之间没有冲突,因为我应该能够使用每个应用程序资源覆盖系统来更改它们

然而,当我尝试使用@string构建时,它似乎给了我一个格式错误的清单错误,并说“提供者不包含(是的,它说包含)权限属性”


我不能使用资源字符串来表示权威,每当我需要在两个位置维护常量时,我都会感到恶心。我们的QA部门很难发现权限冲突,我不希望事情变得不同步,否则会造成混乱。有人知道我的代码为什么不起作用吗?

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


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

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

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

GetNonfigurationString()将自身描述为:

Retrieve the string value of an attribute that is not allowed to change with the given configurations.
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.
getNonResourceString()将自身描述为:

Retrieve the string value of an attribute that is not allowed to change with the given configurations.
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不允许从资源或不同配置获取的清单属性如下所示

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

GetNonfigurationString读取的属性:

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中字符串的引用。

我自己也遇到了这个问题,我发现这在2.2+中效果很好。当然,如果有人指出如何在2.1及之前的版本中实现这一点,那就太好了。我已经谈到了字符串,但也许你可以在将android:minSdkVersion设置为7或其他任何目标时使用更新的API作为构建目标,类似于让appsToSd编译成支持2.1的应用程序。我认为这行不通,因为实际的手机在安装时会抱怨,所以编译时不会失败。最后,我用Maven“手动”替换了字符串。这很糟糕,但它现在起作用了。我想我可以在2.1生命周期结束后收回:)顺便说一句,
provider
-属性应该是
android:authorities
(另请参见)。@dmon“我最终用Maven“手动”替换了字符串”-我需要这样做。我已经替换/覆盖了资源文件夹如何回复身份验证字符串?这是不启用它的一个很好的理由,但它显然在更高版本中有效,而不是2.1,所以“一致性”不是它不起作用的原因。我明白了。该设计防止在某些情况下使用资源字符串。如果我在AndroidManifestProvider\u权限的清单中使用和显式字符串,应用程序中是否有一种方法可以访问该字符串,主要是为了在一个位置定义字符串并在整个过程中使用一致的字符串?