Scala Programming for Android

Substantial Android development in Scala

I'm programming my Android application project in Scala.

If you are interested, you may take a look at this:

  • http://bone.twbbs.org.tw/maidroid/MaidroidOmikuji
  • http://bone.twbbs.org.tw/maidroid/MaidroidReminder

Ya, it is a Chinese website, but you may just take a look at screenshots to get some idea about that Scala can do everything with Android SDK just like Java.

The source code of these two android application is hosted on GitHub: http://github.com/brianhsu/Maidroid

Currently I don't use any IDE, because Vim / SBT is far more convenient and lightweight then any IDE I ever used.

And if you are using SBT to build your Scala Android application, you don't need worry about those progruard stuff, just install the sbt android-plugin and setup your project.

You may read this blog entry to learn how to build your Android application with SBT.

BTW, I use my own handcraft sbt plugin called sbt-android in my project, instead of android-plugin metioned in that blog post.

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

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!

Performance of Scala for Android

To explain a little bit @Aneesh answer -- Yes, there is no extra work to interpret Scala bytecode, because it is exactly the same bits and pieces as Java bytecode.

Sample Image

Note that there is also a Java bytecode => Dalvik bytecode step when you run code in Android.

But using the same bricks one can build a bikeshed and the other guy can build a townhall. For example, due to the fact that language encourages immutability Scala generates a lot of short living objects. For mature JVMs like HotSpot it is not a big deal for about a decade. But for Dalvik it is a problem (prior to recent versions object pooling and tight reuse of already created objects was one of the most common performance tips even for Java).

Next, writing val isn't the same as writing final Foo bar = .... Internally, this code is represented as a field + getter (unless you prefix val with private [this] which will be translated to the usual final field). var is translated into field + getter + setter. Why is it important?

Old versions of Android (prior to 2.2) had no JIT at all, so this turns out to about 3x-7x penalty compared to the direct field access. And finally, while Google instructs you to avoid inner classes and prefer packages instead, Scala will create a lot of inner classes even if you don't write so. Consider this code:

object Foo extends App {
List(1,2,3,4)
.map(x => x * 2)
.filter(x => x % 3 == 0)
.foreach(print)
}

How many inner classes will be created? You could say none, but if you run scalac you will see:

Foo$$anonfun$1.class       // Map
Foo$$anonfun$2.class // Filter
Foo$$anonfun$3.class // Foreach
Foo$.class // Companion object class, because you've used `object`
Foo$delayedInit$body.class // Delayed init functionality that is used by App trait
Foo.class // Actual class

So there will be some penalty, especially if you write idiomatic Scala code with a lot of immutability and syntax sugar. The thing is that it highly depends on your deployment (do you target newer devices?) and actual code patterns (you always can go back to Java or at least write less idiomatic code in performance critical spots) and some of the problems mentioned there will be addressed by language itself (the last one) in the next versions.

Original picture source was Wikipedia.

See also Stack Overflow question Is using Scala on Android worth it? Is there a lot of overhead? Problems? for possible problems that you may encounter during development.

Pure functional programming on android

I doubt that you can find anything mature for writing Haskell-like code for Android. You do need to implement Java abstractions which are required by Android API (implement activity, etc.).

But if you really want to write for Android in a purely functional style you can try to implement your business logic in a pure functional language that compiles to JVM and call it from your Java classes. That approach would be much simpler than trying to implement it entirely in pure functional style.

As your language choice, you can try

  • Frege, it even has a library for android - froid

  • Eta lang, it is very new and probably nobody has tried to use it for Android yet

Android framework for Scala

You can have a look at Proguard for Scala and this documentation.

Creating Android apps without Java

At this point Scala is the one that is most mature..I wanted to try groovy myself but its not even out of alpha..

Plus Scala on android has docs..:)



Related Topics



Leave a reply



Submit