Home > database >  Adroid Studio: what is the difference in tools:replace and tools:ignore mean in the AndroidManifest
Adroid Studio: what is the difference in tools:replace and tools:ignore mean in the AndroidManifest

Time:07-05

I"ve just fixed a bug as "Manifest Merger Failed" with the help of this forum. The solution was to change tools:replace into tools:ingore under the <application in the AndroidManfifest.xlm. I am happy that it solved my problem but I want to learn what is the idea behind it, I've just started coding.

CodePudding user response:

An apk can contain only one manifest when we use multiple dependencies/libraries/modules which has their own manifest files then during building the application, gradle marges all the manifests to one, during merging if one or more dependencies has some conflict then this error comes up read this article for more

CodePudding user response:

Your app can only have one single manifest file, but every library you add to your app can have it's own manifest file, also when you work with multiple module project every module can have it's own manifest file, so when you build you app, all those manifest files are going to be merged into a single file. You main manifest file has higher priority, so you can force what parameters should be passed and replace parameters of other manifest files with tools:replace, let's say for example that you have two manifest files.

Low priority manifest ( from a library) :

<activity android:name="com.example.ActivityOne"
    android:theme="@oldtheme"
    android:exported="false"
    android:windowSoftInputMode="stateUnchanged">

High priority manifest ( your main manifest ):

<activity android:name="com.example.ActivityOne"
    android:theme="@newtheme"
    android:exported="true"
    android:screenOrientation="portrait"
    tools:replace="android:theme,android:exported">

Here in your main manifest file, you are forcing the android:theme and android:exported of your main manifest file so the merged manifest result will be like that:

<activity android:name="com.example.ActivityOne"
    android:theme="@newtheme"
    android:exported="true"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateUnchanged">

These examples are from google documentation here

Now for the attribute tools:ignore, it can be used in xml files to dismiss lint warnings. It's not fixing any problem but just hiding the error and ignoring it. So fixing problems with this technique is most of the time the wrong way to proceed so when you use it. You should have a strong reason to ignore that error (i.e. requesting the protected permission and ignoring the lint warning because you know that in your case the permission will be granted)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      ...>
    <uses-permission android:name="android.permission.SET_TIME"
        tools:ignore="ProtectedPermissions"/>

You can read about tools:ignore in google documentation here

  • Related