Gradle: How to merge Android manifest files for di

2019-01-13 18:57发布

问题:

so I'm trying to use gradle to create a separate buildType, but that buildType needs to use different characteristics for the same Activity. In this case, my splash activity needs a different intent-filter depending on buildType. Is this possible?

I get the following error in gradle:

:Tinder:processUtestManifest
[AndroidManifest.xml:67, AndroidManifest.xml:38] Trying to merge incompatible /manifest/application/activity[@name=com.<company_name>.activities.ActivitySplash] element:
  <activity
      @android:name="com.<company_name>.activities.ActivitySplash"
      <intent-filter>
          <action
--            @android:name="android.intent.action.MAIN">
  <activity
      @android:name="com.<company_name>.activities.ActivitySplash"
      <intent-filter>
          <action
++            @android:name="com.apphance.android.LAUNCH">

回答1:

It's not possible to merge the intent-filter separately at the moment so I would recommend copying the whole <activity> node into

src/buildtype1/AndroidManifest.xml

and

src/buildtype2/AndroidManifest.xml

and it'll get merged automatically into the final manifest (of course you also want to remove it from the main manifest).



回答2:

Let me post a full solution that works. In this case, the application manages intent filters for both images and videos. However, for a particular flavor, we only want to capture videos, not images.

Say your activity is defined like this:

<activity
    android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:screenOrientation="landscape"
    android:theme="@style/CameraTheme">

    <intent-filter>
        <action android:name="android.media.action.IMAGE_CAPTURE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

    <intent-filter>
        <action android:name="android.media.action.VIDEO_CAPTURE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

For your flavor named myFlavor, that I assume you already have, you have to have this in your AndroidManifest.xml file:

<!-- Remove the intent filter for images. MyFlavor is only for videos. -->
<activity
    android:name="com.androidsx.heliumvideochanger.MainActivity"
    tools:node="merge">

    <intent-filter tools:node="remove">
        <action android:name="android.media.action.IMAGE_CAPTURE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Sync with Gradle, and check the final manifest, that is usually in myApp/build/intermediates/manifests/full/myFlavor/debug/AndroidManifest.xml



回答3:

Change the AndroidManifest in the consuming app/lib from:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<activity android:name="com.<company_name>.activities.ActivitySplash">

To:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
...
<activity android:name="com.<company_name>.activities.ActivitySplash"
    tools:merge="override">