譯:cordova

http://cordova.apache.org/docs/en/3.5.0/guide_cli_index.md.html#The%20Command-Line%20Interfacecss

  cordova命令界面(CLI)向你展現了怎樣建立一個app,而且在各類各類的本地移動平臺部署他們。這個工具容許你建立一個新的項目,編譯到不一樣的平臺上面,而後在真實的設備上或者模擬器裏運行。CLI是在概述裏面描述的跨平臺工做流的主要工具。然而,你也可使用CLI來初始化項目代碼,而後切換到各類平臺的SDK和shell工具來繼續開發。html

Prerequisites

在運行任何命令行工具以前,你須要安裝目標平臺的SDK(See the Platform Guidesfor more details.)node

爲了給任何一個平臺添加一個支持或者重建的項目,你須要在支持這個平臺的SDK的機器上運行命令行。 命令工具支持下列系統:android

  • iOS (Mac)
  • Amazon Fire OS (Mac, Linux, Windows)
  • Android (Mac, Linux, Windows)
  • BlackBerry 10 (Mac, Linux, Windows)
  • Windows Phone 8 (Windows)
  • Windows 8 (Windows)
  • Firefox OS (Mac, Linux, Windows)

On the Mac, the command-line is available via the Terminal application. On the PC, it's available as Command Prompt underAccessories.ios

NOTE: For Windows-only platforms, you can still do your development on Mac hardware by running Windows in a virtual machine environment or in dual-boot mode. For available options, see the Windows Phone Platform Guide or the Windows 8 Platform Guide.git

The more likely it is that you run the CLI from different machines, the more it makes sense to maintain a remote source code repository, whose assets you pull down to local working directories.github

安裝Cordova CLI

The Cordova command-line tool is distributed as an npm package in a ready-to-use format. It is not necessary to compile it from source.web

要安裝cordova命令行工具,請按照下面步驟操做:shell

  1. 下載並安裝 Node.js.。按照步驟安裝以後,你能夠在你的命令行調用 node 和 npm 。若是安裝成功,你能夠選擇使用像 nvm 或 nave 這樣的工具來管理你的 Node.js 安裝配置。apache

  2. 下載並安裝一個 git 客戶端,若是你沒有的話。按照步驟安裝以後,你能夠在你的命令行調用  git 命令。即便你不會手動使用 git 命令,在建立一個新項目的時候CLI會在後臺使用它來下載一些資源。

  3. 使用 npm 安裝 cordova 模塊。cordova 模塊將會自動被下載.

  • 在 OS X 和Linux上:

       $ sudo npm install -g cordova
    On OS X and Linux, prefixing the  command with  may be necessary to install this development utility in otherwise restricted directories such as . If you are using the optional nvm/nave tool or have write access to the install directory, you may be able to omit the  prefix. There are more tips available on using  without , if you desire to do that.npmsudo/usr/local/sharesudonpmsudo
  • 在 Windows系統上:

       C:\>npm install -g cordova
     

  上面命令中的 -g 標誌告訴 npm 全局安裝 cordova 。若是沒有這個標誌, cordova 將會安裝在當前工做目錄下的 node_modules 子目錄。

  爲了全局調用 npm 安裝模塊,你可能須要把 npm 的目錄添加到你的環境變量 PATH 中。在Windows系統中, npm 通常在 C:\Users\username\AppData\Roaming\npm。在 OS X 和Linux 系統中,通常在/usr/local/share/npm目錄下。

      The installation log may produce errors for any uninstalled platform SDKs.

  Following installation, you should be able to run cordova on the command line with no arguments and it should print help text.

建立一個App

  切換到你維護的源碼所在目錄,運行以下命令:

    $ cordova create hello com.example.hello HelloWorld

 

It may take some time for the command to complete, so be patient. Running the command with the -d option displays information about its progress.

The first argument hello specifies a directory to be generated for your project. This directory should not already exist, Cordova will create it for you. Its www subdirectory houses your application's home page, along with various resources under cssjs, and img, which follow common web development file-naming conventions. The config.xml file contains important metadata needed to generate and distribute the application.

The second argument com.example.hello provides your project with a reverse domain-style identifier. This argument is optional, but only if you also omit the third argument, since the arguments are positional. You can edit this value later in the config.xml file, but do be aware that there may be code generated outside of config.xml using this value, such as Java package names. The default value is io.cordova.hellocordova, but it is recommended that you select an appropriate value.

The third argument HelloWorld provides the application's display title. This argument is optional. You can edit this value later in the config.xml file, but do be aware that there may be code generated outside of config.xml using this value, such as Java class names. The default value is HelloCordova, but it is recommended that you select an appropriate value.

