Clojure and Android with Emacs on Ubuntu

Prerequisites

This tutorial assumes that the following tutorials have been successfully completed:

Install Clojure for Android

The current version of clojure requires some slight modifications for use on Android. Install and compile a patched version specifically for Android projects.

~$ cd ~/opt

~$ git clone git://github.com/remvee/clojure.git clojure-android

~$ cd clojure-android

~$ ant

Modify TestProject for Clojure

In the Android with Emacs on Ubuntu tutorial, an application named TestProject was created and deployed to the Android emulator. Follow these steps to modify this pproject to use clojure instead of Java.

Copy the android specific clojure.jar file to the project library directory.

~$ cp ~/opt/clojure-android/clojure.jar ~/projects/TestProject/libs

Backup the original, activitycreator generated build.xml file.

~$ cd ~/projects/TestProject

~$ mv build.xml build.xml.org

Using a web browser, open the following project from Remco van 't Veer's github site.


http://github.com/remvee/clojurehelloandroid/tree/master

Download the following files to the ~/projects/TestProject directory. To download the files from github correctly, open the file and then download the file using the link labeled raw.

build.xml

compile.clj

Modify the second line in the new build.xml file to reflect the current project name.

<project name="TestProject" default="debug">

Remove the existing TestProject.java source file (leaving the R.java file) and create a clojure-based source file.

~$ ~/projects/TestProject/src/us/riddell

~$ rm TestProject.java

~$ emacs TestProject.clj

Add the following source and save the file.

(ns us.riddell.TestProject
  (:gen-class
   :extends android.app.Activity
   :exposes-methods {onCreate superOnCreate}))
 
(defn -onCreate [this #^android.os.Bundle bundle]
  (.superOnCreate this bundle)
  (.setContentView this us.riddell.R$layout/main)
  (let [tv (new android.widget.TextView this)]
    (.setText tv "Hello Android from Clojure!")
    (.setContentView this tv)))

Note: This version creates a new TextView which overrides the version in the main.xml file used in the previous tutorial.

Deploy Clojurized TestProject

Compile the project to make sure everything checks out.

M-x android-ant-compile

After a BUILD SUCCESSFUL, start the emulator and reinstall the project.

M-x android-start-emulator

M-x android-ant-reinstall

After opening the application, the new greeting is displayed. The clojure version of the application does start more slowly than the Java version (by at least 4 seconds it seems.) Once loaded, however, the speed seems comparable. Since clojure has not been completely optimized for use within an Android envrionment, hopefully this delay is further reduced in the future.