与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>
所以我将以下 block 添加到我的 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 中所述
It is recommended to use a hook or the custom config plugin to ensure that the above XML will automatically added in case you want to have a fresh checkout or remove/add the platform.
所以我去了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
However: recent versions of the Cordova/Phonegap CLI have added official support for and blocks in the config.xml (previously they only worked in plugin.xml).
So if all you want to do is insert a block of native config or change a native preference, you probably don't need this plugin at all.
我成功地使用 edit-config
将自定义属性从 config.xml
合并到 AndroidManifest.xml
中,如下面绿色所示。
但我仍然不知道如何使用 config-file
来更新 AndroidManifest.xml
,如上图红色所示。
❓ 所以问题是:关于 config-file
的 cordova 文档是否有误? 2018 年将自定义 intent-filter
添加到 AndroidManifest.xml
的最佳解决方案是什么?
我在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>
最佳答案
当您使用 cordova-custom-config
时设置多兄弟 <intent-filter>
元素,您必须确保它们具有唯一的标签属性作为插件 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
的作者
关于android - 配置文件在 Cordova 的 config.xml 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773280/