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.
The following software requirements apply to both the Linux and Microsoft Windows platforms (tested software versions are given in parenthesis):
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 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
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
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!
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
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:
src/
contains both Java and
Scala files (e.g. com/example/helloactivity/HelloActivity.scala
).
build.xml
Ant
script was
slightly modified to
also handle Scala source files (it overwrites the "-post-compile
"
target which depends on the "-post-compile-scala
" Ant target
imported from build-scala.xml
).
build-scala.xml
Ant script
defines the Ant tasks "compile-scala
" and
"-post-compile-scala
" (configured using either
"build.properties
" or "local.properties
").
build.properties
property file defines (at least) the
Ant properties sdk.dir
(e.g. /opt/android-sdk-linux_x86
),
scala.dir
(e.g. /opt/scala
) and
proguard.dir
(e.g. /opt/proguard
). For more
information about ProGuard, please see the
android-sdk/README.txt file.
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
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:
Copy the build-scala.xml
Ant script file from a sample
application, e.g.
~/tmp/android-sdk/TestActivity$ cp ../HelloActivity/build-scala.xml .
Add the following lines directly after "<setup />
" in the build.xml
Ant script file:
<setup /> <import file="build-scala.xml" /> <!-- Converts this project's .class files into .dex files --> <target name="-post-compile" depends="-post-compile-scala" />
Note: We intentionally deviate here from Google's advise to copy the content of the main node "
<project>
" frommain_rules.xml
(previously namedandroid_rules.xml
) and paste it in ourbuild.xml
; we indeed consider the "copy & paste" operation as an ad-hoc solution.
~/tmp/android-sdk/TestActivity$ mv \
src/com/example/android/test/TestActivity.java \
src/com/example/android/test/TestActivity.scala
You can now build respectively install your own application:
~/tmp/android-sdk/TestActivity$ ant debug ~/tmp/android-sdk/TestActivity$ ant install
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]
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/
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]
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]
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]
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.
Use the Ant target uninstall
to uninstall the application
from a running emulator or device. [↑ Up]
~tmp/android-sdk/HelloActivity$ ant uninstall
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: }