In 1996 Sun Microsystems Inc. — the innovative software and hardware company — announced the availability of the Java development environment, a non-commercial software product featuring both a new programming language and a platform-independent execution environment. The success of Java was boosted by the emergence of the World Wide Web and was definitely sealed with its adoption by the software industry as an alternative to the C/C++ mainstream language.
In 2007 Google Inc. — the ubiquitous Internet company — announced the availability of the Android development environment, a non-commercial software product supporting the well-established programming language Java and targeted to the emerging mobile device market. Indeed Google sees — as do its concurrents like Apple Inc. — in the increasing availability of wireless connections a profitable opportunity to strengthen and extend its offer of online services.
We were particularly attentive to those announcements — we were ourself involved in the development of compiler tools for Modula-2, Portal and, later, for Scala — and experimented with preliminary versions of both programming environments.
In the following we briefly expose our motivations for targeting Scala to the Android platform and summarize our experiences in developing Android applications written in Scala.
Good code examples are essential during the learning process of a new programming environment; besides helping the programmer to familiarize himself with language features and development tools they should demonstrate its ability to support the developer in delivering effective software solutions.
The development of the Scala language started as an academic project at EPFL and the availability of good Scala examples was rather poor at its early development stages. Several reasons can explain that situation:
Our main project goal is to apply our Scala expertise to the development of Android applications. In order to facilitate our immersion into the Android environment we choose to reuse publicly available Java code examples and plan to write our own Scala code in a later project phase.
We have experimented with Scala on the Android platform since 2007, starting with Android version 0.9 together with Scala version 2.6.x. As a member of the Scala project team we could act directly on the Scala tools to facilitate their integration with the Android platform.
In the following we resume the most relevant aspects of our experience developing Scala applications for the Android platform:
Scala-specific aspects. Here we distinguish between language translation, code refactoring and implementation issues.
Translating Java code to Scala is best achieved rewriting code in
small steps: move static Java members to Scala companion objects,
rewrite class declarations (e.g. constructors), rewrite type
annotations (e.g. arrays) and throws-clauses, rewrite control
statements (e.g. loop
-statements,
switch
-statements, break/continue
-statements),
remove type casts (or use asInstanceOf
), and so on.
Several formating changes are possible, e.g. to follow the Scala coding conventions: use two spaces indentations, remove line-ending semicolons, use infix notation for unary functions.
Code refactoring is a second-priority task and can be planed on the mid-term; it can be started using several (independent) code adaptations: substitute Java library classes by their Scala counterparts (e.g. collection classes), introduce default parameters in method declarations, introduce XML literals in string expressions manipulating XML-like code, and so on.
More complex transformations — in particular implementation redesign — typically require a more advanced knowledge both of the application logic and of the Scala language features. For instance the former Scala 1.x compiler is coded in Pizza — a superset of the Java language — and the current Scala 2.x compiler is written in Scala and benefits from an important implementation redesign.
We report here one implementation issue which is specific to the
Android platform (other issues are mostly implementation bugs): the
Scala compiler needs to generate special code for classes implementing
the android.os.Parcelable
interface of the Android API;
those classes must namely declare a static field called
"CREATOR
" and accessed by the Android runtime.
Android-specific aspects. We present several technical aspects of the Android platform which make it particularly well suited for developing Android applications in Scala:
The Android SDK is available on several platforms: development tools as well as Android devices are supported both on Unix (eg. Linux, MacOS X) and Microsoft Windows.
During this project we developed and tested our Android applications on Linux Ubuntu 8.04 and Microsoft Windows XP SP3.
build-scala.xml
which we regularly adapted
to the new software versions of the Android platform.
System requirements. Developing Android applications in Scala is a resource-intensive task. In particular, we mention here the following observations:
Developing Scala software requires more system resources than working with Java; that additional cost is however well accepted when putting Scala's expressivity and conciseness into the balance.
For this spare-time project we are using a 2.0GHz Pentium M laptop with 2GB memory for building and testing our Scala applications both on Linux Ubuntu 8.04 and Microsoft Windows XP SP3; working with a low-end configuration is definitely the least pleasant aspect of this project. Furthermore synchronizing project data with the SVN repository and sharing information is a painful task since we have no Internet access.
Developing Android applications in Scala is a very enjoying task. Most development efforts in this project have focused on the software build process and its deployment on the Android emulator. Translating Java source code to Scala was mostly straightforward.
We conclude this project summary with several personal remarks about the strengths and weaknesses of Scala and Android: