DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Setting Up Scala on Android

Setting Up Scala on Android

Getting started with Scala to build Android apps is still a bit complicated. Use this handy guide to get started.

Sergio Rodrigo Royo user avatar by
Sergio Rodrigo Royo
·
Oct. 19, 16 · Opinion
Like (5)
Save
Tweet
Share
4.26K Views

Join the DZone community and get the full member experience.

Join For Free

Scala can be used to build Android applications, as an alternative to Java or Kotlin. Unlike those options, setting up an Android project in Scala with SBT is not straightforward and can give us some headaches to get it right. To show how this can be done, we are going to create a new project template using the Android SDK Plugin for SBT.

Required Tools

In order to develop Android apps in Scala, you need a minimum set of tools: SBT and Android SDK.

Install SBT

You can install SBT on Mac OSX using Homebrew.

$ brew install sbt

To install SBT on other operating systems, you can follow the instructions on the official documentation.

Install the Android SDK

You can just download the latest version of the Android SDK from the Developer website and follow the installation instructions. Alternatively, you can install Android Studio, which comes with the Android SDK and emulators.

Set the ANDROID_HOME Environment Variable

On Mac OSX/Linux, you can just export the variable

$ export ANDROID_HOME=path_to_your_android_sdk

or add it to your bash_rc or bash_profile.

NOTE: On Mac OSX, if Android Studio is installed, the Android SDK is usually located at /Users/your_user_name/Library/Android/sdk/

Add SBT Plugin

The easiest way to install the Android SDK Plugin for SBT is to do it globally. For this, you'll need to create a file in the SBT plugins folder:

~/.sbt/0.13/plugins/android.sbt

and add the following line:

addSbtPlugin("org.scala-android" % "sbt-android" % "1.7.0")

Create the Android Project

Let's create a folder for our Android project:

$ mkdir my-project
$ cd my-project

Now, we are going to use the SBT plugin to create a template project. First, run SBT:

$ sbt

Then, use the plugin to create the project.

> gen-android <package_name> <project_name>

For example:

> gen-android com.codurance scala_on_android

The project structure created looks like this:

my-project/
|-- project/
|   |-- android.sbt
|   |-- build.properties
|-- src/
|   |-- androidTest/
|       |-- java/
|           |-- com/
|               |-- codurance/
|                   |-- Junit3MainActivityTest.java
|                   |-- Junit4MainActivityTest.java
|   |-- main/
|       |-- res/
|           // Android resorces folders
|       |-- scala/
|           |-- com/
|               |-- codurance/
|                   |-- MainActivity.scala
|       |-- AndroidManifest.xml
|-- build.sbt
|-- lint.xml

The SBT plugin creates a project structure with the minimum files needed to run an Android project, plus a setup for running instrumentation tests. Notice that the test classes generated are in Java, and the MainActivity is in Scala.

The most interesting file is build.sbt. I've added some comments to explain the purpose of each line.

// Version of the Scala runtime
scalaVersion := "2.11.8"

// Use the Android plugin
enablePlugins(AndroidApp)
// Add support for vector drawables
useSupportVectors

// Android version code (Same as versionCode on Gradle projects)
versionCode := Some(1)
// Android version name (Same as versionName on Gradle projects)
version := "0.1-SNAPSHOT"

// Instrumentation tests runner (Same as testInstrumentationRunner on Gradle projects)
instrumentTestRunner :=
  "android.support.test.runner.AndroidJUnitRunner"

// Android platform target (Same as targetSdkVersion on Gradle projects)
platformTarget := "android-24"

// Java compile options
javacOptions in Compile ++= "-source" :: "1.7" :: "-target" :: "1.7" :: Nil

// Libraries
libraryDependencies ++=
  "com.android.support" % "appcompat-v7" % "24.0.0" ::
  "com.android.support.test" % "runner" % "0.5" % "androidTest" ::
  "com.android.support.test.espresso" % "espresso-core" % "2.2.2" % "androidTest" ::
  Nil

We don't even need to use the SBT plugin to generate this template. If we prefer to craft our own minimum project, we could just create the project structure for SBT and Android manually, and add only the setup that we need.

Run the project

You will need to have a connected Android device or a running emulator.

Once this is done, the final step is to run the application from sbt:

> android:run

You can also run it from the terminal, instead of from SBT:

$ sbt android:run

More Options on build.sbt

There are other interesting options that can be included in the build.sbt file. A few of them are:

// Application name
name := "scala_on_android"
// Min Android SDK version supported
minSdkVersion := "15"
// Override 'android:run', to use just 'run' instead
run <<= run in Android

Integration With Android Studio

From this point, you could develop your Android apps in Scala using a text editor and sbt. But it would be good to be able to use the official IDE, which offers a lot of useful tools. It's also possible to use IntelliJ, but I won't go into this detail in this post.

Before we start, we'll need to install both the Scala and SBT plugins for Android Studio.

To import our project, open Android Studio and select Import project (Eclipse, ADT, Gradle, etc.).

Select Import project from external module, and SBT:

Import project

Select your Android SDK on the Project SDK option, and check Sources, Javadocs and Sources for SBT and plugins:

Import project

On Project Settings/Modules select the project module and click on +, and Add Android under the Frameworksection. On the Structure tab, replace all /.idea/modules paths with /src/main:

Project structure

Now that the project is set up, we can try to run it. Create a new Run/Debug Configuration. Select Android App and give it a name (e.g. app).

On the General tab, select the Module. We also need to configure how to run the app with SBT. On the Before launchsection, remove the default Gradle-aware Make task clicking on -:

Run/Debug configuration

Finally, click on +, create a new SBT task, and add android:run. Leave the Run in current module option checked:

Before launch SBT android run

From now, you can run the application in the same way as any app written in Java or Kotlin.

Conclusion

Kotlin still looks like a better fit for Android: Among other benefits, it has an easier setup, better IDE support and smaller runtime. However, the possibility of using Scala and SBT can allow developers who build their backends in Scala or want to use high-level Functional Programming features to build complex apps.

This article was first published on the Codurance blog.

Android (robot) Scala (programming language) Android SDK Software development kit Android Studio app application Text editor Functional programming

Published at DZone with permission of Sergio Rodrigo Royo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Guide To Successful DevOps in Web3
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • Top Five Tools for AI-based Test Automation
  • Type Variance in Java and Kotlin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: