Search code examples
javascriptandroidcordovacordova-cli

What does cordova prepare and then run vs cordova platform add and then run?


I checked the documentation but didn't find a clear explanation about when to use those commands:

  1. cordova platform add android && cordova run android
  2. cordova prepare android && cordova run android

In the documentations the run command does this:

Run project (including prepare && compile)

run is already running prepare. So, is there any case where I need the option 2? To me it looks like it doesn't make sense if I'm using run after that.

Thank you in advance for your valuable time : )


Solution

  • Adding a platform in Cordova does not run the prepare command which will run hook scripts.

    1. Assuming the android platform is not already added, this will generate the directory platforms/android and populate it with a complete standalone Android project. Cordova plugins that have been installed will also be installed into the Android project. cordova run android will run cordova prepare android followed by cordova build android, then proceed to install and run the application. If the android platform is already added Cordova will throw and error.
    2. This command is redundant, and, assuming the android platform has already been added, will run cordova prepare android twice followed by cordova build android, then install and run that application. If the platform is not added Cordova will throw an error.

    Conclusion

    It makes little sense to differentiate between the two command sequences you provided as they seldom need to be executed. You usually only add a platform once, and you can just call cordova run android without needing to call cordova prepare android before it unless you intend to use cordova build android manually (potentially as a signed release) followed by using adb or saving/deploying the generated APK without needing to do a full blown run.