我有几个不同的Gradle buildTypes,我正在尝试为每个buildTypes分别指定Google C2D_MESSAGE权限名称,因为我无法在手机上安装不同的应用,因为它们共享一个权限名称。但是我遇到了一个" Missing name属性"错误。
所以我在build.gradle中做了类似的事情:
buildType2 {
applicationIdSuffix '.suffix1'
...
resValue 'string', 'gcmPermission', "com.mypackage.suffix1.permission.C2D_MESSAGE"
}
在我的AndroidManifest.xml中:
<permission
android:name="@string/gcmPermission"
android:protectionLevel="signature" />
<uses-permission android:name="@string/gcmPermission" />
但是,构建时遇到以下错误:
AndroidManifest.xml:19: missing name attribute in element <permission>.
答案 0 :(得分:5)
我通过将permission-和uses-permission元素中的package-name替换为占位符来解决问题,占位符会被gradle自动替换为buildtypes和/或productFlavors:
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
答案 1 :(得分:3)
根据this post,问题只是名称&#39;属性不能引用字符串资源,它必须是原始字符串。
您可以使用资源来指定标签,但不能使用名称。
名称必须是唯一的,因此它应该使用Java样式的作用域 - 例如,&#34; com.example.project.PERMITTED_ACTION&#34;。
这就是arne.jans提供的解决方案有效的原因。 Gradle变量被替换为预编译,显示为Manifest的原始字符串。
答案 2 :(得分:0)
您还可以在build.gradle
中定义自己的清单占位符:
buildType2 {
manifestPlaceholders = [permissionName: 'com.example.my_perm']
#You can define it also as a string resource here
resValue 'string', 'permissionName', 'com.example.my_perm'
}
然后,您可以在Android清单中使用${permissionName}
或在代码中使用R.string.permissionName
。