189

I am using a gradle project with many different library dependencies and using the new manifest merger. In my <application /> tag I have it set up as such:

<application tools:replace="android:icon, android:label, android:theme, android:name"
    android:name="com.example.myapp.MyApplcation"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/application_name"
    android:logo="@drawable/logo_ab"
    android:theme="@style/AppTheme"
    >
....
</application>

Yet I am receiving the error:

/android/MyApp/app/src/main/AndroidManifest.xml:29:9        Error:
Attribute application@icon value=(@drawable/ic_launcher) from AndroidManifest.xml:29:9
is also present at {Library Name} value=(@drawable/app_icon)
Suggestion: add 'tools:replace="android:icon"' to <application> element at AndroidManifest.xml:26:5 to override

/android/MyApp/app/src/main/AndroidManifest.xml:30:9 Error:
Attribute application@label value=(@string/application_name) from AndroidManifest.xml:30:9
is also present at {Library Name} value=(@string/app_name)
Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:26:5 to override

/android/MyApp/app/src/main/AndroidManifest.xml:27:9 Error:
Attribute application@name value=(com.example.myapp.MyApplication) from AndroidManifest.xml:27:9
is also present at {Another Library}

Suggestion: add 'tools:replace="android:name"' to <application> element at AndroidManifest.xml:26:5 to override

/android/MyApp/app/src/main/AndroidManifest.xml:32:9 Error:
Attribute application@theme value=(@style/AppTheme) from AndroidManifest.xml:32:9
is also present at {Library Name} value=(@style/AppTheme)
Suggestion: add 'tools:replace="android:theme"' to <application> element at AndroidManifest.xml:26:5 to override
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Andrew Grosner
  • 2,337
  • 2
  • 18
  • 14
  • 1
    See this post in Meta about deleting and re-posting questions: http://meta.stackoverflow.com/questions/265233/what-should-you-do-if-nobody-answers-your-question-can-you-repost-it . Doing this once won't particularly harm you, but it wouldn't be good to make a regular practice of it. – Scott Barta Sep 22 '14 at 18:56
  • 1
    I guess there is `` tag repeated in the project. Pls ensure there is only one ``. – Panther Sep 22 '14 at 20:18
  • There can be many tags in a project especially if you use any number of library submodules. – Andrew Grosner Sep 22 '14 at 21:14
  • I have examples of cases where this works just fine. What version of the Android build tools are you using? – G. Blake Meike Oct 10 '14 at 23:16
  • did you add xmlns:tools="http://schemas.android.com/tools" in the manifest tag? http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger – TouchBoarder Dec 04 '14 at 08:12
  • Did you get a solution? I have the same problem. The replace tag does not work. I am getting always the error with "suggestion to add tools:replace"android:theme" to it! It is added and of course I have added xmlns:tools line! – chrisonline Mar 15 '15 at 13:45
  • please tell me one thing tools:replace should i add this inside application tag of my project manifest or inside application tag of my library please tell me it will be really helpful for me ?? – Sudhanshu Gaur Dec 16 '15 at 13:19
  • https://stackoverflow.com/a/54825603/1318946 – Pratik Butani Feb 22 '19 at 11:42

15 Answers15

312

Declare your manifest header like this

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

Then you can add to your application tag the following attribute:

<application
    tools:replace="icon, label" ../>

For example I need to replace icon and label.

starball
  • 20,030
  • 7
  • 43
  • 238
Andrus
  • 3,529
  • 1
  • 16
  • 7
  • 1
    how do you handle android:name with meta-data? android:name="com.orm.SugarApp"> – Alan Mar 06 '15 at 15:06
  • @Alan Simply replace the whole meta-data tag with `tools:node="replace"`: `` etc. – Blacklight Mar 11 '15 at 09:59
  • 30
    I'm confused. Isn't that exactly what he's doing in his question? – Jason Robinson Apr 24 '15 at 04:08
  • I added `tools:replace` to my application tag that should replace it and not the source which should be replaced. So my app manifests replace what is defined in my library. You say "what should be replaced" instead of "what can be replaced". I hope that cleans up the questions. – WarrenFaith Nov 10 '15 at 13:18
  • 1
    If replaced attribute is in manifest tag, the `tools:replace="android:versionCode, android:versionName"` must be inside `` tag. – Ivan Chau Nov 15 '15 at 16:13
  • 1
    please tell me one thing tools:replace should i add this inside application tag of my project manifest or inside application tag of my library please tell me it will be really helpful for me ?? – Sudhanshu Gaur Dec 16 '15 at 13:19
