Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 488
Bitcoin Cash - ADA enthusiast Avatar asked Mar 27 '18 21:03

Bitcoin Cash - ADA enthusiast


2 Answers

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.

like image 161
bwt Avatar answered Oct 23 '22 10:10

bwt


You can of course control how to merge resource and may avoid such repetition using the main folder, check the doc here, you may be interesting by tools:node="merge" which help you control how node are merged, you'll get more info about it from teh link above.

like image 45
Samuel Eminet Avatar answered Oct 23 '22 10:10

Samuel Eminet