"The Grid. A digital frontier. I tried to picture clusters of information as they move through the computer. What did they look like? Ships? Motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I’d never see. And then, one day, I got in." — Tron: Legacy

2013-05-31

Clojure on Android, quick intro

This time I'll show how to create Android application using Clojure programming language. Clojure is one of the most interesting practical functional languages ​​(in contrast e.g. to Haskell). It's a Lisp, so you can just run it and start hacking. This language makes it easy to play with code and stimulates curiosity.


It's just a very short intro to the configuration and ho to run basic program. For the impatient who want more I recommend a look at the neko library here: https://github.com/alexander-yakushev/neko





Required tools:
  • Emulator or Android tablet / phone with the system version 2.3 or greater. On the film, I'm using Android 4.0 with VirtualBox
  • Leiningen version 2.0 +
  • Android SDK and target platform minimum of 10 for version 2.3
  • Text editor with support for nREPL or SWANK - it needs to be able to manually connect to a server which is embedded into application. I'm using Eclipse with Counterclockwise plug-in. You may try to use Emacs or Vim - for me these two are just too much hipster.
Preparation:

After downloading lein.bat, copy it to a directory which is included in your system PATH variable.
Then, run in a terminal:

 lein self-install

Now, you need to install a plugin for Leiningen, which will automate creation/compilation and execution of your project on Android. Edit "~/.lein/profiles.clj", ~ - is user's home directory. If this file doesn't exist, you must create it . The purpose of all this is to provide a path to the Android SDK. Mine is for ex.:

{:user
   {:android {:sdk-path "/path/to/sdk"}
    :plugins [[lein-droid "0.1.0-preview5"]]
    :dependencies []}}

Now you can create a demo-project application.

Run this to update dependencies:

lein deps

Creating a basic application:

lein droid new demo org.demo :activity DemoActivity  :target-sdk 10 :app-name DemoApp

cd demo

Create Maven config file. It will later be easier to import project into Eclipse IDE.

lein pom

Now you need to run Android emulator or connect your device to USB. I'm starting a virtual machine, waiting for it to load, and then execute this in terminal:
adb connect 192.168.1.18  - this is my Android's emulator IP. To find yours hit Ctrl+Alt+F1 in emulator and run 'ip a' command, You will have to find your IP from the result. It should look like 192.168.*.* Ctrl+Alt+F7 to switch back to GUI.

Building and running project:

lein droid doall

Project should be built and launched on an emulator. Application has a running REPL server at the default port 9999.


In Eclipse IDE select File/Import... from menu. As import choose "Maven project" and locate pom.xml file. Now Eclipse is ready to connect to Demo app. Again from Eclipse's menu choose "Window" and select "Connect to REPL". Type in emulator's or device's IP address and port.

This is how it looks like on my PC. Step by step. I'm assuming all required tools are present:

 


1 komentarz:

  1. Thanks for this demo! Playing around with clojure on android is now at the top of my todo list!

    OdpowiedzUsuń