Tiliman’s Weblog

February 10, 2010

Android Development with Scala

Filed under: Android, Mobile Software Development — Tags: , , , , , — tiliman @ 4:33 pm

I learned a few weeks back about Scala programming language. I never liked Java and thought to try Scala as I would learn functional language and be able to still use JVMs.
I also wanted to learn Android development as I have done previously Windows Mobile, Symbian and iPhone development. Using Scala with Android was my idea to hit 2 birds with one stone.

Android Decelopment using Eclipse and Scala language

Android Decelopment using Eclipse and Scala language

All the information that I could find about using Scala with Android was for Android 1.6/1.7 but not with Android 2.0.1. Today finally I got Scala-Android development working for me. I am using Eclipse for development on OSX.

The steps involved are documented in detail here which can be followed till Step 4 as after that I will explain the trick to use Scala . If you are an advanced programmer then you should be able to follow my brief guide below.

Increasing memory of Eclipse

Edit eclipse.ini file which can be found in OSX at /Applications/Eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
If you have not made changes to an app contents before in OSX then for your information right-click Show Package Contents displays all the internal directories in .app bundle.
Make sure that these parameters are there in config file as below


Goto android sdk tools directory at <ANDORID_SDK>/platforms/android-2.0.1/tools/dx.sh and edit it and make sure this param is there


It can be different on windows as windows uses batch file. You can find it as -Xmx<NNN>M where NNN is the memory size.

Note: mine was setup as 1024M so I kept it like that. 512M should work fine as well.

Install Android 2.x.x SDK

Download and install Android SDK and Tools from this link http://developer.android.com/sdk/eclipse-adt.html
I had to use HTTP instead of HTTPS for installing ADT plugin on Eclipse.

Install Scala

Download and install Scala plugin for Eclipse
You can put this URL directly in Install New Software feature of Eclipse
Scala – http://www.scala-lang.org/scala-eclipse-plugin
More details can be found at http://www.scala-lang.org/node/94

Download and install full Scala installer as well to get more control on Scala as well as get sbaz support.
http://www.scala-lang.org/downloads/distrib/files/scala-2.7.6.final-installer.jar or find whatever is the latest version at the time of reading.

Install scala-android library for development

Download and install scala-android.jar (library)

sbaz install scala-android

You can compile it yourself by visiting this link http://robey.lag.net/2009/01/19/scala-on-android.html

Create an Android project

Add Scala Nature by right clicking on project and then Scala->Add Scala Nature

Fix Project settings to use Scala

Scala Eclipse Settings

Scala Eclipse Settings

Goto Properties->Builders tab and make Scala Builder as 2nd last item in list.

Goto Java Build Path->Libraries and Remove Scala Library Version 2.x
In same Java Build Path->Libraries, Add External Jar and locate scala-android.jar in lib directory of Scala SDK.

Extending Scala class from Java

I always had issue compiling Android app written in Scala with class not being found as per manifest file. The issue was somewhere between Java expecting .java file as starting point and not being able to find it. So, I found a solution to leave original .java file as entry point and make a new file for Scala. Now the Java startup/main class extends Scala class which allows me to write rest of application in Scala.
My project was called ScalaTest and the ScalaTest.java was boilerplate code generated by android new project wizard.
The file looks like this after changes made for Scala

package android.ScalaTest;

import android.app.Activity;
import android.os.Bundle;

public class ScalaTest extends MainScala {}

The MainScala.scala looks like this

class MainScala extends Activity {
    override def onCreate(savedInstanceState: Bundle) {

First Scala Application

First Scala Application

Thats it. Scala can be used to do rest of Android Development as well as Debugging. Now I am off to learn Scala with Android.


Create a free website or blog at WordPress.com.