简体   繁体   English

如何添加cordova插件和平台

[英]How to add cordova plugins and platforms

I am using this cordova tutorial to try and set up a cordova/phonegap app. 我正在使用此cordova教程尝试设置cordova / phonegap应用程序。 I am developing on Windows, I have node.js installed and I am working inside the Node.js command prompt. 我在Windows上开发,我安装了node.js,我在Node.js命令提示符下工作。 I downloaded and installed: 我下载并安装了:
Andorid SDK from here and have added it to PATH , 来自这里的Andorid SDK 并将它添加到PATH
downloaded ant and installed it , 下载了ant安装了它
Java JDK is also downloaded and installed. 下载并安装了Java JDK。

My PATH contains these: 我的PATH包含以下内容:
%ANT_HOME%\\bin;%JAVA_HOME%\\bin;C:\\Program Files (x86)\\Android\\android-sdk\\tools

Typing android into cmd opens up the Android SDK manager. 在cmd中键入android打开Android SDK管理器。
Typing java shows java help on commands. 键入java显示java命令帮助。
Typing ant -version shows the ant version (1.9.6). 键入ant -version显示了ant版本(1.9.6)。
Typing cordova shows cordova help commands. 键入cordova显示cordova帮助命令。

I successfully installed cordova with npm install -g cordova , and created my workshop directory. 我用npm install -g cordova成功安装了npm install -g cordova ,并创建了我的研讨会目录。 Then I tried adding the platforms and plugins suggested by the tutorial. 然后我尝试添加本教程建议的平台和插件。 Here is the command prompt output for just the android platform and the device plugin: 这是android平台和设备插件的命令提示输出:

C:\Users\Roman\All\Work\CriticalID\again>cordova platforms add android
npm http GET https://registry.npmjs.org/cordova-android/-/cordova-android-4.0.2.tgz
npm http 200 https://registry.npmjs.org/cordova-android/-/cordova-android-4.0.2.tgz

C:\Users\Roman\All\Work\CriticalID\workshop>cordova plugin add cordova-plugin-device
Fetching plugin "cordova-plugin-device" via npm
npm http GET https://registry.npmjs.org/cordova-plugin-device
npm http 304 https://registry.npmjs.org/cordova-plugin-device
npm http GET https://registry.npmjs.org/cordova-plugin-device/-/cordova-plugin-device-1.0.1.tgz
npm http 200 https://registry.npmjs.org/cordova-plugin-device/-/cordova-plugin-device-1.0.1.tgz

Checking if the platforms and plugins are installed: 检查平台和插件是否已安装:

C:\Users\Roman\All\Work\CriticalID\workshop>cordova platforms ls
Installed platforms:
Available platforms: amazon-fireos, android, blackberry10, browser, firefoxos, windows, windows8, wp8

C:\Users\Roman\All\Work\CriticalID\workshop>cordova plugin ls
No plugins added. Use `cordova plugin add <plugin>`.

C:\Users\Roman\All\Work\CriticalID\workshop>cordova build
No platforms added to this project. Please use `cordova platform add <platform>`.

None of the platforms or plugins I installed are showing as installed. 我安装的平台或插件都没有显示为已安装。 Both the workshop\\plugins and workshop\\platforms folders are empty too. workshop\\pluginsworkshop\\platforms文件夹也都是空的。

The tutorial apps work in my browser and phone (put there by the PhoneGap desktop and phone apps), but when i try a more complicated tutorial, like part 12 from this PhoneGap tutorial where the camera API is required, then camera is unsupported in the browser (obviously) and on my phone ( Error: Camera API is not supported ). 教程应用程序可以在我的浏览器和手机中使用(通过PhoneGap桌面和手机应用程序放置),但是当我尝试更复杂的教程时,例如需要相机API的PhoneGap教程中的第12部分,则不支持相机浏览器(显然)和我的手机( Error: Camera API is not supported )。

EDIT 编辑

