【问题标题】:edit-config is not working in config.xml in cordova编辑配置在科尔多瓦的 config.xml 中不起作用
【发布时间】:2017-08-07 19:59:10
【问题描述】:

我正在开发一个 Ionic 应用程序,我想通过 config.xml 在 AndroidManifest.xml 文件中添加 android@autoBackup="false"。 根据cordova文档,cordova 6.4版支持config.xml中的edit-config标签,通过它我可以实现类似于plugin.xml的功能。

这是我在 config.xml 文件中写的内容。

<platform name="android">
  <edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
    <application android:autoBackup="false" />
  </edit-config>
  <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
  <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
  <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
  <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
  <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
</platform>

它会像这样改变我的 AndroidManifest.xml 文件

<application android:autoBackup="false" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
  <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
    <intent-filter android:label="@string/launcher_name">
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <application android:autoBackup="false" />
  <activity android:exported="true" android:launchMode="singleTop" android:name="com.gae.scaffolder.plugin.FCMPluginActivity">
    <intent-filter>
      <action android:name="FCM_PLUGIN_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
  </activity>
  <service android:name="com.gae.scaffolder.plugin.MyFirebaseMessagingService">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
  <service android:name="com.gae.scaffolder.plugin.MyFirebaseInstanceIDService">
    <intent-filter>
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
  </service>
</application>

在上面的 AndroidManifest.xml 代码中,您可以很容易地发现应用程序标记已更新为 android@autoBackup="false" 并且还添加了应用程序标记作为第一个具有 autoBackup 属性的子项。 我不希望应用程序标签作为子标签,我尝试了很多但找不到这个问题的根本原因。 谁能建议我在这里做错了什么? 提前致谢。

【问题讨论】:

    标签: cordova ionic-framework


    【解决方案1】:

    尝试像这样更改编辑配置标签的“目标”属性:

    <edit-config file="AndroidManifest.xml" target="application" mode="merge">
    

    这告诉科尔多瓦你想修改一个名为“应用程序”的节点。有关 XPath 选择器的更多详细信息,请参阅XPath

    如果这不适合您,可以查看cordova-plugin-custom-config。使用这个插件,您可以简单地在 config.xml 的 android 部分添加一个偏好标签,例如 this:

    <preference name="android-manifest/@android:allowBackup" value="false" />
    

    【讨论】:

    • 我尝试了你的建议,但我仍然遇到同样的问题。我以前使用过的建议插件和它的工作预期,但我不想再使用这个插件了,即使我从“cordova-plugin-custom-config”插件文档here得到这个想法。
    • 能否尝试将目标属性更改为/*/manifest/*?也许它必须是一条绝对路径。请问使用custom-config插件有什么问题?
    • 更改目标属性后仍然遇到同样的问题。自定义配置插件没有具体原因,因为我们只需要向应用程序标签添加一个属性,而且我们现在使用的是cordova@6.4.0,这就是为什么我们试图在没有插件的情况下实现它。如果没有任何进展,我们可以继续使用自定义配置插件。非常感谢您一直以来的帮助。
    • 还有一件事要尝试:删除应用程序标签末尾的斜线:&lt;application android:autoBackup="false"&gt;。应用程序根标签不是自闭合标签,由于合并模式,它可能无法找到替换并添加它。
    • 在 XML 中我们需要关闭所有标签,它可以是自关闭的或者有类似的关闭标签。我已经尝试了两种方式,但对我来说没有成功的机会。我的团队仍在努力寻找根本原因,但由于时间短缺,我们计划继续使用自定义配置插件,直到我们得到其他解决方案。我非常感谢您的快速回复。此外,我还将监视此处发布的所有答案。如果我得到任何解决方案,我一定会在这里发布。我希望你能在这里发表你的建议。
    【解决方案2】:

    在我的情况下,我需要将其添加到小部件标签:xmlns:android="http://schemas.android.com/apk/res/android"

    感谢https://github.com/dpa99c/cordova-custom-config/issues/24#issuecomment-172981002修复

    【讨论】:

      【解决方案3】:
      <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
          <application android:allowBackup="false" />
      </edit-config>
      

      试试这个对我有用的。

      【讨论】:

      • 使用cordova-android 8.1为我指定了AndroidManifest.xml文件的路径
      【解决方案4】:

      对我来说这是可行的:

      <platform name="android">
          <preference name="android-targetSdkVersion" value="26" />
          <preference name="android-minSdkVersion" value="26" />
          <allow-intent href="market:*" />
          <preference name="ShowTitle" value="false" />
          <preference name="BackgroundColor" value="0xff2d2e2d" />
          <edit-config target="AndroidManifest.xml" parent="/manifest/application" mode="merge">
               <application android:theme="@android:style/Theme.NoTitleBar" >
               </application>
          </edit-config>
      </platform>
      

      【讨论】:

        【解决方案5】:

        The conflicting plugin, undefined, already modified the same attributes

        这是 Cordova 中的一个明确错误,并且错误声明 undefined 没有帮助。在我的情况下,只有一个插件使用了 edit-config,我清理了 android 平台,清空了 plugins 文件夹,在 package.json 中将有问题的插件移到了 cordova.plugins 部分的顶部。这使得cordova在所有其他插件之前添加了这个有问题的插件。

        这样做确实解决了问题,但最重要的是,它应该可以帮助您确定该列表中是否有第二罪犯。

        【讨论】:

          猜你喜欢
          • 2018-09-21
          • 1970-01-01
          • 2016-04-12
          • 1970-01-01
          • 2017-04-26
          • 1970-01-01
          • 1970-01-01
          • 2016-10-14
          • 1970-01-01
          相关资源
          最近更新 更多