tag androidPer Bothner's bloghttp://per.bothner.com/blog/tag/android/Per Bothner's blogikiwiki2011-04-10T18:57:04ZHello world in Scheme for Androidhttp://per.bothner.com/blog/2010/AndroidHelloScheme/2011-04-10T18:55:46Z2010-10-18T21:27:26Z
<p>(This is an update of the <a href="http://per.bothner.com/blog/2009/AndroidHelloScheme/">2009 version</a>,
which was an update of the <a href="http://per.bothner.com/blog/2008/AndroidHelloScheme/">original 2008 version</a>.)</p>
<p>Google's phone operating system
<a href="http://code.google.com/android/">"Android"</a> is based on
a custom Java virtual machine on top of GNU/Linux.
So it occurred to me: How difficult would it be to
get a <a href="http://www.gnu.org/software/kawa/">Kawa</a>
application running on Android?
Not that difficult, it turns out.
<p>
Here is "Hello world" written in Kawa Scheme:
<pre>
(require 'android-defs)
(activity hello
(on-create-view
(android.widget.TextView (this)
text: "Hello, Android from Kawa Scheme!")))
</pre>
(A more interesting
<a href="http://androidscheme.blogspot.com/2010/10/text-to-speech-app.html">text-to-speech example app</a> is on
<a href="http://androidscheme.blogspot.com/">Santosh Rajan's Android-Scheme blog</a>.)
<p>
The following instructions have been tested on GNU/Linux,
specifically Fedora 13,
but if you've managed to build Android applications under (say) Windows,
you should be able to appropriately modify these instructions.
<h3>Getting and building Kawa and Android</h3>
<p>First <a href="http://code.google.com/android/download.html">download the Android SDK</a>. Unzip in a suitable location,
which we'll refer to as <code>ANDROID_HOME</code>.
<pre>
$ export ANDROID_HOME=/path/to/android-sdk-linux_86
$ PATH=$ANDROID_HOME/tools:$PATH
</pre>
<p>
Next you have to get the appropriate platform SDK:
<pre>
$ android update sdk
</pre>
Select <code>SDK Platform Android 2.2</code> or whatever
and click <code>Install</code>.
<p>
You need to select an Android <q>platform</q>.
Platform 8 corresponds to Android 2.2 (Froyo).
<pre>
ANDROID_PLATFORM=android-8
</pre>
<p>
You need to <a href="http://www.gnu.org/software/kawa/Getting-Kawa.html">get the Kawa source code</a> (version 1.11 or later).
<p>
Set <code>JAVA_HOME</code> to where your JDK tree is, for example:
<pre>
$ export JAVA_HOME=/opt/jdk/1.6.0
</pre>
<p>
If using Ant (as is recommended on Windows):
<pre>
$ ant -Denable-android=true
</pre>
<p>
Alternatively, you can use <code>configure</code> and <code>make</code>:
<pre>
$ KAWA_DIR=path_to_Kawa_sources
$ cd $KAWA_DIR
$ ./configure --with-android=$ANDROID_HOME/platforms/$ANDROID_PLATFORM/android.jar --disable-xquery --disable-jemacs
$ make
</pre>
<h3>Creating the application</h3>
<p>Next, we need to create a project or <q>activity</q>,
in the target directory <code>KawaHello</code>,
with the main activity being a class named <code>hello</code>
in a package <code>kawa.android</code>:
<pre>
$ android create project --target $ANDROID_PLATFORM --name KawaHello --activity hello --path ./KawaHello --package kawa.android
</pre>
<p>Replace the skeleton <code>hello.java</code> by the
Scheme code at the top of this note:
<pre>
$ cd KawaHello
$ HELLO_APP_DIR=`pwd`
$ cd $HELLO_APP_DIR/src/kawa/android/
$ rm hello.java
$ emacs hello.scm
</pre>
<p>
We need to copy/link the Kawa jar file so the Android SDK can find it:
<pre>
$ cd $HELLO_APP_DIR
$ ln -s $KAWA_DIR/kawa-1.10.jar libs/kawa.jar
</pre>
<p>Optionally, you can use <code>kawart-1.10.jar</code>, which is
slightly smaller, but does not support <code>eval</code>,
and does not get built by the Ant build:
<pre>
$ ln -s $KAWA_DIR/kawart-1.10.jar libs/kawa.jar
</pre>
<p>
We also need to modify the Ant <code>build.xml</code>
so it knows how to compile Scheme code:
<pre>
$ patch < <a href="http://per.bothner.com/blog/2010/AndroidHelloScheme/build-xml-patch.txt">build-xml-patch.txt</a>
</pre>
<p>
Finally, we can build our application:
<pre>
$ ant debug
</pre>
<h3>Running the application on the Android emulator</h3>
<p>First you need to
<a href="http://developer.android.com/guide/developing/other-ide.html#AVD">
create an Android Virtual Device (<code>avd</code>)</a>.
Start:
<pre>
android
</pre>
then select <code>Virtual Devices</code>, then click <code>New...</code>.
Pick a <code>Name</code> (we use <code>avd8</code> in the following),
a <code>Target</code> (to match <code>$ANDROID_PLATFORM</code>),
and optionally change the other properties, before clicking <code>Create AVD</code>.
<p>
Start up the Android emulator:
<pre>
$ emulator -avd avd8 &
</pre>
<p>
Wait until Android has finished booting (you will see the Android home screen),
click the menu and home buttons.
Now install our new application:
<pre>
adb install bin/KawaHello-debug.apk
</pre>
<p>The new <code>hello</code> application should show up.
Click it, and you should see something like:
<img src="http://per.bothner.com/blog/2010/AndroidHelloScheme/HelloKawa2.png" alt="HelloKawa2.png" />
<h3>Running the application on your phone</h3>
<p>If the emulator is running, kill it:</p>
<pre>
$ kill %emulator
</pre>
<p>On the phone, enable <code>USB debugging</code>. (This is settable from the
<code>Settings</code> application, under <code>Applications</code> / <code>Development</code>.)
<p>
Connect the phone to your computer with the USB cable.
Verify that the phone is accessible to <code>adb</code>:</p>
<pre>
$ adb devices
List of devices attached
0A3A560F0C015024 device
</pre>
<p>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:
<pre>
$ 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
...
</pre>
<p>The timestamp corresponds to when you connected the phone.
Make it readable:</p>
<pre>
$ sudo chmod a+w /dev/bus/usb/001/006
</pre>
<p>
Obviously if you spend time developing for an Androd phone you'll
want to automate this process;
<a href="http://sites.google.com/site/siteofhx/Home/android/drivers/udc">this link</a> or
<a href="http://groups.google.com/group/android-developers/browse_thread/thread/9d37e184f92d19f3">this link</a> may be helpful.
<p>
Anyway, once <code>adb</code> can talk to the phone, you
install in the same way as before:
<pre>
adb install bin/KawaHello-debug.apk
</pre>
<h3>Some debugging notes</h3>
<p>
You will find a copy of the SDK documentation in
<code>$ANDROID_HOME/docs/index.html</code>.
<p>If the emulator complains that your application
<q>has stopped unexpectedly</q>, do:</p>
<pre>
$ adb logcat
</pre>
<p>This shows log messages, stack traces, output from the <code>Log.i</code>
logging method, and other useful information.
(You can alternatively start <code>ddms</code>
(Dalvik Debug Monitor Service), click on the <code>kawa.android</code>
line in the top-left sub-window to select it, then from the
<code>Device</code> menu select <code>Run logcat...</code>.)
<p>To uninstall your application, do:
<pre>
$ adb uninstall kawa.android
</pre>
Hello world in Scheme for Androidhttp://per.bothner.com/blog/2009/AndroidHelloScheme/2011-04-10T18:53:48Z2009-01-18T00:12:11Z
<p><em>(<a href="http://per.bothner.com/blog/2010/AndroidHelloScheme/">Here is the latest version</a>.</em>
This is in turn an update of the <a href="http://per.bothner.com/blog/2008/AndroidHelloScheme/">original version</a>.)
</p>
<p>Google's phone operating system
<a href="http://code.google.com/android/">"Android"</a> is based on
a custom Java virtual machine on top of GNU/Linux.
So it occurred to me: How difficult would it be to
get a <a href="http://www.gnu.org/software/kawa/">Kawa</a>
application running on Android?
Not that difficult, it turns out.
<p>
Here is "Hello world" written in Kawa Scheme:
<pre>
(require 'android-defs)
(activity hello
(on-create-view
(let ((tv (android.widget.TextView (this))))
(tv:setText "Hello, Android from Kawa Scheme!")
tv)))
</pre>
<p>
The following instructions have been tested on GNU/Linux,
specifically Fedora 10,
but if you've managed to build Android applications under (say) Windows,
you should be able to appropriately modify these instructions.
The article <a href="http://www.sjbaker.org/wiki/index.php?title=Android_Phone_development_from_the_Linux_command-line">Android Phone development from the Linux command-line</a> helped me figure out what to do.
<h3>Getting and building Kawa and Android</h3>
<p>First <a href="http://code.google.com/android/download.html">download the Android SDK</a>. Unzip in a suitable location,
which we'll refer to as <code>ANDROID_HOME</code>.
<pre>
$ ANDROID_HOME=/path/to/android-sdk-linux_x86-1.0_r2
$ PATH=$ANDROID_HOME/tools:$PATH
</pre>
<p>
To get things to work I had to make some modest changes to Kawa,
so you will need to get the <a href="http://www.gnu.org/software/kawa/Getting-Kawa.html">Kawa developer sources from SVN</a>.
<p>
You need to <code>configure</code> and <code>make</code> Kawa appropriately:
<pre>
$ KAWA_DIR=path_to_Kawa_sources
$ cd $KAWA_DIR
$ ./configure --with-android=$ANDROID_HOME/android.jar --disable-xquery
$ make
</pre>
<h3>Creating the application</h3>
<p>Next, we need to create a project or activity,
in the target directory <code>KawaHello</code>,
with the main <q>activity</q> being a class named <code>hello</code>
in a package <code>kawa.android</code>:
<pre>
$ activitycreator --out KawaHello kawa.android.hello
</pre>
<p>Replace the skeleton <code>hello.java</code> by the
Scheme code at the top of this note:
<pre>
$ cd KawaHello
$ HELLO_APP_DIR=`pwd`
$ cd $HELLO_APP_DIR/src/kawa/android/
$ rm hello.java
$ emacs hello.scm
</pre>
<p>
We need to copy/link the Kawa jar file so the Android SDK can find it:
<pre>
$ cd $HELLO_APP_DIR
$ ln -s $KAWA_DIR/kawa-1.9.3.jar libs/kawa.jar
</pre>
<p>
We also need to modify the Ant <code>build.xml</code>
so it knows how to compile Scheme code:
<pre>
$ patch < <a href="http://per.bothner.com/blog/2009/AndroidHelloScheme/build-xml-patch.txt">build-xml-patch.txt</a>
</pre>
<p>
Finally, we can compile our application:
<pre>
$ ant
</pre>
<h3>Running the application on the Android emulator</h3>
<p>
Start up the Android emulator:
<pre>
$ emulator&
</pre>
<p>
Wait until Android has finished booting, clisk the menu and home buttons.
Click the tab above the menu key to show the installed applications.
Now install our new application:
<pre>
adb install bin/hello-debug.apk
</pre>
<p>The new <code>hello</code> application should show up.
Click it, and you should see something like:
<img src="http://per.bothner.com/blog/2008/AndroidHelloScheme/HelloKawa1.png" alt="HelloKawa1.png" />
<h3>Running the application on the G1 phone</h3>
<p>If the emulator is running, kill it:</p>
<pre>
$ kill %emulator
</pre>
<p>
Connect the phone to your computer with the USB cable.
Verify that the phone is accessible to <code>adb</code>:</p>
<pre>
$ adb devices
List of devices attached
HT849GZ17337 device
</pre>
<p>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:
<pre>
$ ls -l /dev/bus/usb/*
/dev/bus/usb/001:
total 0
...
crw-rw-rw- 1 root wheel 189, 5 2009-01-18 16:52 006
...
</pre>
<p>The timestamp corresponds to when you connected the phone.
Make it readable:</p>
<pre>
$ sudo chmod a+w /dev/bus/usb/001/006
</pre>
<p>
Obviously if you spend time developing for an Androd phone you'll
want to automate this process;
<a href="http://sites.google.com/site/siteofhx/Home/android/drivers/udc">this link</a> or
<a href="http://groups.google.com/group/android-developers/browse_thread/thread/9d37e184f92d19f3">this link</a> may be helpful.
<p>
Anyway, once <code>adb</code> can talk to the phone, you
install in the same way as before:
<pre>
adb install bin/hello-debug.apk
</pre>
<h3>Some debugging notes</h3>
<p>
You will find a copy of the SDK documentation in
<code>$ANDROID_HOME/docs/documentation.html</code>.
<p>If the emulator complains that your application
<q>has stopped unexpectedly</q>, do:</p>
<pre>
$ adb logcat
</pre>
<p>This shows log messages, stack traces, output from the <code>Log.i</code>
logging method, and other useful information.
(You can alternatively start <code>ddms</code>
(Dalvik Debug Monitor Service), click on the <code>kawa.android</code>
line in the top-left sub-window to select it, then from the
<code>Device</code> menu select <code>Run logcat...</code>.)
<p>To uninstall your application, do:
<pre>
$ adb uninstall kawa.android
</pre>
Hello world in Scheme for Androidhttp://per.bothner.com/blog/2008/AndroidHelloScheme/2011-04-10T18:57:04Z2008-12-24T19:59:31Z
<p><em>(This has been updated <a href="http://per.bothner.com/blog/2010/AndroidHelloScheme/">here</a>.)</em></p>
<p>Google's phone operating system
<a href="http://code.google.com/android/">"Android"</a> is based on
a custom Java virtual machine on top of GNU/Linux.
So it occurred to me: How difficult would it be to
get a <a href="http://www.gnu.org/software/kawa/">Kawa</a>
application running on Android?
Not that difficult, it turns out.
<p>
Here is "Hello world" written in Kawa Scheme:
<pre>
(module-extends android.app.Activity)
(module-name kawa.android.hello)
(define (onCreate (savedInstanceState :: android.os.Bundle)) :: void
(invoke-special android.app.Activity (this) 'onCreate savedInstanceState)
(let ((tv :: android.widget.TextView (make android.widget.TextView (this))))
(tv:setText "Hello, Android from Kawa Scheme!")
((this):setContentView tv)))
</pre>
<p>
It's got some annoying boiler-plate, though it's similar to
the Java version; hopefully we can simplify later.
<p>Here is how to get this program running on the Android emulator
on GNU/Linux.
(I haven't yet figured out how to get it working on the actual phone.)
This article <a href="http://www.sjbaker.org/wiki/index.php?title=Android_Phone_development_from_the_Linux_command-line">Android Phone development from the Linux command-line</a> was helpful in figuring out what to do.
<p>First you need to <a href="http://code.google.com/android/download.html">download the Android SDK</a>. Unzip, in a suitable location,
which we'll refer to as <code>ANDROID_HOME</code>:
<pre>
ANDROID_HOME=/path/to/android-sdk-linux_x86-1.0_r2
PATH=$ANDROID_HOME/tools:$PATH
</pre>
<p>
To get this to work I had to make some modest changes to Kawa,
so you will need to get the <a href="http://www.gnu.org/software/kawa/Getting-Kawa.html">Kawa developer sources from SVN</a>.
<p>
You need to <code>configure</code> and <code>make</code> Kawa appropriately:
<pre>
KAWA_DIR=path_to_Kawa_sources
cd $KAWA_DIR
./configure --with-android=$ANDROID_HOME/android.jar
make
</pre>
<p>Next, we need to create a project or activity,
in the target directory <code>KawaHello</code>,
with the main <q>activity</q> being a class named <code>hello</code>
in a package <code>kawa.android</code>:
<pre>
activitycreator --out KawaHello kawa.android.hello
</pre>
<p>Replace the skeleton <code>hello.java</code> by the
Scheme code we started out with:
<pre>
cd KawaHello
HELLO_APP_DIR=`pwd`
cd $HELLO_APP_DIR/src/kawa/android/
rm hello.java
emacs hello.scm
</pre>
<p>
We need to copy/link the Kawa jar file so the Android SDK can find it:
<pre>
cd $HELLO_APP_DIR
ln -s $KAWA_DIR/kawa-1.9.3.jar libs/kawa.jar
</pre>
<p>
We also need to modify the Ant <code>build.xml</code>
so it knows how to compile Scheme code:
<pre>
patch < <a href="http://per.bothner.com/blog/2008/AndroidHelloScheme/build-xml.patch">build-xml.patch</a>
</pre>
<p>
Finally, we can compile our application:
<pre>
ant
</pre>
<p>
Next start up the Android emulator:
<pre>
emulator&
</pre>
<p>
Wait until Android has finished booting, clisk the menu and home buttons.
Click the tab above the menu key to show the installed applications.
Now install our new application:
<pre>
adb install bin/hello-debug.apk
</pre>
<p>The new <code>hello</code> application should show up.
Click it, and you should see something like:
<img src="http://per.bothner.com/blog/2008/AndroidHelloScheme/HelloKawa1.png" alt="HelloKawa1.png" />
<h3>Some debugging notes</h3>
<p>
You will find a copy of the SDK documentation in
<code>$ANDROID_HOME/docs/documentation.html</code>.
<p>If the emulator complains that your application
<q>has stopped unexpectedly</q>, start <code>ddms</code>
(Dalvik Debug Monitor Service), click on the <code>kawa.android</code>
line in the top-left sub-window to select it, then from the
<code>Device</code> menu select <code>Run logcat...</code>.
This shows log messages, stack traces, output from the <code>Log.i</code>
loggin method, and other useful information.
<p>To uninstall your application, do:
<pre>
adb uninstall kawa.android
</pre>