Scala 2.9.0 on Android

The new 2.9.0 version of the Scala programming language was released recently and in this tutorial, I’ll show you how it can be used to program Android. Read on for an explanation of how Scala compilation is added to the Android build system.

Since Scala runs in the JVM, it should theoretically be possible to use it to write Android applications and various blog posts on the web suggest that others have had success in writing at least basic demo applications. None of the tutorials worked out of the box for me and almost all, if not all were based on very old Android versions. Different versions of Scala releases that I had obtained were also built with different versions of Java and the ones I found were not binary compatible with Android 2.2. So I have pieced together bits and pieces of knowledge that I have read from around the web, hacked around and created a standalone Scala Android application with all the necessary JAR file dependencies.

For this tutorial, I assume that the reader has a familiarity with how to build a basic Android application and also how to build a basic Scala application.

Overview of the build process

The way to get Scala working on Android involves inserting the Scala compilation step into the Android build process. Scala applications are also dependent on the Scala library JAR file which makes the mobile application that uses it very large. In fact, as described in this bug report, the Scala library is too large to go through the dexing process. This means that a second step needs to be inserted into the Android build process: the use of Proguard to reduce the JAR size to just the necessary classes.

The build process therefore involves the following steps:

  1. Compile Scala files
  2. Compile Java file (the Android resource class R.java will still be generated by the build process)
  3. Run Proguard over the output of the compilation steps and dependencies such as scala-library.jar
  4. Convert to dex and package up the application

Adapting the Android application to build Scala code:

In order to insert the above steps into the Android application build process, the Android application needs to be set up to build with ant on the command line. (This is not the only method to set up Android but is the method used in this guide.)

The build.xml file in the root of the Android project directory can then be modified to incorporate the additional steps.

The build.xml uses Ant’s main_rules.xml as a template. This file can be found in
/tools/ant/main_rules.xml in Android SDK 2.2.

Most of main_rules.xml is copy and pasted into build.xml with a few additions: the Scala compilation step and the Proguard step.

The Scala compilation step is achieved by adding the following task to the build.xml. The scala-compiler.jar and scala-library.jar files are included in a subdirectory of the Android project called tools.

<taskdef resource="scala/tools/ant/antlib.xml"
classpath="tools/scala-compiler.jar:tools/scala-library.jar" />
<scalac force="changed" deprecation="on"
destdir="${out.classes.absolute.dir}"
bootclasspathref="android.target.classpath">
<src path="${source.absolute.dir}" />
<src path="${gen.absolute.dir}" />
<classpath>
<fileset dir="${jar.libs.absolute.dir}" includes="*.jar" />
<fileset dir="tools" includes="*.jar"/>
</classpath>
</scalac>

Proguard is added to the build process by adding the following task to build.xml. Again the proguard.jar file is included in a subdirectory of the project called tools.

<target name="proguard" depends="compile">
<taskdef resource="proguard/ant/task.properties"
classpath="tools/proguard.jar" />
<proguard>
-injars ${out.classes.absolute.dir}:tools/scala-library.jar(!META-INF/MANIFEST.MF,!library.properties)
-outjars "${out.absolute.dir}/classes.min.jar"
-libraryjars "${android.jar}"
-dontwarn
-dontoptimize
-dontobfuscate
-keep public class * extends android.app.Activity
</proguard>
</target>

Building and running an example project

For this tutorial I have the following installed and working:

  • Android Linux SDK v2.2
  • Scala 2.9.0 (edit: updated to 2.9.1 final now)
  • Proguard 4.4

The HelloScalaOnAndroid project has been pushed to Github and can be cloned from there.

Clone the source from the above URL.

Start the Android 2.2 emulator and type:

ant clean install

If everything is set up properly, the application will have been installed on the running Android emulator.

You can leave a response, or trackback from your own site.

7 Responses to “Scala 2.9.0 on Android”

  1. [...] small update to Scala 2.9.0 has been released and I have updated the Scala on Android project to use the latest version. See the changelog for details on exactly what has changed in Scala [...]

  2. Matthias Granberry says:

    If you’re not careful, you will end up packaging up your scala source with the apk as well. You may want to remove the line ” from within the element in build.xml

    • Matthias Granberry says:

      It ate my XML. You will need to remove the “sourcefolder path=” element from the apkbuilder task in build.xml to keep from having your Scala source included in the final apk.

      • NotesAndReviews says:

        Thanks for pointing this out, Matthias. I’m just a novice at Scala and have only hacked around with Ant so didn’t know what some of the parameters were for.

  3. Nice to see people trying to get scala to work with Android. However your project seems to fail where others have as well. I’ve given the details on the github:

    https://github.com/gw111zz/HelloScalaOnAndroid/issues/2

    Other attempts, which also didn’t work:
    http://code.google.com/p/scalaforandroid/
    http://chneukirchen.org/blog/archive/2009/04/programming-for-android-with-scala.html
    http://nevercertain.com/index.php/2011/02/scala-android-intellij-win-part-1-prerequisites/

    • NotesAndReviews says:

      Thanks for the comment, Daniel. I’ll take a look at the issue on Github in detail when I have more time on the weekend.

  4. [...] The Scala on Android project has been updated to use Scala 2.9.1 final and the latest (at the time of writing) Android development tools (r15). The goal of the project is to demonstrate that Scala can be used on Android and is completely standalone. For full details on what this project is and how it works, see this other post. [...]

Leave a Reply