Search code examples
androidactionintentfilterandroid-productflavors

how do i make Activity intentFilter action dynamic from strings.xml


I'm using different product flavours, and opening an Activity through intentFilter whose action is predefined as shown below.

<activity android:name=".MyActivity"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="com.package.name.MyAction"/>
          <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

For opening this activity I'm using :

Intent intent = new Intent("com.package.name.MyAction");
startActivity(intent);

Now, if my device have both product flavour apk, a chooser is coming for some device to open which activity app, for some devices it show a error no app found to perform the action.

As soon as I uninstall one app, and keep only one app in my device everything works fine.

One solution I thought to overcome is, to make the action constant as Intent intent = new Intent("com.package.name.MyAction"); context.startActivity(intent); dynamic. But I'm not sure how can I do this.

Im AndroidManifest, it should be something as:

<action android:name="com.package.name.MyAction"+getString(R.string.product_name)/>

This is the part where I'm unable to get the string in AndroidMaifest.

For opening the activity I have done:

Intent intent = new Intent("com.package.name.MyAction"+getString(R.string.product_name));
startActivity(intent);

Any help will be appreciated, thank you.


Solution

  • In your build.gradle (app), config your flavors like this:

        productFlavors {
            first {
                // ...
                manifestPlaceholders = [action: "com.package.name.ActionA"]
            }
    
            second {
                // ...
                manifestPlaceholders = [action: "com.package.name.ActionB"]
            }
        }
    

    Then place the value of placeholder in AndroidManifest.xml file:

        <activity android:name=".MyActivity"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="${action}"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    

    In order to get the placeholder to open the activity, just get it through BuildConfig:

        String yourAction = BuildConfig.action;
        // start your intent here