Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the package of a flavor in the manifest

Given the following flavors for an app:

productFlavors {
    pro {
        applicationId = "com.example.my.pkg.pro"
    }
    free {
        applicationId = "com.example.my.pkg.free"
    }
}

And I need to declare the GCM permissions:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<application ...>
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.gcm" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />
</application>

Where the com.example.gcm´must be replaced with com.example.my.pkg.pro or com.example.my.pkg.free depending on the current flavor.

How can I configure my manifest to automatically pick the package defined in the applicationId field?

like image 834
Addev Avatar asked Mar 11 '15 23:03

Addev


People also ask

When would you use a product Flavour in your build setup?

You use same core ingredients to make the base but will use different toppings for each one to have a different taste. Similarly, android apps can have the same base functionalities with changes to some of the features like styles, logo etc. This can be achieved using product flavours.

How do I get the current flavor in gradle?

gradle. getStartParameter(). getTaskRequests(). toString() contains your current flavor name but the first character is capital.

What is flavorDimensions?

Flavor Dimensions is a way to group flavors by a name. For now, we're using just a single group. Add the following line in your defaultConfig block: flavorDimensions "default" Now syncing the gradle would give you the following product flavors: Android Build Variants combine build types and product flavors.

How can you tell if a manifest is merged?

Even before you build your app, you can see a preview of what your merged manifest looks by opening your AndroidManifest. xml file in Android Studio, and then clicking the Merged Manifest tab at the bottom of the editor.


1 Answers

Use placeholders:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.commonsware.android.gcm.client"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="17"/>

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"/>

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

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <receiver
            android:name="GCMBroadcastReceiverCompat"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
    </application>

(from this sample project)

Here, you will see ${applicationId} used as a placeholder. This should automatically expand to be the applicationId of the flavor that you are building.

like image 139
CommonsWare Avatar answered Oct 11 '22 20:10

CommonsWare