简体   繁体   中英

Android - How to avoid repetition in multiple Manifest files?

My project has 3 Manifest files:

flavour/AndroidManifest.xml
flavourDebug/AndroidManifest.xml
flavourRelease/AndroidManifest.xml

Here is flavour/AndroidManifest.xml:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>

Here is flavourDebug/AndroidManifest.xml:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application android:name="com.domain.android.MyApplication"
        android:allowBackup="false"
        android:label="@string/app_name"
        android:logo="@drawable/ic_logo"
        android:theme="@style/Theme.CustomActionBarStyle"
        android:networkSecurityConfig="@xml/network_security_config"
        tools:replace="theme">

        // Activity definitions in here

     </application>
</manifest>

Here is flavourRelease/AndroidManifest.xml:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application android:name="com.domain.android.MyApplication"
        android:allowBackup="false"
        android:label="@string/app_name"
        android:logo="@drawable/ic_logo"
        android:theme="@style/Theme.CustomActionBarStyle"
        tools:replace="theme">

        // Activity definitions in here (these are the EXACT SAME as the ones in flavourDebug/AndroidManifest.xml)

     </application>
</manifest>

As you can see, the only difference between the Debug and Release Manifests is that the Release one is missing android:networkSecurityConfig

Also, the // Activity definitions in here part is exactly the same. What I want is to avoid that Activity repetition. Every time we have to change something in an Activity definition (or add a new Activity) we have to do that in 2 Manifest files (Debug and Release).

I had the idea of putting everything inside the main AndroidManifest.xml file. The problem is that I would not be able to add android:networkSecurityConfig="@xml/network_security_config" only to the debug builds .

In Android layouts, that problem is solved with the <include> tag. Unfortunately that is not available in the Manifest.

How can I solve this repetition problem?

You can definitely put the common part in flavour/AndroidManifest.xml and the additionnal attribute in flavourDebug/AndroidManifest.xml (and the referenced xml file in the src/flavourDebug/res/xml dir):

<application
    android:networkSecurityConfig="@xml/network_security_config" />

As you are adding an attribute, it should work out of the box, without tweaking the merge rules ( tools:node="merge" is the default behaviour for most elements).

With Android Studio 3.1 (and probably earlier versions) you can view the final manifest, and from where each attribute or element come from in the Merged manifest tab of the editor.

您当然可以控制如何合并资源并可以使用主文件夹避免这种重复,请查看此处的文档,您可能会感兴趣的tools:node="merge"可以帮助您控制节点的合并方式,您将获得更多从上面的链接的信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM