Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using Scala on Android worth it? Is there a lot of overhead? Problems? [closed]

Tags:

android

scala

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?

like image 618
egervari Avatar asked Oct 11 '10 13:10

egervari


3 Answers

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!

like image 117
Kevin Wright Avatar answered Oct 11 '22 14:10

Kevin Wright


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.

like image 41
om-nom-nom Avatar answered Oct 11 '22 12:10

om-nom-nom


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.

like image 33
pr1001 Avatar answered Oct 11 '22 13:10

pr1001