FeleMed
FeleMed

Reputation: 621

Replace launching activity with flavors

Is it possible to replace the activity that gets the intent-filter

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

by configuring flavors?

Upvotes: 5

Views: 2978

Answers (3)

林果皞
林果皞

Reputation: 7813

In addition to @Tanis answer, I want to add some parts you should aware of.

productFlavors {
    lite {
        dimension "default"
        applicationId "com.foo.bar"
        manifestPlaceholders = [ applicationLabel:"@string/app_name_foo"
                                 , launcherAct: "com.foo.qux.activity.Hello"
                                 , launcherAct2: "com.foo.qux.activity.World"]
    }
    full {
        dimension "default"
        applicationId "com.foo.qux"
        manifestPlaceholders = [ applicationLabel:"@string/app_name_bar"
                                 , launcherAct: ".activity.World"
                                 , launcherAct2: ".activity.Hello"]
    }
}
  1. If let's say, your World.java exists in com.foo.qux.activity folder, then you should use full path com.foo.qux.activity.World instead of .activity.World when put it inside applicationId "com.foo.bar" flavor, or else it will auto prepend to com.foo.bar.activity.World which the file doesn't exist.

  2. Unlike android:label, you can't use something like @string/launcher_class in android:name, or else you will get error: attribute 'android:name' in <activity> tag must be a valid Java class name. when build.

  3. Ensures you don't duplicate the class, e.g. :

    <activity
        android:name="${launcherAct}"
        android:label="${applicationLabel}">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
    <activity
        android:name="${launcherAct2}"
        android:label="${applicationLabel}">
    </activity>
    

Don't forgot to change from .activity.World to android:name="${launcherAct2}" or else you end up duplicated activity (i.e. one in AndroidManifest.xml, the other one in manifestPlaceholders).

  1. The Unresolved class red indicator can safely ignored, seems like Android Studio bug: enter image description here

I make a bug report here.

Upvotes: 1

Bryan Herbst
Bryan Herbst

Reputation: 67239

There are a number of ways to accomplish this thanks to the manifest merger.

The easiest way is to use a placeholder in your manifest and define the appropriate class in your build.gradle.

For example, in your manifest:

<activity android:name="${launchActivityName}">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

And in your build.gradle:

productFlavors {
    flavor1 {
        manifestPlaceholders = [ launchActivityName:"com.example.MainActivity"]
    }
}

You can also include a different manifest file with each flavor.

Upvotes: 10

Sandro Machado
Sandro Machado

Reputation: 10215

Yes, just add a manifest file in the folder of the product flavour and

The Manifest of the product flavor is merged on top of the Manifest of the main configuration.

More info here: http://tools.android.com/tech-docs/new-build-system/build-system-concepts

Upvotes: 6

Related Questions