【问题标题】:config-file is not working in config.xml in cordova配置文件在科尔多瓦的 config.xml 中不起作用
【发布时间】:2018-09-21 05:35:36
【问题描述】:

edit-config is not working in config.xml in cordova相关

❯ cordova --version
6.5.0

我想在AndroidManifest.xml中添加如下配置

<intent-filter>
  <action android:name="android.intent.action.SEND" />
  <action android:name="android.intent.action.SEND_MULTIPLE" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="*/*" />
</intent-filter>

我检查了the official cordova docs,下面是这个例子

<config-file target="AndroidManifest.xml" parent="/manifest/application">
  <activity android:name="com.foo.Foo" android:label="@string/app_name">
    <intent-filter></intent-filter>
  </activity>
</config-file>

所以我将以下块添加到我的config.xml 文件中

<config-file target="AndroidManifest.xml" platform="android" parent="/manifest/application/activity" mode="merge">
  <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
  </intent-filter>
</config-file>

????没有任何成功。

注意:我使用this plugin darryncampbell-cordova-plugin-intent,它在其plugin.xml 文件中使用config-file 标签。但是同一标签在 config.xml 文件中不起作用。

到目前为止一切顺利,我更努力地尝试了另一个文档。如the deprecated plugin cordova-plugin-intent中所述

建议使用钩子或自定义配置插件,以确保上述 XML 将自动添加,以防您想要重新结帐或删除/添加平台。

所以我去了the recommended plugin cordova-custom-config,也列出了the related question about edit-config tag。还有the documentation of this plugin tells me the exact same as the official cordova documentation about config-file tag in config.xml

但是:最新版本的 Cordova/Phonegap CLI 在 config.xml 中添加了官方支持和块(以前它们只在 plugin.xml 中工作)。

因此,如果您只想插入一个原生配置块或更改原生偏好,那么您可能根本不需要这个插件。

我成功使用edit-config将自定义属性从config.xml合并到AndroidManifest.xml,如下图绿色部分。

但是我还是不知道怎么用config-file更新AndroidManifest.xml如上图红色部分。

❓ 所以问题是:关于config-file 的科尔多瓦文档有误吗? 2018年将自定义intent-filter添加到AndroidManifest.xml的最佳解决方案是什么?


注意cordova-custom-config

我加了config.xml

<platform name="android">
  <allow-intent href="market:*" />
  <custom-config-file target="AndroidManifest.xml" parent="./application/activity">
    <intent-filter>
      <action android:name="android.intent.action.SEND" />
      <action android:name="android.intent.action.SEND_MULTIPLE" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="*/*" />
    </intent-filter>
  </custom-config-file>
</platform>

插件将自定义配置从config.xml 应用到AndroidManifest.xml

<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
  <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
  </intent-filter>
  <intent-filter>
    <action android:name="com.darryncampbell.cordova.plugin.intent.ACTION" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

但是自定义的intent-filter 没有附加到intent-filter 列表中,而是替换了第一个

<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" 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>
  <intent-filter>
    <action android:name="com.darryncampbell.cordova.plugin.intent.ACTION" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

【问题讨论】:

    标签: android cordova


    【解决方案1】:

    (这将是一个评论,但我没有足够的代表)

    我确认:&lt;config-file&gt; 在 Cordova 7.0 上不起作用(尽管 &lt;edit-config&gt; 可以),请参阅我的 config.xml

    <?xml version='1.0' encoding='utf-8'?>
    <widget ...>
        <platform name="android">
            <config-file parent="/manifest/application" target="AndroidManifest.xml">
                <test1 />
            </config-file>
            <config-file parent="./application" target="AndroidManifest.xml">
                <test2 />
            </config-file>
        </platform>
    

    ... 被完全忽略。

    只需安装 cordova-custom-config 即可解决问题。 你只需要使用像./application 这样的相对XPath,否则你会得到一个错误:

    cordova-custom-config: Error updating config for platform 'android': Cannot use absolute path on element
    

    【讨论】:

      【解决方案2】:

      当您使用cordova-custom-config 设置多兄弟&lt;intent-filter&gt; 元素时,您必须确保它们具有作为插件distinguishes them by label 的唯一标签属性。您可以在the example project 中看到这一点。

      所以是这样的:

      <custom-config-file target="AndroidManifest.xml" parent="./application/activity/[@android:name='MainActivity']">
          <intent-filter android:label="custom_filter">
              <action android:name="android.intent.action.SEND" />
              <action android:name="android.intent.action.SEND_MULTIPLE" />
              <category android:name="android.intent.category.DEFAULT" />
              <data android:mimeType="*/*" />
          </intent-filter>
      </custom-config-file>
      

      免责声明:我是cordova-custom-config的作者

      【讨论】:

      • ❤️ 非常感谢您的关心!我尝试在我的自定义intent-filter 标签上设置android:label。但是带有android:label="@string/launcher_name" 的那个(请参阅我的问题的详细信息和上下文)仍然被替换。没有它,应用程序将不再显示在启动器上。
      • 您可能需要设置父级,使其与what the plugin is expecting 完全匹配 - 请参阅我的更新答案中的父级属性值
      • 通过更改parent 属性,它就像一个魅力!非常感谢!
      • 我在等着看是否会出现有关 cordova 内置支持的答案,否则我会验证您的答案是否允许继续前进。
      • Cordova CLI 中内置 &lt;config-file&gt; 支持的当前问题是 config.xml 中的 &lt;config-file&gt; 块在 Cordova 在本机配置中设置的默认值之前应用,这意味着如果您的 &lt;config-file&gt; 块修改本机配置的相同部分作为 Cordova 的默认设置,当 Cordova 设置其默认设置时,您的更改将被清除。 cordova-custom-config 通过在 after_prepare 钩子上应用更改来解决此问题,即在 Cordova 之后
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2017-09-20
      • 2015-10-14
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多