Hello Cordova – Develop Android Apps on Ubuntu Linux

Write Android Apps in HTML5 and JavaScript. Cordova allows access to hardware (GPS, accelerometer…) and packaging for Google Play.
You’ll learn how to install Android SDK tools and Cordova on Ubuntu Linux 14.04 LTS. Cordova and Android SDK are installed as a normal user, official Ubuntu packages for requirements are installed with ‘sudo apt-get install’.
Install takes over 7 gigabytes, so you can’t install this environment on a default live USB. Even though there are many steps, installation only takes 15 minutes from fresh Ubuntu install to running “hello world” in an emulator.

Android SDK installer

Install a modern version of Java developer kit

$ sudo apt-get update
$ sudo apt-get -y install openjdk-7-jdk

Download Linux SDK tools from developer.android.com

$ cd
$ wget wget http://dl.google.com/android/android-sdk_r24.3.4-linux.tgz
$ tar -xf android-sdk_r24.3.4-linux.tgz
$ rm android-sdk_r24.3.4-linux.tgz

Now ‘android’ command should run if you specify the path (show help text, not complain about Java missing)

$HOME/android-sdk-linux/tools/android --help
Usage:
android [global options] action [action options]
...

We have only installed Android SDK installer now. Later when Cordova tells us which pages to install, we will Android SDK installer it to install the rest of Android SDK.

NodeJS and npm, without root

Download NodeJS Linux 64 bit binaries from nodejs.org.

$ cd
$ wget https://nodejs.org/dist/v0.12.7/node-v0.12.7-linux-x64.tar.gz
$ tar xf node-v0.12.7-linux-x64.tar.gz
$ rm node-v0.12.7-linux-x64.tar.gz
$ mv node-v0.12.7-linux-x64/ nodejs
NodeJS package manager npm should now run when you specify the path
$ ./nodejs/bin/npm --version
2.11.3

Add NodeJS and Android SDK Tools to your $PATH

$ cd
$ nano .profile

Add this line to the beginning of your .profile:

PATH="$HOME/nodejs/bin/:$HOME/android-sdk-linux/tools/:$HOME/android-sdk-linux/platform-tools/:$PATH"

Exit nano and save the file (ctrl-X y enter).
Take settings in to use immediately

$ source .profile

Now you can use NodeJS and Android commands everywhere, as they are in your $PATH. You should get command specific output instead of “command not found”.

$ npm --version
2.11.3
$ android --help
Usage: ... android [global options] action [action options] ...

Cordova

Git is used by npm for installing Cordova

$ sudo apt-get -y install git

Because we installed NodeJS and npm as non-root, npm will automatically install our packages as non-root. The npm command takes a while.

$ npm install -g cordova

NodeJS binaries are already in our $PATH, so we can start using Cordova

$ cordova --version
5.2.0

Find Out What Parts of Android SDK Are Needed

Let’s try to create a project with Cordova, so it can show us which parts of Android SDK we need. Commands are explained in Cordova CLI documentation.

$ cordova create hellotero com.example.hellotero HelloTero
Creating a new cordova project.
$ cd hellotero

So far so good: we have a new Cordova project, and now we only need to make it build Android apks.

$ cordova platform add android
Adding android project...
Creating Cordova project for the Android platform:
 Path: platforms/android
 Package: com.example.hellotero
 Name: HelloTero
 Activity: MainActivity
 Android target: android-22
Copying template files...
Android project created with cordova-android@4.1.1
Discovered plugin "cordova-plugin-whitelist" in config.xml. Installing to the project
Fetching plugin "cordova-plugin-whitelist@1" via npm
Installing "cordova-plugin-whitelist" for android
$ cordova platform ls
Installed platforms: android 4.1.1
$ cordova build
... [Error: Please install Android target: "android-22".
Hint: Open the SDK manager by running:  ... android
You will require:
1. "SDK Platform" for android-22
2. "Android SDK Platform-tools (latest)
3. "Android SDK Build-tools" (latest)]
...

So that’s what we need to install. Because Cordova requires a specific version of Android SDK, it’s easiest to install it according to the error message of ‘cordova build’.

Install the Needed Parts of Android SDK

Install some support for running 32-bit apps on your amd64 computer. Yes, your computer is most likely 64-bit (amd64).

$ sudo apt-get -y install libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1
$ android

A window opens. Uncheck the defaults, so that we don’t install unnecessary files. Enable the packages listed by ‘cordova build’ error message

  • Tools: Android SDK Platform-tools
  • Tools: Android SDK Build-tools
  • Android 5.1.1 (API 22): SDK Platform

Install some other required packages. At least one system image is needed to run the emulator.

  • Android 5.1.1 (API 22): ARM EABI v7a System Image

Click “Install 4 packages…”. A confirmation window pops up, “Accept License”, “Install”. Installation takes a while.
After the installation completes, you can get a list of installed packages by unchecking “Updates/New”:

Build Cordova Project

Go to your project dir and build

$ cd $HOME/hellotero
$ cordova build
BUILD SUCCESSFUL
Total time: 12.174 secs
Built the following apk(s):
/home/xubuntu/hellotero/platforms/android/build/outputs/apk/android-debug.apk

As it says, you’ve now successfully built an APK for Android.

Run in an Emulator

$ android avd

“Create…”

  • AVD Name: galane
  • Device: Galaxy Nexus…
  • Target: Android 5.1.1 – API Level 22
  • CPU/ABI: ARM (armeabi-v7a)
  • Skin: HVGA
  • Emulation Options: Use Host GPU

