【问题标题】:Tools: replace not replacing in Android manifest工具:在 Android 清单中替换不替换
【发布时间】:2014-09-22 18:50:28
【问题描述】:

我正在使用具有许多不同库依赖项的 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>

但我收到错误消息:

/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

【问题讨论】:

标签: android android-gradle-plugin


【解决方案1】:

像这样声明你的清单标头

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

然后你可以在你的应用标签中添加以下属性:

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

例如我需要替换图标和标签。祝你好运!

【讨论】:

  • 如何处理带有元数据的 android:name? android:name="com.orm.SugarApp"> 跨度>
  • @Alan 只需将整个元数据标签替换为 tools:node="replace": &lt;meta-data tools:node="replace" android:name="QUERY_LOG" android:value="true" /&gt; 等。
  • 我很困惑。这不正是他在他的问题中所做的吗?
  • 如果被替换的属性在manifest标签中,tools:replace="android:versionCode, android:versionName"必须在&lt;manifest &gt;标签内。
  • 请告诉我一件事工具:替换我应该在我的项目清单的应用程序标签内还是在我的库的应用程序标签内添加这个请告诉我这对我真的有帮助吗??
【解决方案2】:

我解决了同样的问题。我的解决方案:

  1. 在清单标签中添加xmlns:tools="http://schemas.android.com/tools"
  2. 在清单标签中添加tools:replace=..
  3. 在清单标签中移动android:label=...

例子:

 <?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"/>

【讨论】:

  • 感谢您,将其直接添加到 &lt;manifest&gt; 而不是在 &lt;application&gt; 下是解决我问题的唯一方法。
  • 我没有为我工作,我应该放 tools:replace="android:allowBackup, android:label" 而不仅仅是 tools:replace="allowBackup, label"。
  • 你确定这有效吗?因为对我来说,看起来你在错误的地方写了 allowBackup ,并且在合并之后,图书馆在他的清单上指定的内容占上风。将其移到顶部后不会发生冲突,因为您只是做错了(developer.android.com/guide/topics/manifest/…) AllowBackup 属于 Application 元素。
  • [...从上面继续] 这不起作用。您在错误的位置编写了 allowBackup,并且在合并后,图书馆在他的清单上指定的内容占上风。将其移到顶部后不会发生冲突,因为您只是做错了(developer.android.com/guide/topics/manifest/…) AllowBackup 属于 Application 元素。我已经通过反编译 APK 对其进行了测试,最终清单的值为 TRUE 而不是 FALSE,正如我按照您的建议指定的那样
【解决方案3】:

尝试在 gradle 文件中重新排序依赖项。我不得不将有问题的库从列表的底部移到顶部,然后它就起作用了。

【讨论】:

  • 请告诉我一件事工具:替换我应该在我的项目清单的应用程序标签内还是在我的库的应用程序标签内添加这个请告诉我这对我真的有帮助吗??
  • @SudhanshuGaur 您只需将其添加到项目的 AndroidManifest.xml 文件中即可。
  • 成功了。但这样的解决方案可能会让人类失去信心。
  • #android 诸如此类的陷阱让为 android 开发非常烦人
  • 如果您有多种产品风格,例如 play 和 non play,并且依赖项是“playReleaseCompile ______”,仅将语句移到顶部可能不起作用,您可能需要将语句更改为“compile ______”,然后将其移至顶部。那么它应该可以工作了。
【解决方案4】:

我刚刚经历了与 OP 描述的 tools:replace=... 相同的行为。

事实证明,tools:replace 被清单合并忽略的根本原因是 here 描述的错误。这基本上意味着,如果您的项目中有一个库,其中包含带有 &lt;application ...&gt; 节点的清单,该节点包含 tools:ignore=... 属性,则可能会忽略主模块清单中的 tools:replace=... 属性。

这里的棘手之处在于它可以发生,但并非必须发生。就我而言,我有两个库,库 A 具有 tools:ignore=... 属性,库 B 具有要在各自清单中替换的属性,以及主模块清单中的 tools:replace=... 属性。如果 B 的清单在 A 的清单之前合并到主清单中,则一切都按预期工作。以相反的合并顺序出现错误。

