用Gradle覆盖GCM权限包前缀

我有一个Gradle安卓项目,有4个产品口味(每个都有自己独特的包名称)。 build.gradle文件非常简单:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } apply plugin: 'android' repositories { mavenCentral() } dependencies { // All dependencies here // ... } android { // Usual stuff goes here productFlavors { flav1 { packageName 'com.company.flav1' versionName calcVersion() } flav2 { packageName 'com.company.flav2' versionName calcVersion() } flav3 { packageName 'com.company.flav3' versionName calcVersion() } flav4 { packageName 'com.company.flav4' versionName calcVersion() } } // Other configurations // ... } 

一切顺利,所需的资源被合并等等现在我正在尝试使用GCM实现推送通知function。 根据文档,我必须声明我的应用程序的许可,像这样:

 <permission android:name="com.company.flav.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.company.flav.permission.C2D_MESSAGE" /> 

但是,我为所有4个应用程序使用单个清单文件(它们只在应用程序资源和一些设置上有所不同,放在assets文件夹中)。

所以我的问题是:我怎样才能覆盖每个产品的味道这些权限?

  • 如何使用deviceCheck任务在远程设备上运行testing
  • 为Android构build手机差距应用程序失败
  • Gradle Artifactory插件 - 如何从项目中的多个模块发布工件?
  • Android依赖关系:apklib vs aar文件
  • 在Android中为Gradle中的库项目构build变体
  • Proguard Printmapping无法在Android Studio 2.0.0(和2.1.0)中运行
  • Android Studio 3.1.2:无法解析:运行时
  • 无法解析配置':classpath'的所有依赖项
  • 我用过:

     <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> 

    在Android清单 – 这似乎是正确解决。

    你可以使用某种inheritanceAndroidManifest。

     The following rules are used when dealing with all the sourcesets used to build a single APK: - All source code (src/*/java) are used together as multiple folders generating a single output. - *Manifests are all merged together into a single manifest. This allows Product Flavors to have different components and/or permissions, similarly to Build Types.* - All resources (Android res and assets) are used using overlay priority where the Build Type overrides the Product Flavor, which overrides the main sourceSet. - Each Build Variant generates its own R class (or other generated source code) from the resources. Nothing is shared between variants. 

    码:

      productFlavors { flav1 { packageName 'com.company.flav1' versionName calcVersion() } flav2 { packageName 'com.company.flav2' versionName calcVersion() } flav3 { packageName 'com.company.flav3' versionName calcVersion() } flav4 { packageName 'com.company.flav4' versionName calcVersion() } } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aild.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } flav1 { manifest.srcFile 'flavor1/AndroidManifest.xml' } flav2 { manifest.srcFile 'flavor2/AndroidManifest.xml' } ... } 

    其中flavor1flavor2是根项目文件夹中的文件夹。

    flavor1/AndroidManifest.xml

     <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample.someapp" android:versionCode="1" android:versionName="1.0"> <permission android:name="com.company.flav1.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.company.flav1.permission.C2D_MESSAGE" /> </manifest> 

    flavor2/AndroidManifest.xml

     <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample.someapp" android:versionCode="1" android:versionName="1.0"> <permission android:name="com.company.flav2.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.company.flav2.permission.C2D_MESSAGE" /> </manifest> 

    并在根AndroidManifest一切。

    AndroidManifest.xml现在使用默认设置从Gradle 1.11正确合并。