Android gradle two different launcher activities for two different product flavors
Asked Answered
P

2

18

This is my case

productFlavors {
    paid {
        applicationId "com.paid.app"
    }
    free {
        applicationId "com.free.app"
    }
}

and in paid flavor I need a different launcher activity in comparison to main or free as done below

main/AndroidManifest.xml

  <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

paid/AndroidManifest.xml

  <activity
        android:name=".SecondMainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

And when I begin to install app in paid build variants, it always install two apps i.e. free and paid but with same app name. And when I uninstall any one , both the app gets uninstalled. Shouldn't only paid variant build a paid app and free variant build a free app? Following is my working environment

  dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
   }
  Android Studio 1.4 beta 2
Pitiable answered 1/9, 2015 at 5:35 Comment(0)
O
8

You are not installing 2 apps.

Using the paid flavor in your Manifest you will merge 2 Activities with the LAUNCHER category.

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

In this way you will have 2 icons to launch your app. One for the MainActivity,one for the SecondActivity.

If you want a different Activity for each flavor,you have to use the same Activity in each flavor.

app/src/free/java/..../MainActivity
app/src/paid/java/..../MainActivity
Overspread answered 1/9, 2015 at 5:50 Comment(8)
Thanks for the info. I know on making same Activity on different flavors to make two different apps. I was curious whether same can be done with two different default or launcher activity.Pitiable
@Pitiable if you would like different default or launcher activity, the way is the same. Define the same class in the flavor folder (not in main)Overspread
Thanks for the info. So there seems no workaround with different Activity. Only option is defining the same Activity in product flavor folder and modifying as per necessity.Pitiable
I had done the second method, but get "duplicate class found" error. So unsure how it is supposed to work.Duffie
@Doomsknight may be you are getting "duplicate class" because you are using the same class in a flavor and in the main folder. You can't do it.Overspread
Thanks, that is the reason. Just trying to figure out how to use one class for two flavours, and another for the other 2 now. :) Without creating copies.Duffie
Does this question make sense, and do you know the solution please? link to questionDuffie
correct decision is here https://mcmap.net/q/500380/-android-gradle-two-different-launcher-activities-for-two-different-product-flavorsCheyennecheyne
I
30

Actually you can do that, with having 2 manifest files and without duplicating the activity:

main/AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

paid/AndroidManifest.xml:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     package="com.yourpackage">
         <activity
              android:name=".MainActivity"
              android:label="@string/app_name">
                  <intent-filter tools:node="remove">
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
         </activity>
         <activity
              android:name=".SecondMainActivity"
              android:label="@string/app_name">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
        </activity>

</manifest>

meaning that at manifest merge for paid flavor you remove the intent-filter that defines the launcher activity from main and add it to SecondMainActivity.

Idiopathy answered 9/12, 2015 at 16:19 Comment(0)
O
8

You are not installing 2 apps.

Using the paid flavor in your Manifest you will merge 2 Activities with the LAUNCHER category.

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

In this way you will have 2 icons to launch your app. One for the MainActivity,one for the SecondActivity.

If you want a different Activity for each flavor,you have to use the same Activity in each flavor.

app/src/free/java/..../MainActivity
app/src/paid/java/..../MainActivity
Overspread answered 1/9, 2015 at 5:50 Comment(8)
Thanks for the info. I know on making same Activity on different flavors to make two different apps. I was curious whether same can be done with two different default or launcher activity.Pitiable
@Pitiable if you would like different default or launcher activity, the way is the same. Define the same class in the flavor folder (not in main)Overspread
Thanks for the info. So there seems no workaround with different Activity. Only option is defining the same Activity in product flavor folder and modifying as per necessity.Pitiable
I had done the second method, but get "duplicate class found" error. So unsure how it is supposed to work.Duffie
@Doomsknight may be you are getting "duplicate class" because you are using the same class in a flavor and in the main folder. You can't do it.Overspread
Thanks, that is the reason. Just trying to figure out how to use one class for two flavours, and another for the other 2 now. :) Without creating copies.Duffie
Does this question make sense, and do you know the solution please? link to questionDuffie
correct decision is here https://mcmap.net/q/500380/-android-gradle-two-different-launcher-activities-for-two-different-product-flavorsCheyennecheyne

© 2022 - 2024 — McMap. All rights reserved.