How to install and run Cordova 3.1.0 / 3.2.0 (Phonegap) on Windows Phone 8 simulator (Windows 8 64 bit)



You might want to try to run Cordova with Windows Phone. The first thing that came up when I started with this project was, that the Cordova (PhoneGap) documentation is really lack of information. So I wrote this article to make it easier for you to make a successful and even quicker installation.



Note: This installation guide can be used for both, physical machine and Virtual Machine. There are a lot of Virtual Machine servers available on the market. I decided to focus on VMWare Fusion only, but it is also possible to use software like Parallels.

Before you start

It is very important that you have a 64 bit version of Windows 8 installed, otherwise it will not work.

The Windows Phone simulator relies on Hyper-V, which only comes with Windows 8 64 bit.

In case you did not installed, a evaluation ISO (Windows 8 64 bit) can downloaded at the evaluation center.

Configure the Window 8 installation for WP 8 simulator

The first thing you need to do is to prepare the Windows 8 installation for Windows Phone 8 simulator.

Choose your target system (Virtual Machine or Physical System):

Windows 8 installation on a VM

To make the required Virtual Machine settings, use the VMWare Fusion settings below:




Important:
  • Hyper-V requires at least two cores. Of course, for a better performance you can also set more.
  • You should use 2048mb RAM at the minimum, because not only Windows will consume RAM, also the simulator will consume another 256-512mb of RAM.
You also have to modify the "*.vmx" file. Add the lines below to the end of the vmx file:
  • hypervisor.cpuid.v0 = "FALSE"
  • mce.enable = "TRUE"
  • vhv.enable = "TRUE"
Make sure that you have no duplicate keys in this file.

Windows 8 installation on a physical machine

To be able to run the Windows Phone simulator on a physical Windows 8 machine, you have to enable hardware-assisted virtualisation in the BIOS. For more information, take a look at this article:

Install Microsoft Visual Studio

After the initial Windows 8 configuration you are ready to install Microsoft Visual Studio. Here you have to options:
  1. Using the Visual Studio Express 2012 for Windows Phone edition. This version is free of charge to you, but not recommended by PhoneGap (no templates can be used in the express edition). But since you are able to create PhoneGap projects via CLI, maybe this version is what you are looking for. Go to the Visual Studio Express products.
  2. Installing Visual Studio 2013 Pro / Premium / Ultimate (If you need templates)
In all cases the additional required Windows Phone 8 SDK is already included in the installation package.

Don't forget to enable the WP 8 SDK checkbox while you are installing Visual Studio.

Tip: If you are using VS 2013, Microsoft asks you to authenticate the product with a MS Account. But there is also a way to use the product key. Take a look at this article.

When the installation process of Visual Studio is finished, your computer will be restarted by the installation routine. After the restart, login again, but wait some seconds, the installation process will go on. Wait until you see the "Installation successful" message.

Add the .NET framework directory to the PATH system environment variable

Later on you need the "msbuild.exe" executable your PATH variable. In my case I have to add the following path:
C:\Windows\Microsoft.NET\Framework\v4.0.30319
The path may differ (different version of .NET), so make sure that it matches.

Test the WP 8 simulator

As soon as Visual Studio is completely installed you are able to test the simulator. Open Visual Studio and click on "File" > "New Project".

Go to your preferred language section and select a simple Windows Phone project.

If the project is created, just start the project. The simulator should come up. If the simulator open successfully, you can close Visual Studio. Otherwise follow the further instruction which VS will show to you.

Install Node.js

Those of you who are not familiar with Node.js, Node.js is a platform that is used to build scalable network applications. For more information click here. In this case we use Node.js to install Cordova (PhoneGap).

Just go to http://nodejs.org and install Node.js. The installation process is pretty clear (no custom settings required).
After you installed Node.js, RESTART your computer or Virtual Machine.

Install Cordova

To install Cordova, open "node.js command prompt" and type “npm install -g cordova”.

Fix broken Cordova 3.1.0 version

At the moment Cordova is really a mess. So if you installed the Cordova version above, you have two options: Installing a prior version of Cordova (3.0.x) or fixing the current one.

If you want to fix your current installation, you have to replace the two files in the Cordova installation directory:

~\AppData\Roaming\npm\node_modules\cordova\src\metadata\wp8_parser.js

with

https://github.com/sgrebnov/cordova-cli/blob/181aa1b7e8f3c8b2f7c4db12e1f79d64c26e8b42/src/metadata/wp8_parser.js

and

~\AppData\Roaming\npm\node_modules\cordova\src\platform.js

with

https://github.com/sgrebnov/cordova-cli/blob/181aa1b7e8f3c8b2f7c4db12e1f79d64c26e8b42/src/platform.js

Otherwise you'll receive the following error message when you are creating a Cordova project:

"Error: An error occured during creation of wp8 sub-project. The system cannot find the path specified."

Fix broken Cordova 3.2.0 version

Maybe this error message depends on your type of .NET installation, but with Cordova 3.2.0 I cannot add a WP8 project anymore. The following message is displayed:
The command `msbuild` failed. Make sure you have the latest Windows Phone SDKs installed, AND have the latest .NET framework added to your path (i.e C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319).

To fix this, open "c:\Users\%user%\.cordova\lib\wp\cordova\3.2.0\wp8\bin\check_reqs.js":
Replace:
var msversion = output.match(/Microsoft\s\(R\)\s+Build\sEngine\s[a-z]+\s4\.0\.30319/i);

with:
var msversion = output.match(/\.NET\sFramework\,\sversion\s4\.0/i);
or simply set "var msversion = true;" if you have the right version of "msbuild.exe" in your path variable.

Create the final Cordova project

At this point you are able to build the final Cordova project. As mentioned earlier, I use the CLI of Cordova to create the project, not the Visual Studio templates.
  1. Open the command line and type: "cordova create hello com.example.hello HelloWorld". The first argument specifies a hello directory to be generated for your project. The other two arguments are optional: the com.example.hello argument provides your project with a reverse domain-style identifier, and the HelloWorld provides the application's display text. You can edit both of these values later in the config.xml file.
  2. Open the "hello" folder ("cd hello") which you created above.
  3. Add a WP 8 platform to the current Cordova project. Type "cordova platform add wp8"
  4. Open the Windows Explorer and go to hello/platforms/wp8 and double click on the solution file "*.sln". Microsoft Visual Studio will open with your selected project. Just click the "Run Button" to show up the simulator.

It is a bit tricky to deploy a HTML5 application on a Windows Phone, but I hope this article helps...

2 comments:

  1. I have tried 10-15 other tutorials and this is the first one that really did the job. Thank you.

    ReplyDelete