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

My first experience with Choco by chocolatey.org

I was hoping to find a good package manager similar to Homebrew for Windows and recently discovered chocolatey.org. Yesterday I followed the instructions on the homepage to install the choco tool (version 0.9.8) in PowerShell (actually the one provided by the GitHub app). Then I started installing the Android Studio using the following command: choco install androidstudio (it told me I should use the -y option for auto-confirm and it will be required in the futureshould have been included in the chocolatey.org examples).

It installed the Android SDK and Java 7.1 SDK dependencies but then I stopped the download of Android Studio. I verified that both the Android SDK and Java SDK were installed ok. When I tried choco install androidstudio again (forgot the -y option), choco complained that it needed elevated (Windows Administrator) mode and that Android Studio was already installed (with a hint that I can use the -force option).

I searched for some hints how to run PowerShell in elevated mode. Unfortunately, a lot of hints do not apply to Windows 8 since they depend on the old file (Windows 7) explorer and start menu. I found the hint for Windows 7/8+ here on serverfault.comStart-Process PowerShell -Verb RunAs

My third attempt to install Android Studio: choco install androidstudio -y -force finally worked.

In conclusion, I am very happy with this tool. Here are some issues I would like to see fixed soon:
  • chocolatey/choco#198 (issue with aborted install)
  • chocolatey.org homepage points to the wrong repo, should point to chocolatey / choco instead
  • make it more explicit on the homepage and for each package that choco install has to be run from an elevated shell, and add a hint how to open an elevated shell on Windows 8 - see this answer on serverfault.comStart-Process PowerShell -Verb RunAs
  • add -y option to the examples given on the homepage and given per package
Some quick links:

FIRST UPDATE: here are some more impressions:
  • I discovered that if you use choco to install node (choco install nodejs.install -y), the node tools such as node and npm are accessible from my PowerShell prompt
  • However, when I used choco to install notepad++, it does not put notepad++ in my path.
  • Also, choco install git -y does not put git in the command path, as needed by the Cordova CLI tool and Cordova plugman tool.
  • choco install ant -y also does not put ant in the command path.