Targeting Scala to the Android platform

by Stephane Micheloud, May 2010

[Home]
[Back]

In this article we briefly present our experience with the development of Scala applications targeted to the Android platform (1.6 or newer) from Google Inc.

The author has experimented with Scala on the Android platform since 2007 and has previously published build instructions on the Scala Project website for developing applications targeted to the Android 0.9 platform.

Prerequisites

The following software requirements apply to both the Linux and Microsoft Windows platforms (tested software versions are given in parenthesis):

See also footnote 1.

Note: We assume in this article that the respective tools are accessible directly from the command prompt; e.g. in our Unix environment we have:

~$ which ant android emulator scalac
/usr/bin/ant
/opt/android-sdk-linux_x86/tools/android
/opt/android-sdk-linux_x86/tools/emulator
/opt/scala/bin/scalac

The following two installation steps are usually performed only once (excepted if you want respectively need to update your installation).

Download the Scala examples

Download and uncompress the file "android-sdk.zip" to some local directory (e.g. ~/tmp/) on your machine; the root directory "android-sdk" contains sample projects adapted from the Android installation (e.g. $ANDROID_SDK_ROOT/platforms/android-<X>/samples/ where "<X>" corresponds to the chosen API level).

~$ cd ~/tmp
~/tmp$ unzip android-sdk.zip
~/tmp$ ls android-sdk
ApiDemos          CubeLiveWallpaper  NotePad          README.txt
BackupRestore     FileBrowser        JetBoy           SearchableDictionary
bin               GestureBuilder     LunarLander      Snake
build.properties  HelloViews         MapsDemo         TicTacToeMain
build.xml         HelloActivity      MarketLicensing  TicTacToeLib
configs           Home               MultiResolution  Wikitionary
ContactManager    INSTALL.txt        PhoneDialer
See also footnote 2.

Create a virtual device

Android Virtual Devices (AVDs) describe emulator configurations and let you better model an actual device.

Android 4.0 provides a unified UI for phones and tablets; available targets are:

Available targets for Android tablets are:

Available targets for Android smartphones are (API levels 6, 5, 2 and 1 are obsolete) :

You can create an AVD with the command "android create avd", e.g.

~/tmp$ android create avd --target 8 --name API_8
See also footnote 3 and footnote 4.

Building Applications

We now describe the build process of two Scala applications using an existing Scala project and a freshly created one. We first make sure an Android emulator is running as we definitely want to install and run the generated application packages!

Start the emulator (if not yet running)

You can start the Android emulator with the command "emulator [options] -avd name" in a separate shell console:

~$ emulator -no-boot-anim -no-skin -avd API_8
~$ adb logcat
See also footnote 5.

Build and run a sample application

Let us now look at the directory contents of the "HelloActivity" project directory before building the sample application.

~/tmp/android-sdk$ cd HelloActivity
~/tmp/android-sdk/HelloActivity$ ls
AndroidManifest.xml  configs             proguard.cfg           tests
build.properties     default.properties  proguard-template.cfg
build-scala.xml      libs                src
build.xml            NOTICE              res

We made the following changes to the project settings of the code samples available from Google's Android SDK:

See also footnote 6.

You can build respectively install (in debug mode) the sample application with the following two commands:

~/tmp/android-sdk/HelloActivity$ ant debug
~/tmp/android-sdk/HelloActivity$ ant install

Similarly you can execute the following commands in release mode:

~/tmp/android-sdk/HelloActivity$ ant release
~/tmp/android-sdk/HelloActivity$ ant install-release
See also footnote 7.

Build and run your own application

You can create an Android project from scratch with the command "android create project", e.g

~/tmp/android-sdk$ android create project \
  --package com.example.android.testactivity \
  --activity TestActivity --target 8 --path TestActivity
~/tmp/android-sdk$ cd TestActivity
~/tmp/android-sdk$ ls
AndroidManifest.xml  default.properties  res
bin                  gen                 src
build.properties     libs                tests
build.xml            local.properties

In order to integrate Scala code into your project you have to modify your project settings as described previously in paragraph "Build and run a sample application"; the changes are the following:

You can now build respectively install your own application:

~/tmp/android-sdk/TestActivity$ ant debug
~/tmp/android-sdk/TestActivity$ ant install

About the Author

Stephane's Picture
Stéphane Micheloud is a senior software engineer. He holds a Ph.D in computer science from EPFL and a M.Sc in computer science from ETHZ. At EPFL he worked on distributed programming and advanced compiler techniques and participated for over six years to the Scala project. Previously he was professor in computer science at HES-SO // Valais in Sierre, Switzerland.
[Top]

Other Articles

[Top]

Notes

  1. On Linux the version 1.6_r1 (September 2009) of the Android SDK requires version 2.4 of GNU C library (GLIBC_2.4). [↑ Up]

  2. Alternatively you may fetch the above examples directly from the Scala SVN repository. [↑ Up]

    ~/tmp$ svn co \
      http://lampsvn.epfl.ch/svn-repos/scala/android-examples/trunk/android-sdk/
  3. Per default AVD images are stored in the user directory $HOME/.android/avd/ (Unix) respectively in %USERPROFILE%\.android\avd\ (Windows). If variable $ANDROID_SDK_HOME is defined in your environment AVD images are taken from the directory $ANDROID_SDK_HOME/.android/avd/. [↑ Up]

  4. Hardware emulation options (e.g. memory size, camera support, etc.) may be specified when creating an AVD (see $ANDROID_SDK_ROOT/docs/guide/developing/tools/avd.html). [↑ Up]

  5. You can use emulator startup options (e.g. option -skin HVGA-L for landscape) and console commands to control the behaviors and characteristics of the emulated environment itself (see $ANDROID_SDK_ROOT/docs/guide/developing/tools/emulator.html for more details). [↑ Up]

  6. If no target is specified the command ant simply displays the list of available targets (same as ant help). That corresponds to the default behavior of a freshly generated Android project (see paragraph "Build and run your own application" above). [↑ Up]

    ~/tmp/android-sdk/HelloActivity$ ant help
    Buildfile: build.xml
        [setup] Project Target: Google APIs
        [setup] Vendor: Google Inc.
        [setup] Platform Version: 1.6
        [setup] API level: 4
    
    help:
         [echo] Android Ant Build. Available targets:
         [echo]    help:      Display this help.
         [echo]    debug:     Builds the application and sign it with a debug key.
         [echo]    install:   (Re-)Install the debug package onto a running emulator or device.
         [echo]    uninstall: Uninstall the application from a running emulator or device.
         [echo]    clean:     Clean up build files from project.
  7. Use the Ant target uninstall to uninstall the application from a running emulator or device. [↑ Up]

    ~tmp/android-sdk/HelloActivity$ ant uninstall
  8. The to Scala translated source file "TestActivity.scala" looks as follows: [↑ Up]

     1: package com.example.android.testactivity
     2:
     3: import android.app.Activity
     4: import android.os.Bundle
     5: import android.widget.TextView
     6:
     7: class TestActivity extends Activity {
     8:   /** Called when the activity is first created. */
     9:   override def onCreate(savedInstanceState: Bundle) {
    10:     super.onCreate(savedInstanceState)
    11:     val tv = new TextView(this)
    12:     tv setText "Test"
    13:     setContentView(tv)
    14:   }
    15: }