In this small post I will try to tell you where to start developing Scala applications for Android using SBT and IntelliJ Idea, as well as the pros and cons of this approach. Everything will be quite simple, you just have to be patient. The whole process is described for unix like systems, it was not checked on Windows, but if you really want to, you can try to do the same through cygwin. So…
Existing Solutions
With a cursory googling, you can stumble upon these three (
one ,
two ,
three ) wonderful articles that gradually describe a long and thorny path to enlightenment, namely how to raise the SBT project for Android in Idea. This method is outdated, because the article describes the process for SBT 0.7.4, as well as many unnecessary steps that only repel further study of the material. Despite this, I recommend reading them to get a basic idea of ​​what you are dealing with.
Many people know that under Eclipse there is also a plugin for development on Scala, but it is in perpetual beta and completely useless due to the constant brakes and scarcity of functionality.
')
Instruments
- ItelliJ Idea community edition with Android support is the most Scala IDE friendly to date (there is still ensime, but this is from another opera).
- JDK 6 for linux / mac os. The path to the JDK must be registered in the global variable JAVA_HOME (available in ~ / .bashrc).
- Android SDK for linux / mac os. The path to the SDK must also be set through the global variable ANDROID_SDK_HOME, but, attention , for the system to be visible, therefore, you need to prescribe the variable strictly in / etc / environment . A reboot may be required.
- Simple Build Tool (SBT) 0.10.1 - a tool for generating and building Scala projects. Recently, the project changed its name to XSBT and moved to github, which is very convenient.
- Android plugin for SBT - for review, because this plugin is installed automatically.
- giter8 (g8) is a utility for generating projects from templates published on github.
- My g8 template for generating Idea projects.
- Configured Android Emulator or directly connected Android smartphone with USB debugging enabled.
SBT installation
You must first download the latest version of
sbt-launch.jar and put it in the ~ / bin folder, then create an empty file named sbt with the following contents
java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"
and also put it in the ~ / bin folder. Next, you need to make the sbt file executable and add the ~ / bin folder to the $ PATH variable in order to access its executable files without explicitly specifying the path:
$ chmod u+x ~/bin/sbt $ echo 'PATH=$PATH:~/bin' >> ~/.bashrc
After that, restart the terminal so that it rereads the ~ / .bashrc file.
Principle of operation
To create a project using giter8 (g8). The principle of its operation is very simple - it downloads a previously prepared project template from github and substitutes the properties specified in the default.properties file instead of the specified variables. This is true for both the contents of the files and their names. For example, in this code
class $main_activity$ extends Activity { ... }
$ main_activity $ will be replaced with the corresponding specified property. All property values ​​are set interactively via g8. Based on this, a
template was created that includes standard for Idea project files and modules, in which at the stage of project generation, specific properties are substituted for it.
Project generation
First install giter8. The simplest solution is to run my little script that will do it by itself:
curl https://raw.github.com/mbektimirov/android-app-idea.g8/master/install.sh | sh
If everything goes well, then g8 will be installed in the ~ / bin folder. For possible problems, see the end of the article.
The next step is actually creating a project from a template. There are no difficulties, during the installation process g8 will request properties. If the default values ​​suit you (they are indicated in square brackets), then they can be omitted.
mbektimirov@mbektimirov-pc:~$ g8 mbektimirov/android-app-idea Template for Android apps in Scala package [my.android.project]: ru.my.first.android.scala.project name [My Android Project]: my super project main_activity [MainActivity]: MyActivity api_level [10]: 8 project_scala_version [2.9.1]: Applied mbektimirov/android-app-idea.g8 in my-super-project
After that, you can see the generated project in the folder my-super-project, which can already be deployed to the device. This can be done using the
sbt start-device command.
IDE Setup
After you have downloaded and installed Idea, you need to add some plugins to it, namely to support Scala and SBT, so to speak, without departing from the cash register, so that you do not have to switch to the console each time and build / deploy the project from there. Plugins are available from the Idea itself in the plugin manager:

After installing the plugins, restart the IDE, then in the settings go to the SBT section and specify the path to sbt-launch.jar:

After these actions, you can upload our generated project to the IDE through “File -> Open project ...”.
Almost everything is ready, it remains to configure the configuration to run the project using SBT. To do this, select the item “Edit configurations” in the “Run” menu. In the window that appears, you need to create a new configuration, in our case it will be an Android Application, and call it StartDevice. In the settings that appear, select the “android-module” module, uncheck the “make” checkbox and check “Run SBT Action” opposite. In the SBT Action, in turn, add the “start-device” task. The screenshot below explains all the steps above:

Now the project can be launched by a green button or hot key. All logs will be written to the SBT console, which is part of the SBT plugin for Idea:

Known Issues
- When installing g8, he can swear that he could not download Apache httpclient. To do this, clean the cache in the folders ~ / .ivy2 / cache / org.apache.httpcomponents and ~ / .m2 / cache / org / apache / httpcomponents.
- The size of the standard Hello world apk file is 680 Kb. Most likely this problem is related to the incorrectly working proguard, which should extract the unused Scala libraries and classes from the apk file. In the near future I will try to write to the author of the plugin Android for SBT to find out ways to solve this problem.
- The Android development plug-in for Idea is not supported by Google, so it lacks most of the features that Eclipse provides.
What to read