Add Platforms添加平臺

  後面全部的命令須要在項目目錄下運行,或者他範圍內任意子目錄,因此咱們切換到項目目錄下面:

    $ cd hello

 

  在你能編譯項目以前,你須要指定一系列的目標平臺。你是否能運行這些命令,徹底取決於你的機器是否支持這些每個SDK,或者你是否已經安裝這些每個SKD。在Mac下面運行以下命令:

   $ cordova platform add ios
    $ cordova platform add amazon-fireos
    $ cordova platform add android
    $ cordova platform add blackberry10
    $ cordova platform add firefoxos

 

  在windows系統下面運行下面命令,wp 指的是Windows Phone 操做系統的不一樣版本:

    $ cordova platform add wp8
    $ cordova platform add windows8
    $ cordova platform add amazon-fireos
    $ cordova platform add android
    $ cordova platform add blackberry10
    $ cordova platform add firefoxos

Run this to check your current set of platforms:

 
  $ cordova platforms ls
 

(Note the platform and platforms commands are synonymous.)

Run either of the following synonymous commands to remove a platform:

   
    $ cordova platform remove blackberry10
    $ cordova platform rm amazon-fireos
    $ cordova platform rm android

Running commands to add or remove platforms affects the contents of the project's platforms directory, where each specified platform appears as a subdirectory. The www source directory is reproduced within each platform's subdirectory, appearing for example in platforms/ios/www or platforms/android/assets/www. Because the CLI constantly copies over files from the source www folder, you should only edit these files and not the ones located under the platforms subdirectories. If you use version control software, you should add this source www folder, along with the merges folder, to your version control system. (More information about the merges folder can be found in the Customize Each Platform section below.)

WARNING: When using the CLI to build your application, you should not edit any files in the /platforms/ directory unless you know what you are doing, or if documentation specifies otherwise. The files in this directory are routinely overwritten when preparing applications for building, or when plugins are reinstalled.

If you wish at this point, you can use an SDK such as Eclipse or Xcode to open the project you created. You will need to open the derivative set of assets from the /platforms/ directory to develop with an SDK. This is because the SDK specific metadata files are stored within the appropriate /platform/ subdirectory. (See the Platform Guides for information on how to develop applications within each IDE.) Use this approach if you simply want to initialize a project using the CLI and then switch to an SDK for native work.

  若是你想整個開發週期都使用跨平臺工做流方法那就繼續讀下去。

Build the App(構建App)

 

  默認的,cordova create 命令腳本生成一個基於web應用的骨架,這個應用的主頁是位於www目錄下的index.html( www/index.html )文件。爲所欲爲的編輯這個應用程序,可是任何初始化都應該被指定爲 裝置就緒 時間句柄的一部分,被默認的 www/js/index.js引用。

  運行下面的命令,迭代構建項目:

  $ cordova build

  上面代碼在項目的 platforms 子目錄下面生成指定平臺的代碼。你能夠經過下面的命令有選擇的限制每次構建的範圍來指定平臺:

  $ cordova build ios

  上面 cordova build 命令是下面的速記,在這個例子中也是有目標的構建單個平臺:   

 $ cordova prepare ios
 $ cordova compile ios

  在這個案例中,一旦你運行 prepare 命令,你可使用蘋果的Xcode SDK做爲修改和編譯指定平臺代碼的一個選擇,Cordova會將它生成在 platforms/ios裏。其餘平臺的SDK也可使用相同的方法。

在模擬器或真實設備上測試這個APP

   移動平臺的SDK和模擬器常常捆綁一個設備圖像,用來在主屏啓動這個app,還能夠看一下他是怎麼和許多平臺特性交互的。運行下面的命令來重建app,在一個指定的平臺模擬器裏看它:
 $ cordova emulate android

  一些移動平臺默認模擬一個特殊的設備,例如iOS系統的iPhone。對於其餘平臺,你可能須要首先和一個模擬器鏈接一個設備。

  注意:模擬器支持Amazon當前不可用的Fire系統

  (查看 平臺指南 的細節) 例如,你可能首先運行 android 命令啓動Android SDk,而後運行一個特殊的設備圖像,根據它的默認行爲來啓動本身:

  

  接着 cordova emulate 命令刷新模擬器圖像顯示最新的應用程序,如今能夠在主屏啓動:

  

 

  另外,你還能夠把手機鏈接到你的電腦而後直接測試這個app

    $ cordova run android

  在運行這個命令以前,你須要創建一個設備來測試,下面的程序在每一個平臺都變化無窮。在Android 和Amazon Fire OS設備上,你不得不提供一個可用的 USB debugging 選項,而且可能提供一個USB驅動在你的開發環境上。在平臺指南查看在每一個平臺所要求的細節 。

 