56

I fixed same issue. Solution for me:

  1. add the xmlns:tools="http://schemas.android.com/tools" line in the manifest tag
  2. add tools:replace=.. in the manifest tag
  3. move android:label=... in the manifest tag

Example:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
              tools:replace="allowBackup, label"
              android:allowBackup="false"
              android:label="@string/all_app_name"/>
stefan K
  • 633
  • 5
  • 4
  • 1
    Thanks for this, adding it in the `` directly instead of under `` was the only thing that fixed my issue. – A. Sokol Jan 30 '17 at 23:27
  • 2
    I didnt work for me, I should put tools:replace="android:allowBackup, android:label" nor just tools:replace="allowBackup, label". – jmarkstar May 18 '17 at 20:38
  • 2
    Are you sure this is working? Because for me, looks like you're writing the allowBackup in the wrong place, and that after the merge, what the Library specified on his manifest, prevails. You don't have a conflict after moving it on top, because you're just doing it wrong (https://developer.android.com/guide/topics/manifest/application-element.html) AllowBackup belongs to Application element. – Reinherd Jul 18 '17 at 13:28
  • 2
    [...continues from above] THIS DOES NOT WORK. You're writing the allowBackup in the wrong place, and that after the merge, what the Library specified on his manifest, prevails. You don't have a conflict after moving it on top, because you're just doing it wrong (https://developer.android.com/guide/topics/manifest/application-element.html) AllowBackup belongs to Application element. I've tested it by decompiling an APK, and the final manifest had the value as TRUE instead of FALSE as I specified as you suggested – Reinherd Jul 18 '17 at 13:44
38

Try reordering your dependencies in your gradle file. I had to move the offending library from the bottom of the list to the top, and then it worked.

GLee
  • 5,003
  • 5
  • 35
  • 39
  • please tell me one thing tools:replace should i add this inside application tag of my project manifest or inside application tag of my library please tell me it will be really helpful for me ?? – Sudhanshu Gaur Dec 16 '15 at 13:19
  • @SudhanshuGaur You just need to add it to the AndroidManifest.xml file of your project. – Kyle L Dec 18 '15 at 21:40
  • 84
    It worked. But such solutions may take out faith from humanity. – Vinay Patil Mar 11 '16 at 09:41
  • 8
    #android gotchas like these make developing for android pretty annoying – Taylor Halliday Apr 08 '16 at 18:32
  • surprising! Worked for me as well :) – Yash Ladia Sep 21 '16 at 11:33
  • 1
    In case you have multiple product flavours like play and non play and the dependency is "playReleaseCompile ______", just moving the statement to the top might not work, you might have to change the statement to "compile ______" and then move it to the top. It should work then. – Soham Sep 29 '16 at 04:38
  • In case you have multiple product flavours like play and non play and the dependency is "playReleaseCompile ______", just moving the statement to the top might not work, you might have to change the statement to "compile ______" and then move it to the top. It should work then. – Soham Sep 29 '16 at 04:38
34

I just experienced the same behavior of tools:replace=... as described by the OP.

It turned out that the root cause for tools:replace being ignored by the manifest merger is a bug described here. It basically means that if you have a library in your project that contains a manifest with an <application ...> node containing a tools:ignore=... attribute, it can happen that the tools:replace=... attribute in the manifest of your main module will be ignored.

The tricky point here is that it can happen, but does not have to. In my case I had two libraries, library A with the tools:ignore=... attribute, library B with the attributes to be replaced in the respective manifests and the tools:replace=... attribute in the manifest of the main module. If the manifest of B was merged into the main manifest before the manifest of A everything worked as expected. In opposite merge order the error appeared.

The order in which these merges happen seems to be somewhat random. In my case changing the order in the dependencies section of build.gradle had no effect but changing the name of the flavor did it.

So, the only reliable workaround seems to be to unpack the problem causing library, remove the tools:ignore=... tag (which should be no problem as it is a hint for lint only) and pack the library again.

And vote for the bug to be fixed, of cause.

Nantoka
  • 4,174
  • 1
  • 33
  • 36
