Google’s phone/tablet operating system Android is based on a custom virtual machine on top of a Linux kernel. Even though Android isn’t strictly (or legally) speaking Java, you can build Android applications using Kawa.
Below is "Hello world" written in Kawa Scheme. A slightly more interesting example is in next section.
(require 'android-defs) (activity hello (on-create-view (android.widget.TextView (this) text: "Hello, Android from Kawa Scheme!")))
The following instructions have been tested on GNU/Linux, specifically Fedora 17. This link may be helpful if you’re building on Windows.
First download the Android SDK. Unzip in a suitable location, which we’ll refer to as
export ANDROID_HOME=/path/to/android-sdk-linux PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
Next you have to get the appropriate platform SDK:
$ android update sdk
You need to select an Android “platform”.
Platform (API) 16 corresponds to Android 4.1.2 (Jelly Bean).
Select that or whatever you prefer, and click
(You can install multiple platforms, but each project
is built for a specific platform.)
JAVA_HOME to where your JDK tree is.
You should use JDK 6; JDK 7 does not work at time of writing.
$ export JAVA_HOME=/opt/jdk1.6
First get the Kawa source code.
If using Ant (as is recommended on Windows):
$ ant -Denable-android=true
Alternatively, you can use
$ KAWA_DIR=path_to_Kawa_sources $ cd $KAWA_DIR $ ./configure --with-android=$ANDROID_HOME/platforms/$ANDROID_PLATFORM/android.jar --disable-xquery --disable-jemacs $ make
Next, we need to create a project or “activity”.
This tutorial assumes you want to create the project
in the target directory
with the main activity being a class named
hello in a
PROJECT_DIR=KawaHello PROJECT_CLASS=hello PROJECT_PACKAGE=kawa.android PROJECT_PACKAGE_PATH=kawa/android
To create the project use the following command:
$ android create project --target $ANDROID_PLATFORM --name $PROJECT_DIR --activity $PROJECT_CLASS --path ./$PROJECT_DIR --package $PROJECT_PACKAGE
Replace the skeleton
hello.java by the Scheme code at the
top of this note, placing in a file named
$ cd $PROJECT_DIR $ HELLO_APP_DIR=`pwd` $ cd $HELLO_APP_DIR/src/$PROJECT_PACKAGE_PATH $ rm $PROJECT_CLASS.java $ create $PROJECT_CLASS.scm
We need to copy/link the Kawa jar file so the Android SDK can find it:
$ cd $HELLO_APP_DIR $ ln -s $KAWA_DIR/kawa-3.1.1.jar libs/kawa.jar
Optionally, you can use kawart-3.1.1.jar, which is slightly smaller, but does not support eval, and does not get built by the Ant build:
$ ln -s $KAWA_DIR/kawart-3.1.1.jar libs/kawa.jar
Copy or link
custom_rules.xml from the Kawa sources:
ln -s $KAWA_DIR/gnu/kawa/android/custom_rules.xml .
Finally to build the application just do:
$ ant debug
First you need to create an Android Virtual Device (avd). Start:
Then from menu
In the new window click
Name (we use
avd16 in the following),
Target (to match
and optionally change the other properties, before clicking
Now you can start up the Android emulator:
$ emulator -avd avd16 &
Wait until Android has finished booting (you will see the Android home screen), click the menu and home buttons. Now install our new application:
adb install bin/KawaHello-debug.apk
If the emulator is running, kill it:
$ kill %emulator
On your phone or other Android devude, enable USB debugging.
(This is settable from the
Applications / Development.)
Connect the phone to your computer with the USB cable.
Verify that the phone is accessible to
$ adb devices List of devices attached 0A3A560F0C015024 device
If you don’t see a device listed, it may be permission problem. You can figure out which device corresponds to the phone by doing:
$ ls -l /dev/bus/usb/* /dev/bus/usb/001: total 0 ... crw-rw-rw- 1 root wheel 189, 5 2010-10-18 16:52 006 ...
The timestamp corresponds to when you connected the phone. Make the USB connection readable:
$ sudo chmod a+w /dev/bus/usb/001/006
Obviously if you spend time developing for an Androd phone you’ll want to automate this process; this link or this link may be helpful.
adb can talk to the phone, you install in the same way as before:
adb install bin/KawaHello-debug.apk
You will find a copy of the SDK documentation in
If the emulator complains that your application has stopped unexpectedly, do:
$ adb logcat
This shows log messages, stack traces, output from the
Log.i logging method, and other useful information.
(You can alternatively start
ddms (Dalvik Debug Monitor Service), click on the
kawa.android line in the top-left sub-window to select it, then from the
Device menu select
To uninstall your application, do:
$ adb uninstall kawa.android
(A more interesting text-to-speech example app is on Santosh Rajan’s Android-Scheme blog.)