Use flavor packaging in the manifest

Given the following benefits for the application:

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

And I need to declare 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> 

If com.example.gcmยด needs to be replaced with com.example.my.pkg.pro or com.example.my.pkg.free depending on the current taste.

How to configure my manifest to automatically select the package defined in the applicationId field?

+5
source share
1 answer

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 the flavor that you are building.

+12
source

Source: https://habr.com/ru/post/1215192/


All Articles