never certain - a blog by James Brechtel
Building Android Apps with Scala - IntelliJ
February 18, 2011

note to the reader This is the third in a series of posts about getting started with Scala, Android and IntelliJ.  The content here assumes that you already have a working install of IntelliJ and the Android SDK plus a project created using simple-build-tool.  If you don't have these things then please check out Part 1 - Prerequisites and Part 2 - simple-build-tool)

IntelliJ Plugins

Before we get started we need to install some IntelliJ plugins.

After the plugins are installed we have to configure the Sbt plugin so that it can find the Sbt jar file. Go to File -> Settings -> Sbt to do this.

Creating the IntelliJ project file

Unfortunately, this part of the process requires a bit of hackery.  My intent is to modify the Android Sbt plugin to create projects that are more suitable from the outset and combine that the work done in the sbt-idea plugin to automate this entire process.  Until someone does that, these are the steps required.

Also, as I said in Part 2, nothing we do here will prevent us from building, packaging, deploying or testing our project using sbt from the command line.  You should always be able to do that regardless of the IDE you are using.

Now, let's fire up IntelliJ and create this project.

Great!  At this point, we’ve got some IntelliJ project files created which reference the generated Java source files for the project we created in Part 2.

Now we need to change a few things.  IntelliJ still doesn’t know about our Scala source files and the IntelliJ Android plugin doesn’t know what Sbt is doing.  Also, a meaningful ‘Run’ configuration from the IDE is nice.

Hacking up the IntelliJ project

Let’s get our source tree in order first.

Configure the IntelliJ Android plugin

The Android facet expects some things to be in different places

Also in the project settings make SURE that the Scala dependecy is set to to ‘Provided’ (NOT compile, test or runtime) so that if you ever let IntelliJ (instead of Sbt) build the project then it won't also try to generate Dalvik bytecode for the Scala library. The Scala library contains classes which have more functions in them than the Dalvik bytecode conversion tool can handle.

This issue is circumvented by the Sbt build process because Sbt uses ProGuard to remove unused classes and functions from the Scala library before converting it to Dalvik bytecode.

Now, let's show IntelliJ how to build our project

(Adding a Run configuration that uses Sbt)

You can now rebuild and run your project from IntelliJ by executing the DeployRun configuration.

You can setup other build configurations to execute additional Sbt targets.

Next time we’ll cover running your tests with the awesome ScalaTest framework from inside IntelliJ.

blog comments powered by Disqus