Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a different launcher activity in a product flavor?

I'm working on an Android library project, in the default src/main/AndroidManifest.xml, the MainActivity is the launcher activity.

For the sake of something else, I created product flavors. Yes, it works perfect if I want to trigger / show different activitis inside different product flavors. However, I wanna keep the default launcher activity from src/main/ folder, while register another flavored activity as the new launcher activity. So that for different product flavors, I could have different launcher activities, and from them I could still start original "launcher" activity in src/main/.

Could anyone kindly tell me how to achive that? Thanks a lot.

Notes:

  1. Adding if (BuildConfig.FLAVOR.equals("flavorName")) code to original launcher activity is not prefered. Because I don't want to modify the production code from someone else (this is a library project).

  2. I've tried manifestmerger and tools:replace, but seems like it doesn't work for intent-filter (I noticed that the element merging policy for intent-filter is always).

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

If this may work, could you please kindly guide me how to make it work? Thanks.

like image 943
Jing Li Avatar asked Aug 31 '15 21:08

Jing Li


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.

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

What I have tried:

  1. Enabling Manifest Merger, which doesn't work;
  2. Using activity-alias, which doesn't work either.

Finally I found out that the problem could be solved by just adding one line:

<category android:name="android.intent.category.DEFAULT" />

==================================================

To make it clear, I'll go through the problem and solution one more time:

Under src/main/java there is a MainActivity, and in corresponding src/main/AndroidManifest.xml it specifies MainActivity as the launcher activity:

<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>

That is a very easy part. Now we start with the product flavor part.

Due to some reason, in a product flavor, I don't want to overwrite the MainActivity, instead, I have a YetAnotherMainActivity. The goal is to set the YetAnotherMainActivity as the new launcher activity in the product flavor, and it should still be able to call MainActivity.

And here, is how you can set the new activity in product flavor as the new launcher activity:

flavorX/AndroidManifest.xml:

<activity android:name="com.example.YetAnotherMainActivity"
    android:label="@string/title_yet_another_main_activity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Yep, it turns out deadly easy. Just add android.intent.category.DEFAULT.

like image 180
Jing Li Avatar answered Oct 16 '22 08:10

Jing Li


I think that <activity-alias> fits there more than any other solution (have no idea why @JingLi couldn't get it working. Maybe there were some troubles year ago, but for now it's okay).

For example, we have the following manifest in main:

<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.application">
    <application>
        <activity android:name=".InfoActivity"/>
        <activity-alias 
            android:name="com.example.application.Launcher"
            android:targetActivity=".InfoActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity-alias>
    </application>
</manifest>

And we want to replace launcher activity with DebugInfoActivity from debug flavor. So, we need to just replace the targetActivity attribute in the specified <activity-alias> tag:

<manifest 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    <application>
        <activity android:name=".DebugInfoActivity"/>
        <!-- to not litter the manifest -->
        <activity 
                android:name="com.example.application.InfoActivity"
                tools:node="remove"/>
        <activity-alias
                android:name="com.example.application.Launcher"
                android:targetActivity=".DebugInfoActivity"
                tools:replace="android:targetActivity"/>
    </application>
</manifest>

Notes:

  • In the example we use the same package name for main and debug.
  • We have to enter the full name for activity-alias, so the merger can merge their correctly.

With the solution we also can inherit all attributes and childs from main activity-alias to not duplicate their in debug.

like image 10
seroperson Avatar answered Oct 16 '22 08:10

seroperson