How to use a more up to date Cordova plugin in Visual Studio 2017

妖精的绣舞 提交于 2019-12-24 00:58:53

问题


I recently added the Camera and File plugins to my Ionic 1.3/ Cordova app, and now the app is intermittently crashing on the Android device I am using.

The crash is not happening when I am actually using the plugins, nonetheless I wanted to see if the problem would go away if I used more up to date plugins.

If I remove the Camera plugin and add it again, I can see this in the output window:

Warning: Unmet project requirements for latest version of cordova-plugin-camera: Warning: cordova-android (5.2.1 in project,

=6.3.0 required) Warning: Fetching highest version of cordova-plugin-camera that this project supports: 2.4.1 (latest is 4.0.2)

Currently in Visual Studio 2017 you can specify a Toolset that uses Cordova 6.3.1 or a Global Cordova version, so I thought I would try using the latest version of Cordova and on a command line entered: npm install -g cordova. I had to restart my computer, but now in my config.xml I can choose "Global Cordova 8.0.0".

Now, in order to use the latest Camera plugin I need to get the cordova-android platform >=6.3.0. But if I use cordova platform add android, it fails because that command needs to be run inside a Cordova-based project.

So I navigate to my existing project folder and run cordova platform add android again. It tries to fetch cordova-android@5.2.1 - so I guess it's now using cordova 6.3.1, because that's the local version. <edit> Wrong! - cordova -v tells me it's 8.0.0, so it must be choosing 5.2.0 for some other reason.</edit>

So what do I need to do now? Do I need to uninstall the local cordova from a command line? Do I delete the existing node_modules, platforms, plugins folders? Do I change the config.xml? (current example entry: - <plugin name="cordova-plugin-camera" spec="~2.4.1" />. If I do all that then decide I need to go back to using Visual Studio's toolset, will I have to keep copies of everything and install manually or can I just go back and select the 6.3.1 toolset again?


回答1:


Changing the Cordova CLI version in Visual Studio 2017 is not as straightforward as it would appear.

Here are the steps I had to follow to change to Cordova 7.1.0 and cordova-android 6.3.0, surely it will allow you to move to Cordova 8.0 and cordova-android 7.1.0 as well.

Follow these steps:

Go to File > New > Project > Blank App (Apache Cordova)

Open config.xml in View Code mode and find this:

<vs:toolsetVersion>6.3.1</vs:toolsetVersion>
<engine name="android" spec="5.2.1" />

Replace with:

<vs:toolsetVersion>8.0.0</vs:toolsetVersion>
<engine name="android" spec="7.1.0" />

Where 8.0.0 is your globally installed Cordova version.

Select Device as target.

Now Build > Build Solution

Save, close and reload the project. When you access config.xml in designer mode you'll see Global Cordova 8.0.0 as the selected toolset.

In order to build you may need to use the external Android SDK Build Tools (API 27 I guess - it was API 26 for CLI 7.1.0) instead of the ones provided by Visual Studio (API 25).

Use the Android SDK Manager to manage versions, no need to get Android Studio for this.

Remember to follow the guidelines from Microsoft when changing the CLI on existing projects. However I strongly recommend creating a new one and then importing your files and adding your plugins to avoid potential problems.




回答2:


After a few try i have found a way:

Follow this steps:

  1. install nodejs 4.8.7 on your machine

  2. in visual studio go under tools>options>"Tools for apache cordova" and deflag chekbox "use a sandboxed version of NodeJS"

  3. Edit taco.json file of your project and set "cordova-cli": "7.1.0"

  4. Open a new console and install cordova on your machine (npm install -g cordova@7.1.0)

For android build:

  1. open prompt, digit android and press return
  2. download API 26 SDK from tool
  3. download gradle 2.x and put bin folder in your system path
  4. install jdk 1.8.x and set java_home system var to it
  5. reboot the system

Hope it help.



来源:https://stackoverflow.com/questions/49319304/how-to-use-a-more-up-to-date-cordova-plugin-in-visual-studio-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!