Customize AndroidManifest in different build types

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.


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
}

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.

Unfortunately, there's a limitation in the merging of manifests where you can't define the same thing (Activity, Service, Receiver, etc.) in the main manifest and the build type manifest. The solution is to define only the non-buildtype specific stuff in the main manifest and everything else in the build type manifests.

链接地址: http://www.djcxy.com/p/76084.html

上一篇: Vector与ArrayList哪个更好?

下一篇: 在不同的构建类型中自定义AndroidManifest