Creating the first hello cordova app using cordova can be achieved by just following 4 simple steps, before which ensure that Apache Cordova is installed and Android Virtual Device (AVD) is available in your system else refer Installing Cordova to install.
- Create a cordova project.
- Add a platform to the project.
- Build the project.
- Test the app in emulator.
Step 1: Create a cordova project
Create a new directory in your local drive where you wish to create the cordova projects (Eg., E:\projects\cordova).
Open the command prompt and navigate to the directory created above and enter the following command,
cordova -d create helloworld
- -d is used to display the verbose in the command prompt.
- create is the command used to create a cordova project
- helloworld is the name of your project.
The image below shows the contents of the cordova project which got created as a result of executing the command above,
- The contents of the www folder is displayed below,
- The index.html by default is designed to display "Apache Cordova". Modify the content of the file if required for testing as of now.
- The config.xml contains important metadata required to generate and distribute the application.
Step 2: Add a platform to the project
The step 1 above just created the project structure and its contents for development. But before compiling the contents, we need to add the target platform to which the application is bundled.
Now cd into helloworld folder and type the command cordova platform add android to add android as a platform,
A new folder "android" gets created inside the platforms folder of your helloworld project. Similarly, you should be able to add other platforms like ios.
If you wish to remove the android platform, you can do this by executing cordova platform remove android
Step 3: Build the project
Execute cordova build from the helloworld directory to build the project for all the platforms added earlier.
If you wish to build the project for a specific platform, then add the platform name to the above command,
cordova build android
Once the build is completed, you should be able to find the following apk files inside platforms/android/bin folder,
Step 4: Test the app in emulator
Run the command cordova emulate android to view the application created in the above steps in an emulator.
Wait for a while after running the command because emulator will take quiet a long time to display.
If you would like to check whats happening, then run cordova -d emulate android
The emulator will be displayed only if the AVD is created properly.
The application will be deployed and displayed in the emulator as shown above. This concludes creating a basic Hello Cordova application using command line interface. There are plenty of articles to come related to the development of applications using cordova. Subscribe to Tech Ariv via Subscribe via Email to get the latest updates.