15

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">
Karim Varela
  • 7,562
  • 10
  • 53
  • 78

3 Answers3

11

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

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • Thanks Xav, when I use this model and try to build within Android Studio, I get: "Android Packager: [] java.util.zip.ZipException: duplicate entry: AndroidManifest.xml" Any ideas? – Karim Varela Sep 12 '13 at 22:39
  • are you customizing the sourcesets? this shouldn't happen if you don't and or if you did, you'll need to make the path I mentioned match your actual structure. – Xavier Ducrohet Sep 13 '13 at 17:44
  • Thanks Xav, I tried setting up my project as you suggest, but this setup has apparently caused another issue with Android Annotations so I'm not able to determine if this fixes my merging problem. The other issue is at http://stackoverflow.com/questions/18833642/gradle-android-annotations-merged-manifest-could-not-find-the-androidmani. If you wouldn't mind taking a look, I'd really appreciate it! – Karim Varela Sep 16 '13 at 17:13
  • @XavierDucrohet I was trying to solve Gradle merging/overwriting issues as well, except that mine isn't related to activities and intent filters, instead it's related to the manifest attributes like android:sharedUserId. Used the folder structure that you've suggested, but it doesn't work. Can you suggest a solution? – Kevin Tan Jan 10 '14 at 10:10
  • 1
    @KevinTan there's no obviously solutions right now but we're working on improving the manifest merger soon – Xavier Ducrohet Jan 11 '14 at 19:37
  • Any news on the improving of the manifest manager? I can't get the merger working when I have added intent-filters in the consuming manifest even when adding the override hint. – slott May 09 '14 at 13:08
  • This may have been the right answer in 2013, but with improvements in Manifest Merger @esp has the right answer. – tir38 Apr 21 '16 at 02:56
  • @KevinTan `android:sharedUserId` is work in @Xavier solution Just have default manifest without `android:sharedUserId` and src/full/AndroidManifest.xml with defined one – Art Aug 16 '16 at 12:38
5

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

espinchi
  • 9,144
  • 6
  • 58
  • 65
4

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">
swooby
  • 3,005
  • 2
  • 36
  • 43
  • This method has worked for me. All that you need is to add tools:merge="override" in the child (overrided) AndroidManifes.xml – Vlad Yarovyi Mar 19 '14 at 15:07
  • I still get "Trying to merge incompatible" after adding this :( – slott May 09 '14 at 11:33
  • This solution bitrotted. See a working one here http://stackoverflow.com/questions/21239844/gradle-merge-wrapper-sub-modules-android-manifest-into-a-main-modules-manifest – Oded Sep 16 '16 at 05:48