Remco van't Veer has done a lot of great working porting Clojure to
Android, but one thing remains missing, runtime compilation which
would allow a fully functional Repl.  The problem is that the Android
VM doesn't use standard Java class files but rather Android specific
ones.  There is a host-side tool that converts Java byte codes to
Android ones.  But that tool doesn't normally run on Android.

So, being an Android systems hacker, I was wondering how hard it would
be to get runtime compilation working.

Turns out, it doesn't seem to be too hard.  The trick is basically to
run the Java to Android/Dalvik byte code converter through itself, so
that the convertor can be run on Android; if you load that converted
convertor into Android, you can then invoke it after each class is
compiled, essentially like this:

if(RT.booleanCast(COMPILE_FILES.deref()))
  {
    writeClassFile(internalName, bytecode);
    String strings[] = {"--dex", "--output=/data/gbj/classes.dex", "/
data/gbj/classes"};
    com.android.dx.command.Main.main(strings);
  }

(Note that *compile-path* is set elsewhere to "/data/gbj/classes")

Loading the generated class file is also a bit tricky, but not too
bad.


It isn't too fast, partly because of Android's slow GC and partly
because I'm using the filesystem as intermediate storage before
loading the classes, (because I haven't figured out how to do it all
in memory yet.)

I'll send out a full set of patches if there is interest, but for now
here is an Android app with the repl built in so you can play with
it.  (It is version of Remco's simple calculator app with a socket
based repl added.)

Much thanks to Remco upon whose Android/Clojure work this is all
built.

App install instructions below.

g


(Note: I've only tested these install instructions out on a Linux host
with this sdk: android-sdk-linux_x86-1.5_r2.zip)

Download the prebuilt apk file here:
http://georgejahad.com/clojure/calc-debug.apk



To configure the emulator:
emulator -avd <your avd name>
adb  install -r    calc-debug.apk
adb shell mkdir /data/gbj
adb shell mkdir /data/gbj/classes
adb shell chmod 777 /data/dalvik-cache
adb shell chmod 777 /data/gbj
adb shell chmod 777 /data/gbj/classes
adb forward tcp:8030 tcp:8030



Then start up the calc app from the emulator gui.

Then access the repl like so:
telnet localhost 8030

Allow compilation like so:
(def *android* true) (def *compile-files* true)

Now you can compile from the repl.  For example, this adds a simple
exponent operator to the calculator:

(in-ns 'examples.calc)
(defn exp [ b e] (reduce * (repeat b e)))
(def calc-opers {\+ #'+
                 \- #'-
                 \/ #'/
                 \* #'* \e #'exp})
(def calc-allowed-chars (.toCharArray "e0123456789."))

You can examine Remco's source code to see how the calculator works
here:
http://github.com/remvee/clj-android/blob/d1e96d33487ddcdc04b403e97f80fcf1b31bb9c2/examples/calc/src/examples/calc.clj


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to