Tuesday, March 24, 2015

Trying Cordova for Android on Windows without Cordova CLI, using the choco tool

The Cordova CLI tool is great for supporting multiple target platforms but not always ideal for project targeting a limited number (1-2) platforms. The alternative is to download a platform-specific version of Cordova, and then use the plugman tool to install any desired plugins.

NOTE (update): This article assumes use of choco by chocolatey.org. It is no problem to work the plugman tool with without using choco, which I may describe sometime in a future post.

Assumptions

This tutorial assumes the following packages will be installed:
  • choco by chocolatey.org - Windows pacage management tool similar to homebrew (see this post)
  • Android Studio, installed by choco as described in this post) (along with a recent version of the Android SDK and Java SDK, which are automatically installed as prerequisites)
  • node/npm, installed by choco as described below
  • gradle, also installed by choco as described below

NOTE: that it would be possible to try Cordova on Android without using the Android Studio, if you install Android SDK and Java SDK yourself.

Install an Editor

It is highly recommended to install a good developer's editor. For example:
  • (g)vim: choco install vim -y as Administrator: installs vim and gvim in your path (you may have to refresh your path or open a new shell)
  • notepad++: choco install notepadplusplus -y as Administratorsdoes not seem put notepad++ in your path (as of this writing)!
Gradle

Use choco to install gradle as Administrator: choco install gradle -y

Node

If node.js is already installed, it is recommended to update it (or uninstall and reinstall) using choco as described:

Use choco to install node.js (and the bundled npm package manager) as Administrator: choco install nodejs.install -y

Now you should be able to open a Windows command prompt or PowerShell and run node -v and npm -v.

Cordova version for Android

Download the a recent release of Cordova for Android from: https://www.apache.org/dist/cordova/platforms/ (see https://cordova.apache.org/#download)

Unpack it near your desired project workarea.

Create your test project using a command like this:
.\cordova-android-3.7.1\package\bin\create.bat MyTest com.test.my MyTest

Prepare project for import

cd into your new project directory and build (debug mode):
.\cordova\build --gradle

NOTE: the first time you run this command, it will download a bunch of stuff for Gradle.

Import and run project in Android Studio

Open Android Studio, import your new project as a Gradle project, and press the Run button to run the project (in an emulator). If there is no AVD (Android Virtual Device) setup, Android Studio will let you create one. If there is no system image for an emulator, Android Studio will give you a chance to download one.

Test with SQLite plugin

If you don't have plugman installed, you can download plugman from https://www.apache.org/dist/cordova/tools/ or (recommended) simply (NOT as Adminstrator): npm install -g plugman

Using npm to install plugman includes cordova-lib and perhaps cordova-js but NOT the cordova CLI package.

NOTE: the following assumes you have git installed in your path. Unfortunately choco install git -y did not do the trick for me.

I installed the Windows GitHub app and ran the following command in the GitHub shell (from your project directory): plugman install --platform android --project . --plugin https://github.com/brodysoft/Cordova-SQLitePlugin

ALTERNATIVE: you can download an archive of the plugin, extract it, and use the path to the extracted plugin in the --plugin option above.

To run the unit tests: copy the test files from brodysoft / Cordova-SQLitePlugin / test-www / www into the assets/www subdirectory.

Builiding/Running from the command line

Building and running your Cordova Android app from the command line, without the Cordova CLI, will take a little more skill due to the deficiency in choco's ant installation module.

Apache ant can be installed using choco in an Administrator shell: choco install ant -y

The ant installation will show you where the ant tool is installed, which must be in your path, but you have to add the bin subdirectory. Open the systems environment or path variables dialog. Where it is depends on which version of Windows is installed, which is well documented on the internet. Please read up on the format of Windows path variables if you do not understand it. Add the path where the ant tool was installed to your own path variable (should not be the system path variable), double-check it, and confirm. Now open a new shell window and verify that ant is installed: ant -version

Try building your project with ant in debug mode: .\cordova\build.bat --ant --debug (--debug is by default; --ant is currently by default as well)

To see the logcat output (another shell window is recommended): .\cordova\log.bat

To install and run on emulator: .\cordova\run.bat --debug

Resources