Adding plugins (Ionic 2)

In my app I need to use the network information plug-in so to add it I ran the command:

ionic plugin add cordova-plugin-network-information

In my Ionic 1 apps I always used ionic plugin add to add my plug-ins as the plugin is then automatically added to the package.json file (which I assumed was good).

Anyway, after adding the plugin (and without adding any code to actually use it) I built the app and tested it on a device. The app crashed on loading (no errors in the log - just crashed on startup). I removed the plugin and added it again and re-tested. Same thing happened.

Then I tried removing the plugin and adding it back again but this time using:

cordova plugin add cordova-plugin-network-information

This time when I built the app, it worked. What’s the difference between using the ionic plugin add and the cordova plugin add commands? I thought the only difference was the entry in package.json but presumably there are other differences?

1 Like

I’m not sure why your app may have crashed with the ionic plugin add vs cordova, however I wanted to mention that you might want to take a look at using Ionic Native as well: https://github.com/driftyco/ionic-native

A lot of things have been in flux too between the late alpha versions of ionic and the current betas. At times I’ve had to do an update of Ionic CLI, generate a fresh codebase from the blank starter, and then move over my app folder and that’s helped to get things back to working right.

1 Like

Hey how would I go about reproducing this? Ionic plugin add should just be calling cordova plugin add under the hood and I’m unable to reproduce with a simple test.

@tim if you added the plugin like I did and you had no problems then I guess its some sort of configuration issue (version issue) at my end. Using the Cordova command to install certainly stopped the app crashing on load though. And it appears it also helped someone else with another plugin issue here.

@richardshergold from what I see, after adding plugins (using ionic plugin add) then adding the platform, you should see particular java files from plugin you’re adding on folder [app]->platforms->androids->src->...

Previously I got error also after adding plugin but without removing and adding platform. If I just adding plugin after adding platform, I have no java file on androids->src folder (even after I run ionic build android).

Looks good.
Please share a hint - should I install first my cordova plugins before using wrappers from this repo?
Or somehow common plugins are already included?

If I recall correctly, my approach was to add the cordova plugins first via cli: “ionic plugin add” and then I installed ionic-native and just began using it. You could do it the other way around but before it actually works you’ll have to add the needed plugins at some point.