这些合并发生的顺序似乎有些随机。在我的情况下,更改 build.gradle 的依赖项部分中的顺序没有任何效果,但更改风味的名称就可以了。

因此,唯一可靠的解决方法似乎是解压缩导致库的问题,删除 tools:ignore=... 标记(这应该没问题,因为它只是 lint 的提示)并再次打包库。

并投票支持要修复的错误。

【讨论】:

  • 在我的情况下,我必须从我的项目中删除工具:忽略,而不是从某个库中
  • 这也是我的问题。将 tools:ignore 从&lt;application&gt; 节点移至&lt;manifest&gt; 节点,它工作正常。
  • 刚刚遇到与 Bazel v2.0.0 相同的问题,在这里提交了一个错误 github.com/bazelbuild/bazel/issues/10543
【解决方案5】:

我的最终工作解决方案(突出显示示例代码中的标记):

  1. 在清单标签中添加xmlns:tools
  2. 在应用标签中添加tools:replace

例子:

<?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"**
        >

【讨论】:

    【解决方案6】:

    对我来说缺少的部分是:

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

    例如:

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

    【讨论】:

    • 您能否准确地分享您在 中的代码?
    【解决方案7】:

    您可以替换 Manifest 应用程序标签中的那些:

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

    并且会为你工作。

    【讨论】:

    • 由于某种原因它不起作用......我尝试将tools:replace="android:label"添加到应用程序但它不起作用
    【解决方案8】:

    已修复 有确切的错误,只需添加此工具:replace="android:icon,android:theme"

    进入清单中的 application 标签, 它工作得很好,

    【讨论】:

    • 这很正常。在某些情况下,即使添加了 tools:replace 也会发生不正常的行为。
    【解决方案9】:

    您可以替换 Manifest application 标签中的那些:

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

    并且会为你工作。

    说明

    在您的 gradle 文件中使用此类在其 Manifest 的应用程序标记中具有这些标签的依赖项/库可能会产生此问题,并在您的 Manifest 中替换它们是解决方案。

    【讨论】:

      【解决方案10】:

      以下 hack 有效:

      1. xmlns:tools="http://schemas.android.com/tools" 行添加到 清单标签
      2. 添加 tools:replace="android:icon,android:theme,android:allowBackup,label" 在应用程序标签中

      【讨论】:

      • tools:replace specified at line:15 for attribute android:theme, but no new value specified
      【解决方案11】:

      我的问题是包含基本模块、应用程序模块和功能模块的多模块项目。 每个模块都有自己的 AndroidManifest,我为调试和主实现了构建变体。 所以我们必须确保“android:name”只是在 debug 和 main 的 Manifest 中声明,并且不要在子模块的任何 Manifest 中设置它。 前任: 主要清单:

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

      调试中的清单:

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

      不要像这样在其他 Manifest 文件中设置“android:name”:

      <application android:name=".App">
      

      只要像这样在功能模块中定义它就会很好地合并

      <application> 
      

      【讨论】:

        【解决方案12】:
         tools:replace="android:supportsRtl,android:allowBackup,icon,label">
        

        【讨论】:

        • 有帮助,但我们只能替换需要的标签,如果我们替换了我们不应该替换的标签,则会显示错误,例如:“tools:replace specified at line:14 for attribute android :supportsRtl,但没有指定新值”。
        【解决方案13】:

        我也遇到了这个问题并改变了:

        <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">
        

         <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">
        

        【讨论】:

          【解决方案14】:

          我在导入的项目中收到了类似的错误:

          具有相同键的多个条目:android:icon=REPLACE 和 工具:图标=替换

          在更改应用程序标记中的以下行后修复:

          tools:replace="icon, label, theme"
          

          tools:replace="android:icon, android:label, android:theme"
          

          【讨论】:

            【解决方案15】:

            这是 Flutter 的新 androidManifest.xml

            <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">
            

            请在

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-01-03
              • 1970-01-01
              • 1970-01-01
              • 2022-12-14
              • 2013-08-27
              • 2017-01-16
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多