Scala on Android - Preparing the environment
- by Fede Fernández Beltrán
- April 08, 2015
- scala• android• code tutorials• scala on android
- 4 minutes to read.
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.
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.
scala-android/ |- 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
Now we are going to see the content of the SBT specific files.
// Using Android Plugin android.Plugin.androidBuild // 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, DefaultMavenRepository, Resolver.typesafeRepo("releases"), Resolver.typesafeRepo("snapshots"), Resolver.typesafeIvyRepo("snapshots"), Resolver.sonatypeRepo("releases"), Resolver.sonatypeRepo("snapshots"), Resolver.defaultLocal) // 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( "-ignorewarnings", "-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")
Now it’s time to run the project. The easy way is to use
activator. Execute the next command:
$ACTIVATOR_HOME/activator > 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:
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.
In the upcoming posts in this series, we’ll address other topics regarding Android development. Stay tuned for those!