A dialog lists its features: “Result of creating AVD ‘galane'”. “OK”.
Start the virtual device (emulator): select it from the list and click “Start…” It will take a long time to start. Don’t close it once you get it running. You can just send a new version of your program to the running instance.

Hello World – Mission Completed

Do you see your “hello Cordova” in the Anrdoid emulator? Well done, you have now installed Cordova and Android SDK.

Troubleshooting

No problems? See your “hello world” in the emulator? You won already, you can skip the troubleshooting section.
Problem: Settings in .profile are ignored. Solution: ‘source $HOME/.profile’ will use the settings immediately. You can also log off and back. You shouldn’t have a .bash_profile, as that will override .profile.
Problem: “Unable to run ‘adb’: Cannot run program”: Install 32-bit support as described above.
Problem: “libz.so.1: cannot open shared object file: No such file or directory”. Install support for 32-bit, especially ‘sudo apt-get -y install lib32z1’.
Probelm: ‘cordova emulate’ says “ERROR : No emulator images (avds) found.”: ‘android avd’, create a new emulator. Explained in more detail above.
Problem: I’m lazy, could you just give me one long apt-get line for all the prequisites? (You still have to install Android SDK and Cordova manually.

sudo apt-get update && sudo apt-get -y install screen shutter gedit curl chromium-browser openjdk-7-jdk git libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1

Problem: “adb: command not found” or “The program ‘adb’ is currently not installed. You can install it by typing: sudo apt-get install android-tools-adb” Solution: adb is now in platform-tools/ folder, not in tools/ anymore. Add it to your $PATH as shown above.
Problem: why can’t you make ‘sudo apt-get install cordova-dev’ install this whole thing? Android license agreement doesn’t allow this.
Problem: how do I debug? ‘adb logcat’.
Edit: more screenshots, a single apt-get line and troubleshooting.

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , | 6 Comments

6 Responses to Hello Cordova – Develop Android Apps on Ubuntu Linux

  1. Tero Karvinen says:

    Alternative settings for KVM & Android Studio
    $ head .profile
    # node:
    PATH=”$HOME/nodejs/bin/:$PATH”
    # android sdk cli
    PATH=”$HOME/android-sdk-linux/tools/:$HOME/android-sdk-linux/platform-tools/:$PATH”
    # android studio
    PATH=”$HOME/android-studio/bin/:/home/tee/Android/Sdk/platform-tools/:/home/tee/Android/Sdk/tools/:$PATH”
    $ apt-get install qemu-kvm libvirt-bin bridge-utils virt-manager
    $ adduser tee libvirtd
    $ virsh -c qemu:///system list
    $ grep -P ‘(svm|vmx)’ /proc/cpuinfo

  2. Q: “This user doesn’t have permissions to use KVM (/dev/kvm).” when attempting to launch emulator. A: $ sudo adduser tero kvm

  3. For Windows, it’s better to upgrade to Linux, but in the meanwhile try:
    set PATH=%PATH%;C:\Users\%USERNAME%\AppData\Roaming\npm\node_modules\cordova\bin\;C:\Users\%USERNAME%\AppData\Local\Android\Sdk\tools;C:\Users\%USERNAME%\AppData\Local\Android\Sdk\platform-tools
    For debugging Cordova
    $ adb logcat chromium:D SystemWebViewClient:D *:S

  4. Install Cordova on a Windows (better upgrade to Linux) machine that already has Android Studio and BlueStacks.
    set PATH=%PATH%;C:\Users\%USERNAME%\AppData\Roaming\npm\node_modules\cordova\bin\;C:\Users\%USERNAME%\AppData\Local\Android\Sdk\tools;C:\Users\%USERNAME%\AppData\Local\Android\Sdk\platform-tools
    npm install -g cordova
    cordova create thello com.example.thello
    cd thello
    cordova platform add android
    cordova run
    android # API 22: SDK Platform + 2 muuta; NO System images
    Tools: Android SDK Platform-tools
    Tools: Android SDK Build-tools
    Android 5.1.1 (API 22): SDK Platform
    cordova run

  5. adb devices
    adb kill-server
    adb connect localhost:5555
    cordova run
    Try on your phone and blog about it. Ad-funded (“free”) blogs wordpress.com

  6. Miten aloitella Cordovalla?
    Cordovalla voit paketoida tavallisen weppisivun (HTML+JS+CSS) ohjelmaksi, jonka voit asentaa kännykälle tai myydä vaikkapa Google Playssa.
    Windows asennus koneelle, jossa on jo
    – Android Studio (ja Hello worldin saa pyörimään emulaattorissa)
    – NodeJS (npm-komento)
    http://terokarvinen.com/2015/hello-cordova-develop-android-apps-on-ubuntu-linux#comment-21203
    Linux asennus
    http://terokarvinen.com/2015/hello-cordova-develop-android-apps-on-ubuntu-linux
    KVM nopeuttaa emulaattoria huomattavasti
    http://terokarvinen.com/2015/hello-cordova-develop-android-apps-on-ubuntu-linux#comment-21189
    Osan voi kehittää selaimessa
    https://www.mozilla.org/fi/firefox/new/
    https://addons.mozilla.org/fi/firefox/addon/firebug/
    Tärkeitä kirjastoja
    https://jquery.com/
    http://code.jquery.com/jquery-2.1.4.min.js
    http://getbootstrap.com/
    https://github.com/twbs/bootstrap/releases/download/v3.3.5/bootstrap-3.3.5-dist.zip