Saturday, July 14, 2012

Scala on Android: what you need to know (some of this applies to any large Android codebase)

Here's a collection of all of the interesting links talking about issues with Scala running on Android.  (And note that it's not specific to Scala; it's large codebases generally.)

Summary

Android tools limit you to 65535 methods referenced, which includes both methods defined and methods called.  If you hit this limit (and you will with anything past hello-world when you're using the Scala library), you need to run an external tool.

If you use Eclipse, try my AndroidProguardScala plugin.

If you use sbt, use the sbt Android plugin.

Useful links

http://code.google.com/p/android/issues/detail?id=7147 Issue 7147: dx fails on scala-library.jar and scala-compiler.jar
The Android bug.  Important quote:
It turns out that you have made the first report we've seen of anyone running into Dalvik's existing limit of 
65536 method references per dex file
https://issues.scala-lang.org/browse/SI-5278 Scala outputs redundant interface declarations in bytecode
This is the bug + fix that should solve LinnearAlloc issues.

http://code.google.com/p/android/issues/detail?id=22586 Issue 22586: Dexopt fails with "LinearAlloc exceeded" for deep interface hierarchies
Earlier Scala versions hit this, but the problem has been solved by SI-5278.

https://issues.scala-lang.org/browse/SI-4620 Android install of scala 2.9.0 built binary fails (worked with scala 2.8.1)
Here's the Scala issue covering the Scala patch that reduced the

https://github.com/scala/scala/commit/7a99c03da1d31ac5950eecb30f422f43c5e3d04e Don't generate redundant interfaces
Here's the Scala patch itself.  It includes this comment:
Drop redundant interfaces (ones which are implemented by some
other parent) from the immediate parents. This is important on
android because there is otherwise an interface explosion.
http://code.google.com/p/android/issues/detail?id=20814 dx crashes on projects with > 65,535 methods
Note that the title is misleading (it's not just number of methods).  From the bug:
As I understand it, the limit is the number of methods referenced, which includes both methods defined and methods called, which is why you might see this even if your app only contains 56,484 method definitions.
http://www.scala-lang.org/node/10929
A conversation about using Scala with Android

2 comments:

HRJ said...

Thanks for collecting this information together.

PS. Please add a +1 button somewhere, so that I can find this page later easily.

James Moore said...

There should be a google+ +1 button there (it shows up for me at least). Strangely, though, it took about five seconds to appear, and then bounced around the screen for a while.