Add Plugin Features

  當你構建並查看一個新項目時,默認的應用程序看上去好像不能作不少事。你能夠充分利用標準的web技術許多方式來修改app,可是app想要和不一樣層次設備的特性緊密交流,你須要添加提供使用核心cordova API權利的插件。

  插件 是一點附加代碼,向本地組件提供一個接口。你能夠設計你本身的插件接口,例如當設計一個混合app的時候,它和本地組件混合了一個cordova web視圖。 (看嵌入WebViews 和插件開發指南 的詳細說明.) 更常見地,你將添加一個插件來使cordova的基本設備級的特性之一可以使用,在API Reference有更詳細的說明 。包括社區提供的其餘插件的列表,能夠在 plugins.cordova.io找到。你可使用CLI從註冊表取搜索插件。例如,搜索 bar 和 code 生成一個對大小寫不敏感匹配這兩個子字符串的結果。:

    $ cordova plugin search bar code

    com.phonegap.plugins.barcodescanner - Scans Barcodes

  只搜索 bar 生成另外的結果:

    org.apache.cordova.statusbar - Cordova StatusBar Plugin

 

  這個 cordova plugin add 命令須要你指定插件代碼倉庫。請注意,當你是Web項目開發流而且使用CLI時,CLI將會很當心的向每一個平臺合適的地方添加這個插件代碼。(若是你是使用本地項目開發劉,你將不得不爲每一個平臺屢次使用Plugman添加插件(引導連接)

  這是一些你可能使用CLI怎樣爲app添加特性的例子:

  • 基本設備信息 (Device API):

      $ cordova plugin add org.apache.cordova.device
     
  • 網絡鏈接(Connection) 和電池事件(Events):

     
     $ cordova plugin add org.apache.cordova.network-information
     $ cordova plugin add org.apache.cordova.battery-status
     
  • AccelerometerCompass, and Geolocation:

     
      $ cordova plugin add org.apache.cordova.device-motion
      $ cordova plugin add org.apache.cordova.device-orientation
      $ cordova plugin add org.apache.cordova.geolocation
     
  • CameraMedia playback and Capture:

     
      $ cordova plugin add org.apache.cordova.camera
      $ cordova plugin add org.apache.cordova.media-capture
      $ cordova plugin add org.apache.cordova.media
     
  • 設備可訪問的文件和網絡 (File API):

     
      $ cordova plugin add org.apache.cordova.file
      $ cordova plugin add org.apache.cordova.file-transfer
     
  • 通知(Notification) 經過對話框或震動:

     
      $ cordova plugin add org.apache.cordova.dialogs
      $ cordova plugin add org.apache.cordova.vibration
     
  • Contacts:

     
      $ cordova plugin add org.apache.cordova.contacts
     
  • Globalization:

     
      $ cordova plugin add org.apache.cordova.globalization
     
  • 閃屏(Splashscreen):

      $ cordova plugin add org.apache.cordova.splashscreen
     
  • 打開新瀏覽器窗口(InAppBrowser):

      $ cordova plugin add org.apache.cordova.inappbrowser
     
  • 調試控制檯:

      $ cordova plugin add org.apache.cordova.console

     

  使用 plugin ls (或者 plugin list, 或者 plugin 自己)來查看當前已安裝的插件。每一個經過本身的標識符顯示:

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

  刪除插件指的是出如今列表相同的標識符。例如,這是你將怎樣從一個發佈版本移除一個調試控制檯的支持:

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

  你能夠經過每一個命令指定一個以上的參數批刪除或添加插件:

    $ cordova plugin add org.apache.cordova.console org.apache.cordova.device

 

Advanced Plugin Options(高級插件選項)

  當添加一個插件時,幾個選項容許你指定從哪兒獲取這個插件。上面的例子使用了一個總所周知的registry.cordova.io 註冊表,經過 id指定這個插件:

    $ cordova plugin add org.apache.cordova.console

  這個  可能包含插件的版本號,在  符號以後添加。 The  version is an alias for the most recent version. For example:id@latest
    $ cordova plugin add org.apache.cordova.console@latest
    $ cordova plugin add org.apache.cordova.console@0.2.1

If the plugin is not registered at registry.cordova.io but is located in another git repository, you can specify an alternate URL:

    $ cordova plugin add https://github.com/apache/cordova-plugin-console.git

The git example above fetches the plugin from the end of the master branch, but an alternate git-ref such as a tag or branch can be appended after a # character:

    $ cordova plugin add https://github.com/apache/cordova-plugin-console.git#r0.2.0

If the plugin (and its plugin.xml file) is in a subdirectory within the git repo, you can specify it with a : character. Note that the # character is still needed:

    $ cordova plugin add https://github.com/someone/aplugin.git#:/my/sub/dir

You can also combine both the git-ref and the subdirectory:

    $ cordova plugin add https://github.com/someone/aplugin.git#r0.0.1:/my/sub/dir

Alternately, specify a local path to the plugin directory that contains the plugin.xml file:

    $ cordova plugin add ../my_plugin_dir
相關文章
相關標籤/搜索