使用Gradle覆盖GCM权限包前缀

时间:2013-12-02 16:59:27

标签: android gradle android-gradle

我有一个Gradle Android项目,它有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
    // ...
}

一切顺利,所需资源合并,e.t.c。现在我尝试使用GCM实现推送通知功能。根据文档,我必须为我的应用声明一个权限,如下所示:

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

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

所以我的问题是:如何为每种产品风格覆盖这些权限?

3 个答案:

答案 0 :(得分:64)

我已经使用过:

<permission android:name="${applicationId}.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
在Android清单中 - 这似乎已正确解析。

答案 1 :(得分:14)

您可以使用某种继承AndroidManifest。

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>

在root AndroidManifest其他所有内容。

答案 2 :(得分:3)

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