Plugins are not always added after cordova add platform android and iOs

16,197

Solution 1

I've experienced some similar problems myself. Try reseting your ionic project:

ionic state reset

This removes the platforms/ and plugins/ folder and restores them from the information stored in your package.json. There's a few other useful commands documented on the ionic-cli project.

Solution 2

The solution that I ended up using is to uninstall and reinstall all the plugins after adding the platform. Since I've had trouble with this issue in past Cordova apps, I'm trying to make the builds as consistent as possible, so I'm not committing the platforms directory and deleting it after I build the apk. I've done this with a script:

ionic platform add android

ionic plugin remove org.apache.cordova.device
ionic plugin remove org.apache.cordova.console
ionic plugin remove com.ionic.keyboard

ionic plugin add org.apache.cordova.device
ionic plugin add org.apache.cordova.console
ionic plugin add com.ionic.keyboard

platforms/android/cordova/build --release

rm -rf platforms

This has consistently worked for me, but since I'd rather not have to worry about keeping this current, I have moved these commands into the: after_platform_add/010_install_plugins.js, with the following additions:

packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];

packageJSON.cordovaPlugins.forEach(function(plugin) {
  exec('cordova plugin remove ' + plugin, function(error, stdout, stderr) {
    sys.puts(stdout);
  });
});

packageJSON.cordovaPlugins.forEach(function(plugin) {
  exec('cordova plugin add ' + plugin, function(error, stdout, stderr) {
    sys.puts(stdout);
  });
});

This assumes that something along these lines exists in the package.json in the root JSON object:

"cordovaPlugins": [
  "org.apache.cordova.console",
  "org.apache.cordova.device",
  "com.ionic.keyboard"
]

Which should occur automatically if the after_plugin_add/010_register_plugin.js is working properly.

All that said, I feel like this is kind of hacky and that Ionic should be handling all this properly, so hopefully I can find some time to look into this issue on that side of things and find the root issue of this problem.

Share:
16,197
batanasov
Author by

batanasov

mine about me is currently blank :)

Updated on June 07, 2022

Comments

  • batanasov
    batanasov almost 2 years

    I need to run ionic platform add android/iOs few times to get the desired result. Sometimes plugins are added after first run, but usually I have to wipe out the plugins and platform directories and re-run adding the platform cycle few times until all plugins are added to android folder.

    Did anyone else experienced same problem and if yes - what is the resolution? if any ...

    thanks in advance

  • laughingpine
    laughingpine about 9 years
    Isn't cordova prepare [platform] all that really is needed? This copies over the files from the projects plugin folder to the target platform.
  • jbeck
    jbeck about 9 years
    cordova build is a shortcut for: cordova prepare, cordova compile, so it should do everything you need. Problem is that it's kind of hit or miss as to whether it actually works. I really need something that's truly reproducible (we do continuous deployment to our clients). This method seems to do the trick.
  • batanasov
    batanasov about 9 years
    That didn't solve the issue for me. The problem still persist.
  • Cris
    Cris about 6 years
    Unfortunately, this is no longer supported: ionic state has been removed as of CLI 3.0.