Alfredo the (Android) Clojurist

In the last post, I was just wondering how it could be possible to write a simple Clojure-based Android app. Well, I’ve succeeded, so I want to share this simple experiment with you. The main idea behind this weird experiment is that  you can import a java .jar into your Android application, then call any public-exposed method within your Android app: due to the fact that Leningen allows you to export a Clojure project into a STANDALONE jar, who prevent you to use all this awesomeness??

Download the Android SDK

In order to develop full-fledged Android apps you need the Android SDK, you can download it from here (I really like the site’s graphics). Once you have chosen your favourite platform, unzip che SDK and run the android executable (you can find it into the bin directory). The scope of this post is not teaching you how to install the Android SDK, so I’ll move further 🙂

Setting the environment

Although you can use your favourite IDE or editor, I really like IntelliJIdea, cause it provides Clojure as well as Android support. You can easily install LaClojure plugin from Idea’s plugin manager.

Another Yet Greeter out there

Which is the dumbest program you can create? We’ll create a simple program that greet you (wow, cool!). Create a Leiningen project as usual:

lein new greeter

Then write this into your core.clj file:

(ns greeter.core
  (:gen-class
    :methods [#^{:static true} [greet [String] String]]))

(defn -main [&args] )

(defn greet [param]
  (str "Hello, " param))

(defn -greet
  "Java wrapper around greet method"
  [param]
  (greet param))

The code is pretty straightforward, just two observation:

  • You can expose a method to outside with the minus before the function/method name (e.g. -greet)
  • You must specify the Javish signature for your metod, see the line under :gen-class

Now we can easily export this project as a standalone .jar (remember to set the :main method into project.clj!!):

lein uberjar greeter

Now we are ready to embed our greeter into an Android project.

Final step

Now we are going to create a new Android project. For the brevity sake, I suggest you to use an IDE.
In IntelliJ Idea you can create a new Android project from stratch: (File -> New Project -> Android Module). Leave all the options to default. Now open MyActivity.java and you should see something like this:

package com.example;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Now we have to import out greeter standalone, so right click on the name of project in the project panel and click on “Open Module settings”, go to the “Dependencies” tab and add your brand-new standalone: greeter-1.0-SNAPSHOT.jar. Apply and quit.

As the last step, we have to hack a little MyActivity.java code, in order to greet using our greet method:

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import greeter.core;

public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText(core.greet("Alfredo the Clojurist"));
        setContentView(tv);
    }
}

Now run the project (you have to configure down the emulator, I don’t think it will be too difficult) and BAM!:

This is only the tip of the iceberg. Stay tuned for other weird experiments. The Frankestein era has begun!