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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With