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?
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.
gradle. getStartParameter(). getTaskRequests(). toString() contains your current flavor name but the first character is capital.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With