28

I was thinking of building an app on Android with Scala instead of the regular Java (or the equivalent I guess). Is it worth it?

Any problems and unnecessary headaches?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
egervari
  • 22,372
  • 32
  • 121
  • 175

3 Answers3

26

Working with Scala should be mostly painless, as the dex compiler just works with bytecode - which is exactly what Scala produces.

Your biggest problem then is the dependency on scala-library, as dex expects everything to be in a single Jar. This is best handled with Proguard (which will also remove unused code and give you a smaller executable, ideal for mobile)


Current best practice is to use SBT with the Android plugin; It'll take care of everything for you: http://github.com/jberkel/android-plugin

If you must use Eclipse and the plugin supplied by Google, then you're going to have a non-standard directory structure. I also wrote an article on how to deal with this: http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android

But be warned... it takes a lot more effort that way!

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • 3
    The main problem is those library use reflection (something like myClass.getMethod()), because proguard can not detect them, you need declare to keep those class/function manually. And some library just hard to make it work with Scala/Proguard/Android, ex. gdata-java-client. Or you need some tweak to keep cenrtain files after proguard process the JAR, eg. ical4j. Otherwise, libraries usually could work with this combnation very easily. – Brian Hsu Oct 11 '10 at 15:11
  • This is true! Be wary of reflection... – Kevin Wright Oct 11 '10 at 15:46
15

There is a talk by guys from bu.mp on Scala Days 2011.

Disclaimer: While they talk a lot about issues they've faced, the overall experience was definitely positive. Moreover, this were the problems of one particular team and they are minor ones.

Summary:

  • Memory usage. Closures eat memory. Scala has closures all over the place. While it is not a relative big problem (memory overheads) on your table PC, it should be considered when you develop for mobile phone. You may ask: how they solved it? They had a hunt for a tight loops and then optimized them (rewritten in closureless scala/java). (26th minute) I think Java 8 with native closures support will ease this problem Next scala release will target java 8 platform with a new backend and aim for much better performance. Discussion on roadmap is here.
  • Slower compiler (as Michal Galpin said they have 15 seconds for Java code compilation, about a minute for Scala and about an order a magnitude more for Proguard, but note that it depends on your code base and machines, but I suspect that ratio will be the same). It especially annoying when you making UI, like moving pixels, buttons and so on, and want to quickly see the result. (7th minute)
  • There were some issues with inner classes which are quite common for Android (look at 23:00 for details).
  • There were rare bugs in scalac (scala compiler) in scala-java-collections interop. The problems were solved by writing this things explicitly (I suspect that is not a lot of boilerplate at all).

While he talks about IDEs too, I don't think that it is a problem (look this post, about Scala environment). Guys that do Scala IDE and IntelliJ do their work well and IDE support is getting better and better.

For unit testing one could use RoboSpecs (allows you to run unit tests inside JVM, not inside emulator), which is faster.

One of the biggest problems I've stumbled upon is the limitations of the dex format (no more than 65536 method references per dex file). Scala library simply doesn't fit into limits, so you have to workaround that by:

  • using Proguard that will trim the fat from scala lib. But it is really slow on my laptop, and is just wasting time in the development stage (but is much much helpfull when you are releasing your app to the Google Play)
  • using predexed library. The bad thing is that this project is a little abandoned and contains only 2.9.0-1 version.
  • preinstalling scala on your phone

Some guys claiming that there are problems with IDEs, but things getting changed fast: the current version of the IntelliJ IDEA 12 not only has a good support of Scala as a plugin, but also contains UI Designer for Android. Moreover, you can use Eclipse which has a support from both Android (by Google) and Scala (by Typesafe) sides. Android Studio (the new IDE based on IDEA which was announced on Google IO '13 and is developed by Google) support IDEA plugins thus Scala.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
5

We discussed this at Scala Lift Off London last Friday and the consensus seemed to be that it generally works fine as long as you avoid Actors. Also, the sbt-android-plugin was highly recommended. Nathan Hamblen's blog has many posts on Android and the ones also tagged Scala have a lot of gotchas worth looking out for.

pr1001
  • 21,727
  • 17
  • 79
  • 125
  • http://code.technically.us/post/824974287/rewiring-android-for-type-safe-layout-resources is a cool (if minor) advantage to programming for Android in Scala instead of Java. – Ken Bloom Oct 11 '10 at 18:29