47 Degrees joins forces with Xebia read more

Scala on Android - Preparing the environment

Scala on Android - Preparing the environment

Wouldn’t it be great not to have to check every possible null field? What about using functional programming in our Android applications? These days, everybody is looking for ways to innovate on the Android platform, whether it’s by redefining architectures, creating an endless list of new libraries, or creating studies about reborn patterns. But let’s face it, the big anchor in Android development is the language. Scala may not be the definitive solution, but it can provide us with the evolutionary leap that we’re looking for.

We’ll start this series of posts with the aim of helping other developers with programming in Android with Scala. There are a lot of good resources out there to learn this great programming language, but the two that helped me the most were Martin Odersky’s course Functional Programming Principles in Scala at Coursera and Daniel Westheide’s blog and his series of posts The Neophyte’s Guide to Scala.

SBT Build System

Coming back to the subject of this post, in Scala we use the SBT build system. In broad terms, SBT is a build system like Ant, Maven, or Gradle, but it provides a more confortable development environment for Scala-based projects. Based on Ivy for dependency management, SBT allow us create the build descriptions in Scala code and can be used in pure Java projects too.

The first step is to install Scala. You can find the steps and instructions on the Scala website. My personal recommendation is to install these three options: Scala, Activator, and the IDE, preferably IntelliJ IDEA with the Scala plugin.

Project Structure

Lets take a look at the structure for a simple project. If you have worked previously with Maven or Gradle you will notice that the project structure is very similar.

|- project/
|  |- plugins.sbt 
|- src/
|  |- main/
|     |- assets/
|     |- java/
|     |- res/
|        |- layout/
|           |- main.xml
|        |- values/
|           |- strings.xml
|     |- scala/
|        |- com/
|           |- fortysevendeg/
|              |- scala/
|                 |- android/
|                    |- SampleActivity.java/
|     |- AndroidManifest.xml
|  |- test/
|     |- java/
|     |- res/
|     |- scala/
|- build.sbt

Configuration Files

Now we are going to see the content of the SBT specific files.


// Using Android Plugin
// Specifying the Android target Sdk version
platformTarget in Android := "android-21"

// Application Name
name := """scala-android"""

// Application Version
version := "1.0.0"

// Scala version
scalaVersion := "2.11.4"

// Repositories for dependencies
resolvers ++= Seq(Resolver.mavenLocal,

// Override the run task with the android:run
run <<= run in Android

// Activate proguard for Scala
proguardScala in Android := true

// Activate proguard for Android
useProguard in Android := true

// Set proguard options
proguardOptions in Android ++= Seq(
  "-keep class scala.Dynamic")

This is the main build file. We are specifying that we are using the Android plugin and configuring some basic aspects of the build.


// Android SDK Plugin
addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.3.18")

In this file, we’re importing the plugins. In our simple example, we only need the android-sdk-plugin, an SBT plugin developed by Perry Nguyen that works flawlessly with Scala on Android.

Now it’s time to run the project. The easy way is to use activator. Execute the next command:

> run

Here we are using run because in build.sbt we are overriding the task with the plugin’s task android:run. Alternatively, we could have executed:

> android:run

With these settings we have everything prepared for coding Scala on Android.

You can see the content of the rest of the files in this repository created for the post.

This project is available as an Activator template. You can find more information and the instructions on the template details web.

In the upcoming posts in this series, we’ll address other topics regarding Android development. Stay tuned for those!

If you have any questions, feel free to get in touch with us on Facebook, Twitter, or in the comments below.

Ensure the success of your project

47 Degrees can work with you to help manage the risks of technology evolution, develop a team of top-tier engaged developers, improve productivity, lower maintenance cost, increase hardware utilization, and improve product quality; all while using the best technologies.