• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    Scala
  • Created over 11 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Directions on how to create a Scala based project in Intellij IDEA without having to resort to additional plugins or external building that cripples the nice features of the IDE.

Scala on Android without SBT

Want to use Scala on Android with Intellij without having to bring the entire sbt build system into your life and the pros/cons it provides?

Before you had to use something like this with sbt and build externally. It's a nice solution if you want to use SBT, but sometimes I just want to do some scripting in Scala without bringing along an entire build system.

Setup & Directions

Directions are below or click here to jump directly to them. Just added images and an Intellij project file. With any luck, the addition of the project file + images should help anyone experiencing issues.

Caveats

Any Android library that requires custom annotation processing may not work such as Android Annotations. Just mentioning it now if you find yourself spending way too much time with errors that make no sense. A detailed explanation about why it doesn't work can be found on Stack Overflow.

Examples

Aside from the minimal code showing that Scala works on Android, here's some more elaborate source code to a Scala Android app I built using a few popular libraries ActionBarSherlock, support.GridLayout and loaderex).

Guide and examples on using the Slick DB library on Android

An example using Scala's Pimp my Library pattern to get rid of some of the verbosity of Java Android for setting View.onClick listeners and getting view types from a resource ID.

A small example using Typesafe's Async/Await library and an example of how to convert a Java Anonymous function to a Scala lambda. Also requires adding the continuations library.

FAQ

What works?

Everything as far as I can tell (even debugging)

What doesn't work?

Nothing as far as I know so far.

Why use this over the sbt plugin?

A few reasons:

  • No additional tools or code needed (other than ProGuard) so less potential for bugs or issues with Intellij/Android updates.

  • Simpify your build process by doing what you normally do when building apps in Intellij.

  • You're used to using Java and would like to keep building apps mostly how you did beforehand.

  • Don't need a formal build system like sbt or just want to start writing code instead of also learning a useful, but somewhat complex build system.

What version of Scala did you test with?

2.11.6, the latest stable as of 2015-05-06

In 2.11.x, Scala's XML library, parser/combinators library, and continuations library have been decoupled from scala-library.jar and require being added in addition to scala-library.jar if needed. Those libraries can be found under the same directory as scala-library (/lib/ under your scala install directory). Proguard may also optional if you do not require the full library (but recommended to reduce size for market apps) for Scala 2.11.x due to the reduced size of scala-library.jar

How is debugging possible?

Proguard is set not to obfuscate so it doesn't remove the debugging symbols. That's really the key to my guide. Proguard can be tricky, but I've taken care of most of the common edge cases you might encounter with the included proguard file in this repository. If you encounter any I missed, just let me know. Things like actionbar sherlock and other libraries should work fine though.

Why does Proguard work?

