cordova配置安装

结合网上资料整理,分享~~~

一、系统环境配置

准备

首先你需要安装一些依赖包和配置环境。如果你的开发环境是Linux或Mac,需要把下面命令写在~/.bash_profile中,然后执行source ~/.bash_profile。如果是windows,则需要将这些变量添加到”我的电脑”->”环境变量”中。

JDK

下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

设置环境变量:

JAVA_HOME=/path/to/jdk

JAR_HOME=$JAVA_HOME/jre

PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib: :.:$JAVA_HOME/jre/lib

Apache Ant

下载地址:http://ant.apache.org

设置环境变量:

ANT_HOME=/path/to/ant

PATH=$ANT_HOME/bin/:$PATH

Android SDK

下载地址:http://developer.android.com/sdk/installing/index.html?pkg=adt

设置环境变量:

ANDROID_HOME=/path/to/sdk

PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools

Node.js

下载地址:http://nodejs.org/download/

二、Cordova环境配置

Installing the Cordova CLI

The Cordova command-line tool is distributed as an npm package.

To install thecordovacommand-line tool, follow these steps:

1Download and installNode.js. On installation you should be able to invokenodeandnpmon your command line.

2(Optional) Download and install agit client, if you don't already have one. Following installation, you should be able to invokegiton your command line. The CLI uses it to download assets when they are referenced using a url to a git repo.

3Install thecordovamodule usingnpmutility of Node.js. Thecordovamodule will automatically be downloaded by thenpmutility.

•on OS X and Linux:

$sudo npm install -g cordova

On OS X and Linux, prefixing thenpmcommand withsudomay 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 thesudoprefix. There aremore tipsavailable on usingnpmwithoutsudo, if you desire to do that.


Create the App

Go to the directory where you maintain your source code, and create a cordova project:

$cordova create code com.example.code codeDemo

This creates the required directory structure for your cordova app. By default, thecordova createscript generates a skeletal web-based application whose home page is the project'swww/index.htmlfile.


Add Platforms

All subsequent commands need to be run within the project's directory, or any subdirectories:

$cdhello

Add the platforms that you want to target your app. We will add the 'ios' and 'android' platform and ensure they get saved toconfig.xml:

$cordova platform add ios --save

$cordova platform add android --save

To check your current set of platforms:

$cordova platform ls

Running commands to add or remove platforms affects the contents of the project'splatformsdirectory, where each specified platform appears as a subdirectory.

Install pre-requisites for building

To build and run apps, you need to install SDKs for each platform you wish to target. Alternatively, if you are using browser for development you can usebrowserplatform which does not require any platform SDKs.

To check if you satisfy requirements for building the platform:

$ cordova requirements

Requirements check results for android:

Java JDK: installed .

Android SDK: installed

Android target: installed android-19,android-21,android-22,android-23,Google Inc.:Google APIs:19,Google Inc.:Google APIs (x86 System Image):19,Google Inc.:Google APIs:23

Gradle: installed

Requirements check results for ios:

Apple OS X: not installed

Cordova tooling for iOS requires Apple OS X

Error: Some of requirements check failed


Build the App

By default,cordova createscript generates a skeletal web-based application whose start page is the project'swww/index.htmlfile. Any initialization should be specified as part of thedevicereadyevent handler defined inwww/js/index.js.

Run the following command to build the project forallplatforms:

$cordova build

You can optionally limit the scope of each build to specific platforms - 'ios' in this case:

$cordova build ios


Test the App

SDKs for mobile platforms often come bundled with emulators that execute a device image, so that you can launch the app from the home screen and see how it interacts with many platform features. Run a command such as the following to rebuild the app and view it within a specific platform's emulator:

$cordova emulate android


Add Plugins

You can modify the default generated app to take advantage of standard web technologies, but for the app to access device-level features, you need to add plugins.

Apluginexposes a Javascript API for native SDK functionality. Plugins are typically hosted on npm and you can search for them on theplugin search page. Some key APIs are provided by the Apache Cordova open source project and these are referred to asCore Plugin APIs. You can also use the CLI to launch the search page:

$cordova plugin search camera

To add the camera plugin, we will specify the npm package name for the camera plugin:

$ cordova plugin add cordova-plugin-camera

Fetching plugin "cordova-plugin-camera@~2.1.0" via npm

Installing "cordova-plugin-camera" for android

Installing "cordova-plugin-camera" for ios

Plugins can also be added using a directory or a git repo.

NOTE: The CLI adds plugin code as appropriate for each platform. If you want to develop with lower-level shell tools or platform SDKs as discussed in theOverview, you need to run the Plugman utility to add plugins separately for each platform. (For more information, seeUsing Plugman to Manage Plugins.)

Useplugin ls(orplugin list, orpluginby itself) to view currently installed plugins. Each displays by its identifier:

$ cordova plugin ls

cordova-plugin-camera 2.1.0 "Camera"

cordova-plugin-whitelist 1.2.1 "Whitelist"


Updating Cordova and Your Project

After installing thecordovautility, you can always update it to the latest version by running the following command:

$sudo npm update -g cordova

Use this syntax to install a specific version:

$sudo npm install -g cordova@3.1.0-0.2.0

Runcordova -vto see which version is currently running. To find the latest released cordova version, you can run:

$npm info cordova version

To update platform that you're targeting:

$cordova platform update android --save

$cordova platform update ios --save

...etc.


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,556评论 2 45
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,541评论 5 6
  • 一直活在自己小世界的人其实是蛮可悲的。 小时候总会觉得自己是那么独特,上天一定会很眷顾自己,好像什么东西自...
    小米虫儿阅读 294评论 0 0
  • 有个人很经常问我一些问题,让我烦躁又头疼。 比如昨天,问我奇葩说的题目:闺蜜约你去撕小三,你去还是不去? 我说:撕...
    陈炜杭阅读 353评论 0 0
  • 换人不换指标,有助于团队平衡健康发展。 比如说这C员工,到年中离职了,按照以前的处理方式。C的客户就会被AB...
    耶林世界阅读 328评论 0 0