译:cordova
http://cordova.apache.org/docs/en/3.5.0/guide_cli_index.md.html#The%20Command-Line%20Interface
cordova命令界面(CLI)向你展示了怎樣創建一個app,并且在各種各種的本地移動平臺部署他們。這個工具允許你創建一個新的項目,編譯到不同的平臺上面,然后在真實的設備上或者模擬器里運行。CLI是在概述里面描述的跨平臺工作流的主要工具。然而,你也可以使用CLI來初始化項目代碼,然后切換到各種平臺的SDK和shell工具來繼續開發。
Prerequisites
在運行任何命令行工具之前,你需要安裝目標平臺的SDK(See the?Platform Guidesfor more details.)
為了給任何一個平臺添加一個支持或者重建的項目,你需要在支持這個平臺的SDK的機器上運行命令行。 命令工具支持下列系統:
- 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.
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.
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.
安裝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.
要安裝cordova命令行工具,請按照下面步驟操作:
下載并安裝?Node.js.。按照步驟安裝之后,你可以在你的命令行調用?node?和?npm?。如果安裝成功,你可以選擇使用像?nvm?或?nave?這樣的工具來管理你的 Node.js 安裝配置。
下載并安裝一個?git 客戶端,如果你沒有的話。按照步驟安裝之后,你可以在你的命令行調用??git 命令。即使你不會手動使用?git?命令,在創建一個新項目的時候CLI會在后臺使用它來下載一些資源。
使用?npm?安裝?cordova?模塊。cordova?模塊將會自動被下載.
-
在 OS X 和Linux上:
$ sudo npm install -g cordova On OS X and Linux, prefixing the?npm?command with?sudo?may be necessary to install this development utility in otherwise restricted directories such as?/usr/local/share. If you are using the optional nvm/nave tool or have write access to the install directory, you may be able to omit the?sudo?prefix. There are?more tips?available on using?npm?without?sudo, if you desire to do that. -
在 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?css,?js, 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 firefoxosRun 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 androidRunning 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 codecom.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 ? -
Accelerometer,?Compass, 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 ? -
Camera,?Media?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這個?id?可能包含插件的版本號,在?@?符號之后添加。 The?latest?version is an alias for the most recent version. For example: ? ? $ 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.gitThe 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.0If 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/dirYou can also combine both the git-ref and the subdirectory:
? ? $ cordova plugin add https://github.com/someone/aplugin.git#r0.0.1:/my/sub/dirAlternately, specify a local path to the plugin directory that contains the?plugin.xml?file:
? ? $ cordova plugin add ../my_plugin_dir轉載于:https://www.cnblogs.com/miss-radish/p/4788876.html
總結
- 上一篇: Codeforces Round #33
- 下一篇: [转]Java——Servlet的配置和