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.
- (+) 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)
If the instructions below are too dry for your taste, check out these tutorials:
- Android & Scala (part 1) by Jan Macháček
- Scala on Android — Preparing the environment (part 1) by Federico Fernández
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:
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:
Recommended. Use sbt, Scala’s de-facto standard build system. You’ll have to install it from its website. To compile and run Android projects you’ll need the Android plugin. Follow the plugin’s readme page to set things up.
Not recommended. Just use an IDE without the build system. For Eclipse you’ll need the ADT plugin, Scala IDE and AndroidProguardScala plugin. For Intellij IDEA or Android Studio, which is the same, see this guide.
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:
Make sure that the paths to
AndroidManifest.xml and the
are configured properly. Go to Project settings → Modules → (select main module) → Android → Structure/Packaging.
* Manifest file should be
APK path should be
It is possible to preload Scala standard library on the emulator to reduce build times. Here is a tool to do that.