Targeting the Android Platform
Disclaimer: work in progress.
For targeting the Android platform you'll need:
- Android SDK 0.9 or later.
- Scala version 2.7.2.RC1 (or newer)
and the Sbaz packagescala-android(to be installed usingsbaz). Eitherthe Android Plugin for EclipseorApache Ant(or both).
- 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.
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 filescala_android_tools.zipand install its contents (3 template files, a library file and the scriptscalaActivityCreator.py) at the root of your Android installation (Windows users may also need to installPython2.3 or newer on their system).Make sure the Sbaz packagescala-androidis present in your Scala installation directory (simply checked using the commandsbaz installed).Provided that theAndroid toolsare defined in yourPATHand the variableANDROID_HOMEis defined in your environment, you can create an Android project as follows (see also theactivityCreatortool descriptionand theAnt tool usageon 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 Android
ThescalaActivityCreator.pyscript extends the originalactivityCreatortoolwith the option--template(or shorter-t) which let you choose between the Java and the Scala source template (default is "scala").The generated Ant scriptbuild.xmldepends onscala-depend.jar, a tiny Scala library used together with thedependAnt task to reduce the application size by removing the unnecessary class files from the generated.apkfile.
