Writing your Android project in Scala is becoming smoother and smoother. This is a short guide that will help you get started.
- (+) Concise code
- (+) Fantastic concurrency support (futures/promises, scala-async, actors, ...)
- (+) Advanced DSLs (like Macroid ;) )
- (−) Build time (due to ProGuard + dex taking up to 1 minute on big projects)
If the instructions below are too dry for your taste, check out these tutorials:
The SDK can be downloaded from the Android website.
You will also need to configure ANDROID_SDK_HOME
environment variable to point to the installation.
To use the bundled libraries, such as the support library, make sure you install the following items in the SDK manager:
The most important part of the equation is the build system (in mainland Android you are likely to use one anyway). After configuring the build you will be able to compile and run the project from the command line and generate IDE project files automatically.
Currently there are three options:
-
Recommended. Use sbt, Scala’s de-facto standard build system. You’ll have to install it from its website. To compile and run Android projects you’ll need the Android plugin. Follow the plugin’s readme page to set things up.
-
Use Gradle and Android+Scala plugin, which — according to its page — is not stable yet.
-
Not recommended. Just use an IDE without the build system. For Eclipse you’ll need the ADT plugin, Scala IDE and AndroidProguardScala plugin. For Intellij IDEA or Android Studio, which is the same, see this guide.
The recommended IDE is Intellij IDEA or Android Studio, which is the same. You will need to have the Scala and sbt plugins installed. Below I am assuming that you are using the first building option, i.e. sbt.
Once you have the build running from the command line, follow the instructions for the sbt plugin to create IDE project files.
Inside IDEA, you can use the sbt console to run the project. Alternatively, the “run” button could
be reconfigured for that purpose: go to Run → Edit Configurations →
(select a configuration) → Before launch. You need to configure it to look like this:
Make sure that the paths to
AndroidManifest.xml
and the APK
are configured properly. Go to Project settings → Modules → (select main module) → Android → Structure/Packaging.
- Manifest file should be
/path-to-project/src/main/AndroidManifest.xml
: - APK path should be
path-to-project/target/android-bin/build_integration/{module name}-BUILD-INTEGRATION.apk
:
It is possible to preload Scala standard library on the emulator to reduce build times. Here is a tool to do that.
- When in doubt, don’t hesitate to use the Scala-on-Android mailing list.
- See the talks section, as I also cover the matter in those talks.