从项目中删除 cordova 插件

不知怎么的,我的应用程序中安装了很多 Cordova 插件,因此它需要访问几乎所有内容——从我的联系人到当前位置(尽管这个应用程序并不需要这个)。

这个应用程序是通过 jenkins 构建的,据我所知,一个解决方案是用一个命令删除每个插件,所以它会像:

cordova plugin rm org.apache.cordova.battery-status
cordova plugin rm org.apache.cordova.camera
cordova plugin rm org.apache.cordova.contacts
cordova plugin rm org.apache.cordova.geolocation
cordova plugin rm org.apache.cordova.media
cordova plugin rm org.apache.cordova.media-capture
cordova plugin rm org.apache.cordova.splashscreen
cordova plugin rm org.apache.cordova.vibration

但是有时候它会显示一些错误,对于 jenkins,任何错误最终都会导致构建失败,那么是否存在删除所有插件的命令呢?(在安装基础插件,需要任何应用程序的工作是通过科尔多瓦自动添加,所以我一直在寻找一些 cordova plugin rm -all,但无法找到它)

229510 次浏览

As far as I remember from Cordova, you should have an xml file in "res" folder containing the list of plugins used in your project. You probably need to remove those unused plugins from list. And also you should remove related files.

This is the commandline for removing plugins in Cordova

cordova plugin remove <pluginid>

For example I ran cordova plugin and got a list of plugins then I used the id for the plugin to uninstall

cordova plugin remove com.monday.contact-chooser

You can get help in the commandline by typing

cordova help <command>

First, you should list your plugins:

cordova plugin list

With this result, you can simply do:

cordova plugin remove <PLUGIN_NAME>

For example:

cordova plugin remove org.apache.cordova.media

I do it with this python one-liner:

python -c "import subprocess as sp;[sp.call('cordova plugin rm ' + p.split()[0], shell=True) for p in sp.check_output('cordova plugin', shell=True).split('\n') if p]"

Obviously it doesn't handle any sort of error conditions, but it gets the job done.

You can do it with bash as well (after switching to your Cordova project directory):

for i in `cordova plugin ls | grep '^[^ ]*' -o`; do cordova plugin rm $i; done

From the terminal (osx) I usually use

cordova plugin -l | xargs cordova plugins rm

Pipe, pipe everything!

To expand a bit: this command will loop through the results of cordova plugin -l and feed it to cordova plugins rm.

xargs is one of those commands that you wonder why you didn't know about before. See this tut.

You could use: cordova plugins list | awk '{print $1}' | xargs cordova plugins rm

and use cordova plugins list to verify if plugins are all removed.

Scripts based on processing the list of installed plugins may not work as there are dependencies between installed plugins (e,g, cordova-plugin-file and cordova-plugin-file-transfer).

In the example, the script will find the file plugin first, then it will try to remove it and we'll get an error as file-transfer requires it. Therefore, we shall have

v2.0.0 of cordova-check-plugins enables you to remove all plugins in a project:

$ npm install -g cordova-check-plugins
$ cordova-check-plugins --remove-all

It will attempt to use the Cordova CLI to remove each plugin, but if this fails it will force removal of the plugin from platforms/ and plugins/.

If you also want to remove from config.xml, use:

$ cordova-check-plugins --remove-all --save

Disclaimer: I am the author of cordova-check-plugins

When running the command: cordova plugin remove <PLUGIN NAME>, ensure that you do not add the version number to the plugin name. Just plain plugin name, for example:

cordova plugin remove cordova.plugin_name

and not:

cordova plugin remove cordova.plugin_name 0.01

or

cordova plugin remove "cordova.plugin_name 0.01"

In case there is a privilege issue, run with sudo if you are on a *nix system, for example:

sudo cordova plugin remove cordova.plugin_name

Then you may add --save to remove it from the config.xml file. For example:

cordova plugin remove cordova.plugin_name --save
  • access the folder
  • list the plugins (cordova plugin list)
  • ionic cordova plugin remove "pluginName"

Should be fine!

If the above solution didn't work and you got any unhandled promise rejection then try to follow steps :

  1. Clean the Cordova project

    cordova clean

    1. Remove platform

cordova platform remove android/ios

  1. Then remove plugin

cordova plugin remove

  1. add platforms and run the project It worked for me.
cordova platform rm android


cordova plugin rm cordova-plugin-firebase


cordova platform add android

This is somewhat related and might help others, I had a corrupt version of some of my plugins so I was able to just delete the entire contents of the plugins folder. Note, all references are still in the package.json and config.xml files to the plugins. So then when I removed and added the Android platform, it re-installed the uncorrupted versions of the plugins and fixed my problem.

I am probably late, however there is a way...

Firstly, unfortunately there is no cordova plugin rm all, but we can work around it.

Just remove plugins dir and then re add all the plugins u need. But be ware this is not 'official' way to do it, but it works on both Android and iOS. I have just tested it, because I needed similar scripts for platform specific.

If u find it helpful consider pinning this response if possible.

Regards ;)