Scala on Android

Writing your Android project in Scala is becoming smoother and smoother. This is a short guide that will help you get started.

Pros/cons

  • (+) Concise code
  • (+) Fantastic concurrency support (futures/promises, scala-async, actors, …)
  • (+) Advanced DSLs (like Macroid ;) )
  • (−) Build time (due to ProGuard + dex taking up to 1 minute on big projects)

Tutorials

If the instructions below are too dry for your taste, check out these tutorials:

The Android SDK

The SDK can be downloaded from the Android website. You will also need to configure ANDROID_HOME environment variable to point to the installation.

To use the bundled libraries, such as the support library, make sure you install the following items in the SDK manager:

SDK manager screenshot

Alternatively, you can find a UNIX install script in Macroid’s Travis config.

The build system

The most important part of the equation is the build system (in mainland Android you are likely to use one anyway). After configuring the build you will be able to compile and run the project from the command line and generate IDE project files automatically.

Currently there are three options:

The IDE

The recommended IDE is Intellij IDEA or Android Studio, which is the same. You will need to have the Scala and sbt plugins installed. Below I am assuming that you are using the first building option, i.e. sbt.

Once you have the build running from the command line, follow the instructions for the sbt plugin to create IDE project files. Since IDEA 14, you just need to import the project and specify Android SDK as its SDK.

Inside IDEA, you can use the sbt console to run the project. Alternatively, the “run” button could be reconfigured for that purpose: go to Run → Edit Configurations → (select a configuration) → Before launch. You need to configure it to look like this:

before launch

Make sure that the paths to AndroidManifest.xml and the APK are configured properly. Go to Project settings → Modules → (select main module) → Android → Structure/Packaging. * Manifest file should be /path-to-project/src/main/AndroidManifest.xml:

manifest path

  • APK path should be path-to-project/target/android-bin/build_integration/{module name}-BUILD-INTEGRATION.apk:

    apk path

Additional steps

It is possible to preload Scala standard library on the emulator to reduce build times. Here is a tool to do that.

Useful resources