26

Final Working Solution for me (Highlighted the tages in the sample code):

  1. add the xmlns:tools line in the manifest tag
  2. add tools:replace in the application tag

Example:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pagination.yoga.com.tamiltv"
    **xmlns:tools="http://schemas.android.com/tools"**
    >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        **tools:replace="android:icon,android:theme"**
        >
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
Yogamurthy
  • 988
  • 15
  • 22
17

The missing piece for me was this:

xmlns:tools="http://schemas.android.com/tools"

for example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
    package="com.your.appid">
Abdul Rahman A Samad
  • 1,062
  • 1
  • 15
  • 21
partizan
  • 285
  • 2
  • 9
11

You can replace those in your Manifest application tag:

<application
    tools:replace="android:icon, android:label, android:theme, android:name,android:allowBackup"
android:allowBackup="false"...>

and will work for you.

  • 2
    It does not work for some reason.... I try to add `tools:replace="android:label"` to application but it does not work – iKK May 28 '19 at 18:03
7

The following hack works:

  1. add the xmlns:tools="http://schemas.android.com/tools" line in the manifest tag
  2. add tools:replace="android:icon,android:theme,android:allowBackup,label" in the application tag
Shoaib Ahmed
  • 757
  • 7
  • 7
5
 tools:replace="android:supportsRtl,android:allowBackup,icon,label">
Aqif
  • 326
  • 3
  • 5
  • The helps, but we can ONLY replace the needed tags, if we replace the tags we should not replace, an error will be shown, like: "tools:replace specified at line:14 for attribute android:supportsRtl, but no new value specified". – MadHatter Nov 29 '21 at 08:34
4

FIXED IT HAD THE EXACT ERROR, Just add this tools:replace="android:icon,android:theme"

into your application tag in your manifest, it works just fine,

Ismael ozil
  • 573
  • 5
  • 6
  • it's normal thing. what is topic about is not normal behavior happens in some cases when even tools:replace added. – David Oct 04 '16 at 05:54
4

You can replace those in your Manifest application tag:

<application
    ...
    tools:replace="android:label, android:icon, android:theme"/>

and will work for you.

Explanation

Using such a dependency/library in your gradle file which has those labels in its Manifest's application tag may produce this problem and replacing them in your Manifest is the solution.

blueware
  • 5,205
  • 1
  • 40
  • 60
4

My problem is multi modules project with base module, app module and feature module. Each module has AndroidManifest of its own, and I implemented build variant for debug and main. So we must sure that "android:name" just declared in Manifest of debug and main only, and do not set it in any of Manifest in child module. Ex: Manifest in main:

 <application
        android:name=".App"/>

Manifest in debug:

<application
        tools:replace="android:name"
        android:name=".DebugApp"
        />

Do not set "android:name" in other Manifest files like this:

<application android:name=".App">

Just define in feature module like this and it will merged fine

<application> 
Hoang Minh
  • 41
  • 1
1

I also went through this problem and changed that:

<application  android:debuggable="true" android:icon="@drawable/app_icon" android:label="@string/app_name" android:supportsRtl="true" android:allowBackup="false" android:fullBackupOnly="false" android:theme="@style/UnityThemeSelector">

to

 <application tools:replace="android:allowBackup" android:debuggable="true" android:icon="@drawable/app_icon" android:label="@string/app_name" android:supportsRtl="true" android:allowBackup="false" android:fullBackupOnly="false" android:theme="@style/UnityThemeSelector">
1

This is new androidManifest.xml for flutter

<application
    android:label="Your app Name"
    tools:replace="android:label"
    android:name="io.flutter.app.FlutterApplication"
    android:networkSecurityConfig="@xml/network_security_config"
    android:usesCleartextTraffic="true"
    android:icon="@mipmap/ic_launcher">

please make to add android:label in the first line in <application, 'cause if you are using this package flutter_app_name will throw an error if the not sorted like example Above

Rachid Loukili
  • 623
  • 6
  • 15
0

I was receiving a similar error on a project I was importing:

Multiple entries with same key: android:icon=REPLACE and tools:icon=REPLACE

Fixed after changing the below line within the application tag:

tools:replace="icon, label, theme"

to

tools:replace="android:icon, android:label, android:theme"
brybry
  • 163
  • 2
  • 9