Scala on Android

19 Jan 2009

Chris DiBona gave me a free G-phone after an open-source talk last week (thanks!) with the admonishment that I write an app for it. I’m going to give it an effort, at least!

My first task was to figure out the scala-android build process, because that will make coding much easier. Turns out to be easier than the instructions I found online, so I thought I’d share.

You don’t have to use Eclipse like the instructions say. You can use the ant build file, but you have to modify it to find and compile using the scala compiler.

First, copy scala-compiler.jar and scala-library.jar out from your scala-home and into your android project folder. Since the compiler is only needed for building, and the library is the only one you’ll need installed, I made a folder for the compiler and put the library in libs/

$ mkdir scala-compiler
$ cp $SCALA_HOME/lib/scala-compiler.jar scala-compiler/
$ cp $SCALA_HOME/lib/scala-library.jar libs/

Second, edit build.xml and change the compile task to look like this:

<target name="compile" depends="dirs, resource-src, aidl">
    <javac encoding="ascii" target="1.5" debug="true" extdirs=""
           srcdir="${srcdir}" includes="**/*.java"
            <fileset dir="${external-libs}" includes="*.jar"/>

    <taskdef resource="scala/tools/ant/antlib.xml"
             classpath="scala-compiler/scala-compiler.jar:${external-libs-ospath}/scala-library.jar" />
    <scalac force="changed" deprecation="on"
            srcdir="${srcdir}" includes="**/*.scala"
            <fileset dir="${external-libs}" includes="*.jar"/>

Third, you need to edit dx from the android toolkit so that it gets lots of memory. Uncomment the line with the java heap option and give it a lot of memory, like 512MB ("-Xmx=512m"). It needs this because dex is going to convert the entire scala library into dalvik. Without a lot of memory, it will run out of heap space.

The end result is a little wasteful: even a tiny “hello world” app is 800K, probably because of that big ol' scala library. I suspect dex is proactively throwing away classes that I didn’t use, or it would be even bigger.

« Back to article list

Please do not post this article to Hacker News.