Targeting the Android Platform

Android

Disclaimer: work in progress.

 

For targeting the Android platform you'll need:

Create a project with Eclipse
  • Follow these instructions to install the Android Eclipse plugin (if you haven't already).
  • Make sure you have installed the Scala Eclipse plugin 2.7.2.RC1 or later.
  • Create an Android and a Scala project as usual. Add the Scala project and the corresponding scala-library.jar to the Android project's build path (Project → Properties → Java Build Path). You'll find the library jar in a jar in the plugin folder of your eclipse installation. In later version of the Android plugin, it may suffice to export the scala-library.jar from the Scala project. All class files from both projects are now compiled into a single Android binary. Java/Scala interoperability should work as usual.
  • Note that you have to add your Scala project or a jar to the build path of the Android project. Class folders, classpath variables, or user libraries don't work.
  • Caveat: a Scala class can't be the main activity for now.
Create a project without Eclipse (currently not supported)

 

The following steps detail the procedure for building a Scala application from the command line and running it on the Android emulator.

  • Download the archive file scala_android_tools.zip and install its contents (3 template files, a library file and the script scalaActivityCreator.py) at the root of your Android installation (Windows users may also need to install Python 2.3 or newer on their system).
  • Make sure the Sbaz package scala-android is present in your Scala installation directory (simply checked using the commandsbaz installed).
  • Provided that the Android tools are defined in your PATH and the variable ANDROID_HOME is defined in your environment, you can create an Android project as follows (see also the activityCreator tool description and the Ant tool usage on the official Android website):
    > emulator & (or 'start emulator' on Windows)
    > mkdir HelloAndroid && cd HelloAndroid
    > scalaActivityCreator.py examples.hello.HelloAndroid
    > ant
    > adb install bin/HelloAndroid.apk
     


    Hello AndroidHello Android


    The scalaActivityCreator.py script extends the original activityCreator tool with the option --template (or shorter -t) which let you choose between the Java and the Scala source template (default is "scala").

    The generated Ant script build.xml depends on scala-depend.jar, a tiny Scala library used together with the depend Ant task to reduce the application size by removing the unnecessary class files from the generated .apk file.

 

Copyright © 2008 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland