一、概述

在写这篇文章时,cordova的版本已是9.0.0 。config.xml 是Cordova项目的全局配置文件。 这份配置文件的基础是W3C’s Packaged Web Apps (Widgets) 规范,并进行了扩展。它份配置文件是用来指定核心Cordova API 特性、插件、特定平台的设置等。

Config.xml文件位于用Cordova命令创建的项目的根目录下。如果你用以下命令创建项目:

~$ sudo cordova create ~/Desktop/Cordova/sample

那么config.xml就在~/Desktop/Cordova/sample/config.xml下。

当使用Cordova命令行创建项目时,这个文件会被复制到platforms/下的子目录下,如:​​/home/kyun/Desktop/Cordova/sample/platforms/android/app/src/main/res/xml/config.xml​

二、config.xml中的标签

我们只挑一些重要的与Android相斗的标签和属性来介绍
widget标签

<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

如果在Android平台上运行的,不要忘了添加Android的命名空间:

xmlns:android="http://schemas.android.com/apk/res/android"

属性

描述

id(string)

必要的,在Android就是包名,一般是反向写的域名

version(string)

必要的,完整版本号,版本号一般这样组成会较有意义:以主/次/补丁符号表示,如1.1.3表示当前版本是主版本1,次版本为1的第三个3补丁包

xmlns(string)

必要的, config.xml文档的命名空间。

xmlns:cdv(string)

必要的,命名空间前缀

android-versionCode(string)

Android的替代版本

android-packageName(string)

android的替代包名,覆盖id。 一般不用。

android-activityName(string)

defaultlocale

指定应用程序的默认语言,作为IANA语言代码。

name标签

<widget ...>
<name>Cordova</name>
</widget>

指定应用程序的正式名称,该名称显示在设备的主屏幕

content标签

<widget ...>
<content src="index.html" />
</widget>

定义app的顶层web assets目录的起始页,默认值是index.html,它通常出现在项目的顶级www目录中。

access标签

<widget ...>
<access origin="*" />
</widget>

定义允许应用程序与之通信的外部域集。

allow-intent标签

<widget ...>
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
</widget>

定义允许应用程序请求系统打开的URL。

allow-navigation标签

<widget ...>
<!-- 允许链接到example.com -->
<allow-navigation href="http://example.com/*" />
<!-- 协议允许通配符作为主机的前缀或路径的后缀。 -->
<allow-navigation href="*://*.example.com/*" />
</widget>

定义允许WebView导航到的外部域集。

platform标签

<widget ...>
<platform name="android">
<allow-intent href="market:*" />
<preference name="Fullscreen" value="true" />
<config-file parent="/*" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
</config-file>
</platform>
</widget>

定义指定平台的首选项。

resource-file标签

<widget ...>
<resource-file src="FooPluginStrings.xml" target="res/values/FooPluginStrings.xml" />
</widget>

属性

描述

src(string)

文件相对于config.xml的位置

target(string)

将文件复制到目录中的路径

将资源文件安装到平台中
hook标签

<widget ...>
<hook type="after_plugin_install" src="scripts/afterPluginInstall.js" />
</widget>

属性

描述

type(string)

指定调用自定义脚本的操作。

src(string)

指定发生特定操作时要调用的脚本的位置

表示当发生某些操作(例如,在添加插件或调用平台准备逻辑之后)时Cordova将调用的自定义脚本。
preference标签

<widget ...>
<preference name="DisallowOverscroll" value="true"/>
<preference name="Fullscreen" value="true" />
<preference name="BackgroundColor" value="0xff0000ff"/>
<preference name="HideKeyboardFormAccessoryBar" value="true"/>
<preference name="Orientation" value="landscape" />
<!-- Android only preferences -->
<preference name="KeepRunning" value="false"/>
<preference name="LoadUrlTimeoutValue" value="10000"/>
<preference name="InAppBrowserStorageEnabled" value="true"/>
<preference name="LoadingDialog" value="My Title,My Message"/>
<preference name="ErrorUrl" value="myErrorPage.html"/>
<preference name="ShowTitle" value="true"/>
<preference name="LogLevel" value="VERBOSE"/>
<preference name="AndroidLaunchMode" value="singleTop"/>
<preference name="DefaultVolumeStream" value="call" />
<preference name="OverrideUserAgent" value="Mozilla/5.0 My Browser" />
<preference name="AppendUserAgent" value="My Browser" />
</widget>

将各种选项设置为名称/值属性对。每个首选项的名称都不区分大小写。许多偏好是特定平台所独有的,并将以这样的方式表示。

属性

描述

Orientation(string)

允许值:default, landscape(横向值), portrait(纵向值)。允许您锁定方向并防止界面根据方向的更改而旋转,注意:默认值意味着cordova将从平台的manifest/configuration文件中删除orientation首选项条目,允许平台返回到其默认行为。

LoadUrlTimeoutValue(number in milliseconds)

默认值:20000,20秒。加载页时,引发超时错误之前等待的时间量。

KeepRunning(boolean)

默认值:true。确定应用程序是否在暂停事件激发后仍在后台运行。将此设置为false不会在暂停事件后终止应用程序,而只是在应用程序处于后台时停止在Cordova WebView中执行代码。

InAppBrowserStorageEnabled (boolean)

默认值:true。控制在InAppBrowser中打开的页是否可以访问与使用默认浏览器打开的页相同的本地存储和WebSQL存储

FullScreen(boolean)

默认值:false。允许您隐藏屏幕顶部的状态栏。注意:建议使用statusbar插件来实现这一点。

ErrorUrl(URL)

默认值:null。如果设置,将在应用程序出错时显示引用页,而不是显示标题为“应用程序错误”的对话框。

BackgroundColor(string)

设置应用程序的背景色。

AppendUserAgent(string)

如果设置,该值将追加到webview的旧useragent的末尾。与OverrideUserAgent一起使用时,将忽略此值。

android-targetSdkVersion(integer)

默认:依赖于Cordova Android版本。在项目的androidmanifest.xml中设置标记的targetsdkversion属性

AndroidLaunchMode(string)

默认值:singletop。允许值:standard、singletop、singletask、singleinstance。设置活动android:launchmode属性。这将更改从app icon或intent启动应用程序并已在运行时发生的情况。

plugin标签

<widget ...>
<plugin name="cordova-plugin-device" spec="^1.1.0" />
<plugin name="cordova-plugin-device" spec="https://github.com/apache/cordova-plugin-device.git#1.0.0" />

</widget>

指定准备期间要还原的插件的详细信息。使用–save标志添加插件时,此元素会自动添加到项目的config.xml中。

属性

描述

name(string)

要还原的插件的名称

spec(string)

有关要还原的插件的详细信息。这可能是major.minor.patch版本号、包含插件的目录或指向git存储库的url。此信息将用于从npm、本地目录或git存储库检索要还原的插件代码。

engine标签

<widget ...>
<engine name="android" spec="https://github.com/apache/cordova-android.git#5.1.1" />
<engine name="ios" spec="^4.0.0" />
</widget>

指定准备期间要还原的平台的详细信息。

属性

描述

name(string)

要还原的平台的名称

spec(string)

有关要还原的平台的详细信息。这可能是major.minor.patch版本号、包含平台的目录或指向git存储库的url。此信息将用于从npm、本地目录或git存储库检索要还原的平台代码。

上面就是与Android平台比较密切相关的配置信息。

谢谢阅读。