工具:在 Android 清单中替换而不是替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25981156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tools: replace not replacing in Android manifest
提问by agrosner
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:
我正在使用具有许多不同库依赖项的 gradle 项目并使用新的清单合并。在我的<application />
标签中,我将其设置为:
<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
回答by Andrus
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. Good luck!
例如,我需要替换图标和标签。祝你好运!
回答by stefan K
I fixed same issue. Solution for me:
我修复了同样的问题。对我的解决方案:
- add the
xmlns:tools="http://schemas.android.com/tools"
line in the manifest tag - add
tools:replace=..
in the manifest tag - move
android:label=...
in the manifest tag
xmlns:tools="http://schemas.android.com/tools"
在清单标记中添加该行- 添加
tools:replace=..
清单标签 android:label=...
在清单标签中移动
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"/>
回答by GLee
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.
尝试在您的 gradle 文件中重新排序您的依赖项。我不得不将有问题的库从列表底部移到顶部,然后它就起作用了。
回答by Nantoka
I just experienced the same behavior of tools:replace=...
as described by the OP.
我刚刚经历了与tools:replace=...
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.
事实证明,tools:replace
清单合并被忽略的根本原因是此处描述的错误。这基本上意味着,如果您的项目中有一个包含清单的库,该清单的<application ...>
节点包含一个tools:ignore=...
属性,则tools:replace=...
主模块清单中的属性可能会被忽略。
The tricky point here is that it canhappen, 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.
这里的棘手之处在于它可能发生,但并非必须发生。在我的例子中,我有两个库,库 A 具有tools:ignore=...
属性,库 B 具有要在相应清单中替换的tools:replace=...
属性以及主模块清单中的属性。如果 B 的清单在 A 的清单之前合并到主清单中,一切都按预期进行。以相反的合并顺序出现错误。
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.
这些合并发生的顺序似乎有些随机。在我的情况下,更改依赖项部分中的顺序build.gradle
没有任何影响,但更改风味的名称却起到了作用。
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.
因此,唯一可靠的解决方法似乎是解压缩导致库的问题,删除tools:ignore=...
标记(这应该没问题,因为它只是对 lint 的提示)并再次打包库。
And vote for the bug to be fixed, of cause.
并投票支持修复错误。
回答by Yogamurthy
Final Working Solution for me (Highlighted the tages in the sample code):
我的最终工作解决方案(突出显示示例代码中的标签):
- add the
xmlns:tools
line in the manifest tag - add
tools:replace
in the application tag
xmlns:tools
在清单标记中添加该行- 添加
tools:replace
应用程序标签
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"**
>
回答by partizan
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">
回答by Saurabh Gaddelpalliwar
You can replace those in your Manifest application tag:
您可以替换 Manifest 应用程序标签中的那些:
<application
tools:replace="android:icon, android:label, android:theme, android:name,android:allowBackup"
android:allowBackup="false"...>
and will work for you.
并且会为你工作。
回答by blueware
You can replace those in your Manifest application
tag:
您可以替换 Manifestapplication
标签中的那些:
<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.
在您的gradle
文件中使用这样的依赖项/库,在其 Manifest 的应用程序标签中有这些标签可能会产生这个问题,而在您的文件中替换它们Manifest
就是解决方案。
回答by Ismael ozil
FIXED ITHAD THE EXACT ERROR, Just add this tools:replace="android:icon,android:theme"
修复它有确切的错误,只需添加这个工具:replace="android:icon,android:theme"
into your applicationtag in your manifest, it works just fine,
进入清单中的应用程序标签,它工作得很好,
回答by Shoaib Ahmed
The following hack works:
以下黑客工作:
- add the
xmlns:tools="http://schemas.android.com/tools"
line in the manifest tag - add
tools:replace="android:icon,android:theme,android:allowBackup,label"
in the application tag
xmlns:tools="http://schemas.android.com/tools"
在清单标记中添加该行- 添加
tools:replace="android:icon,android:theme,android:allowBackup,label"
应用程序标签