I have solved the problem by adding the Android platform and plugins using git; 我通过使用git添加Android平台和插件解决了这个问题; The solution to that is below. 解决方案如下。 However, if I have missed something, please let me know. 但是,如果我错过了什么,请告诉我。
It seems that config.xml is supposed to handle some functionality, but I've not been able to figure it out. 似乎config.xml应该处理一些功能,但我无法弄明白。
Everything for Cordova 5.1.1 Cordova 5.1.1的一切

I believe the Documentation is old, which has been giving me problems. 我相信文档很旧,这给我带来了麻烦。 Additionally, some config.xml and file structure is different when creating the app from scratch with Cordova, or via PhoneGap or PhoneGap build. 此外,使用Cordova或通过PhoneGap或PhoneGap构建从头开始创建应用程序时,某些config.xml和文件结构会有所不同。

First and foremost, these lines of code do not seem to work when building the app with Cordova: 首先,在使用Cordova构建应用程序时,这些代码行似乎不起作用:

cordova platforms add android
cordova plugin add org.apache.cordova.device
cordova plugin add cordova-plugin-device

All platforms and plugins need to be added via Git . 所有平台和插件都需要通过Git添加。 Android platform here , and a list of plugins here . Android平台在这里 ,以及这里的插件列表

So, for example to get the default Cordova app, (for instillation, see question above) copy and paste the following into CMD: 因此,例如,要获取默认的Cordova应用程序(用于灌输,请参阅上面的问题),将以下内容复制并粘贴到CMD中:

cd into your working directory
cordova create workshop com.name.workshop Workshop
cd workshop
cordova platform add https://github.com/apache/cordova-android.git
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-whitelist.git
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

Note that the platforms have to be added before the plugins. 请注意,必须在插件之前添加平台。 I added whitelist because config.xml wanted to, and I also added device and console because the tutorial told me to. 我添加了whitelist因为config.xml想要,我也添加了deviceconsole因为教程告诉我。

Now, if you type cordova platform ls and cordova plugin ls , then you should have a non empty list returned to you. 现在,如果您键入cordova platform lscordova plugin ls ,那么您应该返回一个非空列表。

For completeness sake, if you would now like to use your camera: 1) Copy the following into index.html : 为了完整起见,如果您现在想要使用相机:1)将以下内容复制到index.html

<img id='image' src='' style="position:absolute;margin:5px;left:0px;top:0px;"/>
<button id="test_camera">Camera Test</button>
<script type="text/javascript" src="js/test.js"></script>

2) Create a new script test.js , and paste the following into it. 2)创建一个新脚本test.js ,并将以下内容粘贴到其中。 I don't like the convoluted way the tutorials create this function, this is way more straight forward: 我不喜欢教程创建此函数的复杂方式,这是更直接的方式:

var changePicture = function() {
  if (!navigator.camera) {
      alert("Camera API not supported", "Error");
      return;
  }
  var options =   {   quality: 50,
                      destinationType: Camera.DestinationType.DATA_URL,
                      sourceType: 1,      // 0:Photo Library, 1=Camera, 2=Saved Album
                      encodingType: 0     // 0=JPG 1=PNG
                  };

  navigator.camera.getPicture(
      function(imgData) {
          $('#image').attr('src', "data:image/jpeg;base64," + imgData);
      },
      function() {
          alert('Error taking picture', 'Error');
      },
      options);

  return false;
};

$("#test_camera").on('click', function() {
    changePicture()
})

You can leave the initialisation code in index.js alone. 您可以单独在index.js保留初始化代码。

Now use the PhoneGap desktop app and the PhoneGap phone app to test your application on your phone. 现在使用PhoneGap桌面应用程序和PhoneGap手机应用程序在手机上测试您的应用程序。 I'ts pretty easy. 我很容易。 I'm using windows and Android for this, I don't know about any other systems. 我正在使用Windows和Android,我不知道任何其他系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM