Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle two different launcher activities for two different product flavors

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
like image 912
laaptu Avatar asked Sep 01 '15 05:09

laaptu


People also ask

Can we have two launcher activities?

On latest Android versions - If there are more than one launcher activities and if we don't put this category tag then the activity which is mentioned first in the Android manifest file will get launched as start-up activity. When i hav 2 launch activity, that time 2 launch icon display in my mobile.

What is a build type and a product flavor in Android?

buildType is the how of the build. flavor is the what of the build.

What are the variants of Android?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.

What is meant by launcher activity in Android Studio?

Launcher Activities are the activities that can be launched for a given intent. For example, when you press an app icon on the home screen, the StartActivity intent starts the activity you have specified as the launcher activity.


2 Answers

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.

like image 196
Mihaela Romanca Avatar answered Sep 19 '22 12:09

Mihaela Romanca


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
like image 30
Gabriele Mariotti Avatar answered Sep 19 '22 12:09

Gabriele Mariotti