本系列文章分析基于node.js的命令行工具Cordova CLI,所以如果对node.js基础不是很了解,建议参考http://nodejs.gamesys.net/node-js提供的基础教程

文中提到的包和模块是同一概念

1、简介

Cordova CLI是一个基于node.js的命令行工具,用于编译、部署和管理跨平台的Cordova 混合应用程序。

Apache Cordova 使开发者运用Html, CSS,javascript就能够构建原生移动应用

支持平台

l Android

l BlackBerry 10

l iOS

l Windows Phone 7 & 8

运行环境

Node.js

各平台SDK

安装

npm install -g cordova

默认安装路径

C:\Documents and Settings\xxx\Application Data\npm\node_modules\cordova\src

2、命令参数

全局命令

create <directory> [<id> [<name>]] 创建一个新的 cordova项目,三个参数分别是 保存路径,项目id,项目名称(反域名格式com.xxx.xxxx)

项目命令

platform [ls | list] list all platforms the project will build to

platform add <platform> [<platform> ...] add one (or more) platforms as a build target for the project

platform [rm | remove] <platform> [<platform> ...] removes one (or more) platforms as a build target for the project

plugin [ls | list] list all plugins added to the project

plugin add <path-to-plugin> [<path-to-plugin> ...] add one (or more) plugins to the project

plugin [rm | remove] <plugin-name> [<plugin-name> ...] remove one (or more) added plugins

prepare [platform...] copies files into the specified platforms, or all platforms. it is then ready for building by Eclipse/Xcode/etc.

compile [platform...] compiles the app into a binary for each added platform. With no parameters, builds for all platforms, otherwise builds for the specified platforms.

build [<platform> [<platform> [...]]] an alias for cordova prepare followed by cordova compile

emulate [<platform> [<platform> [...]]] launch emulators and deploy app to them. With no parameters emulates for all platforms added to the project, otherwise emulates for the specified platforms

serve <platform> [port] launch a local web server for that platform's www directory on the given port (default 8000).

可选参数

-d或--verbose 添加调式信息输出

-v 或--version 输出cordova-cli版本信息

3、目录结构

运行cordova create hello hellotest com.xxx.hellotest

hello/

|--.cordova/

| | -- hooks/

| | -- config.json

|-- merges/

| | -- android/

| | -- blackberry10/

| | -- ios/

|-- www/

| `-- config.xml

|-- platforms/

| |-- android/

| |-- blackberry10/

| `-- ios/

`-- plugins/

刚创建完的merges,platforms,plugins都是空目录,而.cordova/config.json包含create创建时指定的参数{"id":"hellotest","name":"com.xxx.hellotest"}

(1).cordova目录是辨别是否是cordova项目的重要标志,存储一些配置文件和信息;

(2)www目录下存放的是各个平台上一致的web文件,即每个平台使用相同代码,当调用prepare命令时,拷贝此目录下内容到platforms下原生目录;

config.xml是符合W3C's Packaged Web Apps (Widgets) 标准的配置文件,通过修改config.xml中参数值,cordova-cli完成应用参数的修改

示例:

<?xml version='1.0' encoding='utf-8'?>

<widget id="hellotest" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">

<name>com.jinkai.hellotest</name>

<description>

A sample Apache Cordova application that responds to the deviceready event.

</description>

<author email="dev@callback.apache.org" href="http://cordova.io">

Apache Cordova Team

</author>

<content src="index.html" />

<access origin="*" />

<preference name="fullscreen" value="true" />

<preference name="webviewbounce" value="true" />

<plugins>

<plugin name="MyPlugin" value="MyPluginClass" />

</plugins>

<content src="index.html" />

</widget>

其中主要有以下几个参数:

<name> 展示给用户的应该程序名称

<widget> 中id属性指定包名(bundle identifier 或application id)

<widget> 中version属性版本

<access> 中 origin 属性,指定网络访问白名单

<preference>  用于存储一些键值对参数

<plugin> 定义原生插件,当运行时Cordova framework确保应用可以调用设备的API接口

<content> 定义web起始页,默认值是index.html

(3)merges目录下存放的是有平台差异的web文件,当调用prepare命令时,拷贝此目录下内容到platforms下原生目录,如果有同名www目录下文件存在,merges目录下文件将覆盖www下文件;

例如存在以下目录结构

merges/

|-- ios/

| `-- app.js

|-- android/

| `-- android.js

www/

`-- app.js

最终编译时,platforms目录下,android目录包含android.js和app.js,iOS目录包含app.js,这个文件与merges目录下ios/app.js一致,即merges目录下文件覆盖www目下文件

(4)platforms 各平台原生应用程序目录结构

(5)plugins 所有插件将会解压后放在此目录

(6)hooks 定义一系列回调函数

包含两类:项目指定和模块指定函数

4、Cordova CLI使用流程

(1)创建应用 cordova create (创建应用程序骨架)

cordova create hello hellotest com.jinkai.hellotest

(2)添加平台 cordova platform add

cd hello

Mac支持平台

$ cordova platform add ios

$ cordova platform add android

$ cordova platform add blackberry10

Windows支持平台

$ cordova platform add wp7

$ cordova platform add wp8

$ cordova platform add android

$ cordova platform add blackberry10

查看支持平台

cordova platform ls

