Customize AndroidManifest in different build types

android, android-gradle-plugin, android-manifest

Solution

As of gradle plugin 0.10 it's finally supported. More info about new manifest merger: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger

At the moment (gradle plugin 0.10) it requires this extra configuration in `build.gradle`

android {
 useOldManifestMerger false
}

Problem

I want to customize AndroidManifest in different build types. For example in debug mode I just want an Activity to be exported. Assume main manifest: ``` /main/AndroidManifest.xml <application> <activity android:name="com.example.MainActivity" /> </application> ``` Debug manifest: ``` /debug/AndroidManifest.xml <application> <activity android:name="com.example.MainActivity" android:exported="true" /> </application> ``` Example manifest (same as debug): ``` /example/AndroidManifest.xml <application> <activity android:name="com.example.MainActivity" android:exported="true" /> </application> ``` In the debug manifest I get `Duplicate registration for activity com.example.MainActivity` That's why I created the example build type. ``` /build.gradle android { buildTypes { example.initWith(buildTypes.debug) } } ``` But it also doesn't work. ``` [AndroidManifest.xml:17, AndroidManifest.xml:4] Trying to merge incompatible /manifest/application/activity[@name=com.example.MainActivity] element: <activity -- @android:name="com.example.MainActivity"> --</activity> --(end reached) <activity ++ @android:exported="true" ++ @android:name="com.example.MainActivity"> ++</activity> ``` I'm wondering whether this is a bug, missing feature (will be implemented in the future) or I'm doing something wrong? I know I can provide different manifest in release and debug (without the one in `/main`), but I don't think this is a good solution. EDIT: The solution so far is to define bool in resources and use it inside main manifest. In debug resources the bool will be `true` while in the release `false`. This solution seems much better than duplicated manifests, but the question is still actual.

Original source

Related problems