How to: Android development in Scala
I recently decided to write an Android app, and I decided to do it in Scala. Android uses its own VM (Dalvik), but Dalvik bytecode is generated from Java bytecode, so it should be possible to write Android apps using any language that compiles to Java bytecode. However, the tooling that Google has provided is geared pretty much to Java, so you may have to jump through a couple of hoops to get things working for other languages. In this article, I describe the toolchain that I use for developing Android apps in Scala. I have only 3 disclaimers.
- Disclaimer 1: this process works at the time of writing, which is October 16th, 2011. By the time you read this, things may have changed. For the better, I would hope, but still, this article might be out of date.
- Disclaimer 2: I was unable to get Eclipse to work, unfortunately. So continue only if the command-line doesn’t scare you.
- Disclaimer 3: I use Ubuntu 11.10, so you might have to adjust for your OS if you use another. I’ll assume you already know how to do things like add folders to the path.
Ok, those were the disclaimers. Let’s go:
- In your home folder, create the folders
bin. In the
devfolder, we’ll be putting all the development tools. Choose a different name or location if you prefer. The
binfolder will contain some small executables. One of the steps will automatically generate this folder, so you might as well use it for the rest as well.
~/binon your path.
- First, to install the Android SDK, follow these instructions. You can skip the steps related to Eclipse.
- You can download the SDK to the
~/devfolder you just created. The unzipper will automatically extract the contents to a subfolder called
~/dev/android-sdk-linux_x86/tools/androidto download a platform (Android 2.2 or 2.3, etc.) Don’t download all platforms at once, or be prepared to wait a long time.
- Also, create a Virtual Device. You will need it later to deploy. I called
~/dev/android-sdk-linux_x86/platform-toolsto your path.
- If you run 64-bit Ubuntu (as I am), run
sudo apt-get install ia32-libs. I know, it says to do that at the bottom of the Android installation instructions, but who reads that shit? I certainly didn’t… which is why I mention it here.)
- Make sure you have a recent JDK installed on your system.
- Install Scala. I’m using Scala 2.9 at the moment. Your system may have a package for it; if not, you can download it here.
- Next, we need SBT. Every self-respecting language needs its own build tool nowadays, and SBT is Scala’s. To install, follow these instructions. I’m using version 0.11.
- I did one thing differently, which you may want to do as well:
- I downloaded
sbt-launch.jarto a new folder called
~/dev/sbt-0.11instead of in
- This meant that the shell script needs to be adjusted, too:
java -Xmx512M -jar ~/dev/sbt-0.11/sbt-launch.jar "$@"
- I downloaded
Scala + Android
- Next, we need the SBT Android plugin. This plugin adds the necessary Android capabilities to SBT. The plugin’s GitHub page contains installation instructions.
giter8thing looks a bit weird, but I recommend going along with it anyway: it will keep things easier. BTW, this is the step that creates the
~/binfolder which I mentioned earlier. Do create the project using the
- At the time of writing, it’s still necessary to
git cloneandroid-plugin’s repository and
sbt publish-localit. I recommend cloning it into our trusty
~/devfolder, to keep things clean.
- Once you run SBT the first time, it will start downloading and compiling teh internets, so this might take a while.
While SBT is running, you can start the emulator. It, too, needs some time to start up. Assuming you called your Virtual Device avd, like I did, you can use the following command-line:
emulator -avd avd &
Add the following folders to your
.gitignorefile (or equivalent thing for your favourite SCM tool):
lib_managed/ src_managed/ project/boot/ project/build/target/ project/plugins/target/ project/plugins/lib_managed/ project/plugins/src_managed/
- I like to keep SBT running in a terminal window, so I can quickly type
instructions into the interactive prompt:
compilecompiles the code
testruns the unit tests
android:package-debugcreates an Android APK file with debug information.
android:package-releasecreates an Android APK without debug information.
android:start-emulatordeploys the APK file to the emulator.
android:start-devicedeploys the APK to your device, if you have it connected.
- Note that
android:start-devicedon’t automatically compile and package your code. It happened to me a couple of times that I made a change and deployed it without packaging it first, and I would wonder why I couldn’t see the changes…
- If you run
android:start-emulatorand SBT says “
error: device offline”, even though the emulator is running, just wait a few seconds and try again. It needs some time to warm up or something.
That’s it! Enjoy! And if you find out how to do all this in Eclipse, please let me know in the comments.