删除

$ cordova platform remove blackberry10

$ cordova platform rm android

(3)编译cordova build(在platforms目录下生成平台代码)

$ cordova build ios

等价于顺序执行以下两个步骤

 $ cordova prepare ios
$ cordova compile ios

(4)测试

$ cordova emulate android

(5)添加插件

安装插件cordova plugin add

Basic device information (Device API):

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

Network Connection and Battery Events:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status.git

Accelerometer, Compass, and Geolocation:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git

Camera, Media playback and Capture:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-media.git

Access files on device or network (File API):

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git

Notification via dialog box or vibration:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-dialogs.git

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration.git

Contacts:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts.git

Globalization:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization.git

Splashscreen:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-splashscreen.git

Open new browser windows (InAppBrowser):

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git

Debug console:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git

查看已安装插件plugin ls 或plugin list 或plugin

$ cordova plugin ls    # or 'plugin list'
[ 'org.apache.cordova.core.console' ]

删除插件

$ cordova plugin rm org.apache.cordova.core.console
$ cordova plugin remove org.apache.cordova.core.console # same

Cordova CLI源码分析(一)——简介的更多相关文章

  1. Cordova CLI源码分析(三)——初始化

    本部分主要涉及以下三个文件 1 cli.js 2 cordova.js 3 events.js 通过前一篇package.json的分析,可以知道,当命令行执行cordova相关命令时,首先调用mai ...

  2. Cordova CLI源码分析(四)——创建工程

    在第一篇分析我们曾经举例,创建一个新工程, cordova create hello hellotest com.xxx.hellotest cli.js文件分析命令行参数后,会走到 else if  ...

  3. Cordova CLI源码分析(二)——package.json

    每个包需要在其顶层目录下包含一个package.json文件,该文件不仅是包的说明,也影响npm安装包时的配置选项 更多参数详见参考文档https://npmjs.org/doc/json.html ...

  4. Appium Android Bootstrap源码分析之简介

    在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...

  5. kubelet源码分析——kubelet简介与启动

    kubelet是k8s集群中一个组件,其作为一个agent的角色分布在各个节点上,无论是master还是worker,功能繁多,逻辑复杂.主要功能有 节点状态同步:kublet给api-server同 ...

  6. kubelet源码分析——关闭Pod

    上一篇说到kublet如何启动一个pod,本篇讲述如何关闭一个Pod,引用一段来自官方文档介绍pod的生命周期的话 你使用 kubectl 工具手动删除某个特定的 Pod,而该 Pod 的体面终止限期 ...

  7. Appium Server 源码分析之启动运行Express http服务器

    通过上一个系列Appium Android Bootstrap源码分析我们了解到了appium在安卓目标机器上是如何通过bootstrap这个服务来接收appium从pc端发送过来的命令,并最终使用u ...

  8. Appium Android Bootstrap源码分析之控件AndroidElement

    通过上一篇文章<Appium Android Bootstrap源码分析之简介>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一个初步的 ...

  9. ArrayList相关方法介绍及源码分析

    目录 ArrayList简介: ArrayList 相关方法介绍 代码表示 相关方法源码分析 ArrayList简介: java.util.ArrayList 是我们最常用的一个类,ArrayList ...

随机推荐

  1. EasyUI - Resizable 调整大小

    效果: html代码: <div id="rr" style="width: 100px; height: 100px; border: 2px solid #cc ...

  2. 大数据实时处理-基于Spark的大数据实时处理及应用技术培训

    随着互联网.移动互联网和物联网的发展,我们已经切实地迎来了一个大数据 的时代.大数据是指无法在一定时间内用常规软件工具对其内容进行抓取.管理和处理的数据集合,对大数据的分析已经成为一个非常重要且紧迫的 ...

  3. CloudStack搭建KVM环境

    软件环境:agent:CentOS 6.3,minimal安装,CPU启用VT management server:CentOS 6.3,minimal安装 存储:CentOS 6.3 搭建在mana ...

  4. spring获取bean 实例

    ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...

  5. Python 学习入门(23)—— 进程

    本文介绍Python的os包中有查询和修改进程信息的函数,Python的这些工具符合Linux系统的相关概念,所以可以帮助理解Linux体系. 1. 进程信息 os包中相关函数如下: uname()  ...

  6. 【Demo 0016】SQLite 数据库

    本章学习要点:       1.  熟悉SQL语句:       2.  掌握SQLit库的基本用法;        3.  掌握SQLite封装:

  7. Swift - 使用导航条和导航条控制器来进行页面切换

    通过使用导航条(UINavigationBar)与导航条控制器(UINavigationController)可以方便的在主页面和多层子页面之间切换.下面通过一个简单“组件效果演示”的小例子来说明如何 ...

  8. 使用简单的 5 个步骤设置 Web 服务器集群

    通过在多个处理器之间分担工作负载并采用多种软件恢复技术,能够提供高度可用的环境并提高环境的总体 RAS(可靠性.可用性和可服务性).可以得到的好处包括:更快地从意外中断中恢复运行,以及将意外中断对终端 ...

  9. 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密

    原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密 [索引页][源码下载] 与众不同 wi ...

  10. 与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信

    原文:与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信 [索引页][源码下载] 与众不同 windows phone (29) - Co ...