-
First we need to create an
sbtproject.Sbtstands forSimple Build Tool, and is the go to build tool when writing scala projects. It can handle both Java and Scala source files mixed together very easily. You can add bothMavenandIvydependencies to your project very easily. A very neat thing aboutsbtis that it uses Scala as it's "configuration" language, so you don't need to bother with bloated XML or {insert current flavor of the month markup language here} files, instead you can do everything in Scala.Sbttakes some learning and getting used to, I don't want to go into much detail about that, just take a look athproject\AndroidTest.scala. The config file always goes into theprojectfolder. We can also see abuild.proeprtiesfile here. With this we can pass some config tosbtbut for now we only need to set what version to use. -
Let's add the android
sbtplugin which will take care of most of the heavy lifting for us when compiling to Android (after proper and painful configuration that is). To add plugins we need to create aplugins.sbtfile in theprojectfolder and add oursbtplugins there. (See the file in the folder for the proepr syntax.) You could add the plugin as a global one by putting it in~/.sbt/0.13/pluginsbut we rarely want this particular one to be added to every project so I'd advise against doing that. -
Next step is installing Android ADT. This is a not a very complicated task with a good help over at the ADT's official site, but everyone has to suffer through it at least once if they want to develop for Android. I would not like to go into much detail regarding this point. (The
sbtplugin will warn us either way if something's set up incorrectly). -
After we got our empty project laid out we need to make it an android project. Now for this you need to have
androidand the ADT in your path. If you have that you can issue anandroid create projectcommand like so:android create project --path android-test -n AndroidTest -a Main -k hu.inf.elte.androidtest -t android-19
Path is the directory you want to be your android project,
-nis the name of the project,-ais the name if the main activity,-kis the package name and-tis the preferred android version. (API level 19 is 4.4.2). For more info on the command check its help. -
Now we have to set up our
android-sdk-pluginproject properly. We can do that by making our config object extendandroid.AutoBuildbut we will do some fine tuning with our config. See this inproject/AndroidProject.scala. -
Finally we have our project set up so now we can start hacking.
Scala is really not a language we want to use only a text editor for, so we will set up IntelliJ IDEA to work on our project. You can go grab the community edition on their website or if you are a student you can get a non-commercial license for the ultimate edition. We will also need the Scala and SBT plugins. After this we can import (use auto import) our project as an SBT project. File -> Import Project... -> select project root folder-> OK -> Import project from external model -> SBT Project -> Check "Use auto-import" & for Project SDK, select an Android API platform -> Finish. Choose to configure the android project when IDEA asks.
For compiling I recommend running a separate sbt process with sbt ~compile. This will automatically recompile the
Scala source every time the code changes. This is somewhat faster than invoking sbt compile manually every time because
you don't have to restart the JVM every time and sbt even keeps scalac open so you don't have to spin that up either.
The best way for actually running your project is obviously to have a physical device and run on that. This will be much faster than any emulator, but obviously you can use the emulator that ships with ADT or Genymotion for better performance.
Just invoke the android:run command and see your app show up on the screen of your device.
There is a very comprehensive Scala library for Android which gives us very nice abstractions over what the Android platform provides in Java. Obviously we don't have to use any library at all as Scala has superb Java interoperation, but it's much nicer to write idiomatic Scala code than one with a lot of interop. Scalod gives us many implicit conversions for ease of use, a bunch of traits that can come in handy when dealing with activities and a lot of typesafe extensions for what the Android platform provides (we need a lot less casting than we would in Java).