In Scala 2.10.0, there are way too many methods in Scala for the dalvik compiler (dx) to handle if you simply add all the jars to the class path and compile. Proguard will strip out all the excess (otherwise, you'll get errors like Android Dex: [android-scala] trouble writing output: Too many methods: 66095; max is 65536.).

In Scala 2.11.x, the jar size for scala-library has been reduced considerably by decoupling the XML, parser and continuation libraries and may not require Proguard (though it's recommended if you want to reduce the size of the apk for market apps).

Does Google's Android Studio work, since it's based on Intellij?

Last I checked, I did not see a way to compile an Android app in Android Studio without Gradle, so my guide will not work. There exists a Android + Scala + Gradle plugin that would work for Android Studio, but I have never used it. You're probably better off using SBT though, if you want a build system for Scala on Android.

I have questions or want to add examples or documentation to this guide

Feel free to create an issue and/or send a pull request.

Building with Intellij IDEA 12+ or Android Studio

Intellij IDEA 14 Update

If you're coming from a project on Intellij IDEA 13, Intellij IDEA 14 will automatically update it for you and work without an issue (unless you were using continuations, you may have to re-add the library). If you're starting out with IDEA 14, there have been some changes (mentioned below). I will update the images in my guide to reflect that soon, but do not have time until later in November.

I recently tested (2014-11-07) Intellij IDEA 14 with my guide and it works fine. However some of the locations for configuration changes have moved since version 13. Scala specific settings are now located under File → Settings → Build, Execution, Deployment → Compiler → Scala Compiler (new location for enabling/adding the continuations library) while others remain under "Project Structure."

Certain steps, like adding a Scala facet are no longer needed (thanks JetBrains!) and you can add Scala directly into an existing Android project under Project Settings → Modules → Add (the + icon) → New Module. This will also guide you through setting up the Scala Compiler and libraries if you do not already done so. Proguard settings are under Facets → Android → Proguard Tab and still need to be configured as usual.

Scala Android projects also compile a bit faster with IDEA 14 (about 5-10% depending on the project size and libraries). Scala performance in the editor is also much improved.

Prerequisites

  1. Get Scala. If you don't have the Scala plugin for Intellij, you need that too of course. Just download the plugin from within the IDE under settings → plugins.

  2. Add a Scala facet to the project, and set up the compiler (I recommend using at least Scala 10.x). You may have to create a separate throwaway Scala module first if no Scala plugin has already been added globally. Note that scala compiler is now its own section and not under compiler as of IDEA 12.1.

  3. Set up the Scala facet for the Android project (add continuations if you wish). Alternatively, setting up a Scala project and then adding an Android Facet might be easier for some. It's possible to do this while creating a new project or for an exising project via File → Project Structure → Facets

module settings

  1. Add the following jars as libraries to your project via File → Project Structure → Libraries:

scala-actors.jar under /path-to-scala-install-dir/libs/ (use the Akka framework jars included in Scala instead on 2.11 since the Scala actor framework was depreciated)

scala-library.jar under /path-to-scala-install-dir/libs/

continuations.jar (no longer needed on Scala 2.11, but was required for concurrency code on 2.10). The jar is in your scala install directory under /path-to-scala-install-dir/misc/scala-devel/plugins.

  1. Make sure the libraries created above are set as a dependency for all modules you add a Scala facet from step 2. Module dependencies are set via File → Project Structure → Modules → Dependencies Tab.

module settings

module settings

If you create any additional modules not using Scala (like Java only ones), but wish to use code you wrote from a module with a Scala facet, you just have to add that Scala module as a dependency to the Java only module as you would normally in Intellij.

Adding Proguard to the Project

  1. Use the given proguard file in this project. It should work for most projects without modifications. If you encounter an issue when adding a library, let me know and I can add a proguard fix for it.

  2. Enable proguard for the building of the project via the following:

Intellij 12

Go to: File → project settings → modules → select module(s) → compiler tab → enable proguard

Intellij 13 / Android Studio

Go to: File → project settings → modules → select module(s) → compiler tab → check Run Proguard checkbox

module settings

Make sure both your project proguard file (proguard-project.txt) and the Android SDK provided one (found under /android-sdk-home/tools/proguard/proguard-android.txt) are both added to the list below the checkbox.

Compiling your Project

  1. After adding proguard, build the project like how you would a normal Java Android project. Proguard is a little slow, but it's required unless you preload the libraries onto the device (which requires root or using the previously mentioned sbt plugin). You should see no errors in the example activity (other than a highlight bug on * symbol for the test() method you can ignore). If anyone has time, feel free to file an issue about it with JetBrains, but it's not a major issue.

module settings

  1. Run the application

More Repositories

1

Java7-on-Android

Java 7 features backported to Android 2.2+
Java
87
star
2

SignalInfo

Gives you all the available information for the CMDA/GSM/LTE Signal on your device
Scala
42
star
3

yajl-json-android

Source, makefiles and precompiled binaries for the super fast/efficient C based yajl json library.
C
3
star
4

json-to-csv

Converts Json to CSV
Scala
3
star
5

ScalaSlickAndroid

Setting up and using Scala's Slick DB framework on Android for SQLite.
Scala
1
star
6

android_proprietary_files

How to fetch the Android proprietary files for building Android.
1
star
7

twitter-clone-python

Project I did a while ago for school using Flask + Redis + Torando to build a Twitter like clone
Python
1
star
8

wikiquote-parser

Pulls down quotes from wiki, filters out unverified/false attributed and formats them into xml/json
Python
1
star
9

Libgdx-Testing

Tests for the LibGDX game framework
Java
1
star
10

SignalInfoMono

SignalInfo for Android translated to Mono/C# (see: https://github.com/yareally/SignalInfo)
C#
1
star
11

standupbot

Agile Standup bot for Slack written in Python.
Python
1
star
12

swing-connect4

Once upon a time, I took a class where I had to write a game in Swing for my midterm. This repository is a result of that 16 hour nightmare.If you have a choice, do not try making a simple game in Swing. Use libgdx instead or better yet, avoid Java all together. Compared to making Solitaire once in Visual Studio with Windows Forms, this was far more painful and endured far longer. As a fellow classmate said so eloquently: Things that take less time than this test took: Training my dog to use the toilet. Waiting for George RR Martins next Game of Thrones book. Serving time for Murder. Explaining to my mother that not every clickable link on the internet is worth clicking. Teaching pigs to fly. Bullet wounds healing. That is all.
1
star
13

tech-talent-twitter

Social Media project for students
JavaScript
1
star