Scala on Android

The comprehensive documentation

  • Information reliability Fragile Might become outdated very soon
  • Last edit Fri Jul 10 19:44:32 2015 +0200
  • Edit on GitHub

Android Studio

The official IDE for Android works surprisingly well with Scala Build Tool (sbt). You can use it to edit code, run & deploy the app from within the IDE, access the sbt console and use the debugger. However, there is currently an issue with the Android SDK. All references to the SDK render as red code, even though the IDE manages to import them for you. This does only happen in Scala code, Java sources are not affected by this.

Download and install Android Studio from the official website.

  • Information reliability Moderate Might become outdated
  • Last edit Fri Jul 10 19:44:32 2015 +0200
  • Edit on GitHub

Plugins

After startup make sure to first install the Scala and sbt plugins as shown below.

On the start screen click Configure
Click on Plugins
Click on Browse repositories
Search for scala and install the Scala (custom language) plugin
Search for sbt and install the SBT (build) plugin

Now close the plugins configuration and restart the IDE to apply the changes.

  • Information reliability Moderate Might become outdated
  • Last edit Fri Jul 10 19:44:32 2015 +0200
  • Edit on GitHub

Open Project

Now, opening a sbt Scala on Android is dead simple.

On the start screen click Import project

Select the root directory of the project that you prepared in the Project setup section.

Select Import project from external model: SBT and click Next
Select download (sbt) sources and docs, set Android API as project SDK and click Finish

You are now able to edit code and access the sbt console from within Android Studio (View > Tool Windows > SBT Console).

  • Information reliability Fragile Might become outdated very soon
  • Last edit Fri Jul 10 19:44:32 2015 +0200
  • Edit on GitHub

Configuration

To run, package and debug your app from within the IDE, further adjustments are necessary.

Select Edit Configurations... on the main IDE screen
Click the + icon to add a configuration
Add an Android Application
Add a Before launch SBT task
Insert android:package

You should now be able to run and debug your code via the IDE controls. If the build progress gets stuck, open the sbt console (View > Tool Windows > SBT Console) to see detailed error messages.

  • Information reliability Moderate Might become outdated
  • Last edit Fri Jul 10 19:44:32 2015 +0200
  • Edit on GitHub

Manifest

Some manifest values (e.g. the version and targetSdkVersion) can be specified in the sbt configuration file. The Android SDK Plugin for SBT will then inject the values into the existing AndroidManifest.xml and save it at ./target/android-bin/AndroidManifest.xml. If you don't let Android Studio know about the patched manifest file, the IDE might annoy you with linting messages that some API calls are not available with a minSdkVersion of 0.

On the main IDE screen, click button Project Structure
Select AndroidManifest file
Navigate to and select ./target/android-bin/AndroidManifest.xml
  • Information reliability Fragile Might become outdated very soon
  • Last edit Sat Aug 29 13:51:29 2015 +0200
  • Edit on GitHub

Troubleshooting

IDE integration tends to break easily. If it doesn't work for you, have a look on the methods below to overcome common issues.

gen-idea

If the IDE import does not work out, you can let the gen-idea sbt plugin generate the configuration files instead.

gen-idea is a sbt task that generates configuration files for the IntelliJ IDEA. To install the gen-idea plugin create the file ~/.sbt/0.13/plugins/gen-idea.sbt (the filename is arbitrary) with the contents below.

resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin( "com.github.mpeltonen" % "sbt-idea" % "1.7.0-SNAPSHOT" )

This makes the gen-idea sbt command globally available.

Now, navigate your terminal to the project root directory and run sbt gen-idea. This will create IntelliJ IDEA 13 configuration files, but newer versions of IntelliJ IDEA are able to convert the configuration to their appropriate format.

You can now chose the Open option of the IntelliJ IDEA and select the root directory of your project.

Selection of IntelliJ IDEA open menu
Convert a project configuration of an older IntelliJ IDEA version to the current one

Converting the project files only needs to be done once at the initial setup. When the IDE finished loading you may delete the /projectFilesBackup folder.

Import the Android dependency explicitly

If your setup is basically working, but imports of the Android SDK are red, you can fix that by manually importing the android.jar. Go to File → Project Structure and change the SDK of each module (in the Dependencies tab) from Android to a simple Java SDK. Then, add the android.jar via + → JARs or directories and navigate to $ANDROID_HOME/platforms/android-x/android.jar.

This approach does unfortunately sacrifice the IDE source code lookup for Android sources.

Start all over

If everything fails, remove the .idea* directories from the project's root directory and start over by importing the sbt project.

Further reading

Comments