Scala 2.13 on Android

We currently are extensively using Scala to build our app and had to go through some tricky situations to make Scala 2.13 (or Scala 2.12.8 and higher) on android to happen, addressing the following issues

  • Currently Android’s support on JVM 8 and above isn’t perfect, missing ClassValue.java from its package. We had to write own java.lang.ClassValue.java that mimics the behavior to get through it

  • Access to java.lang.invoke.VarHandle.releaseFence() is currently blacklisted on Android, which frequently used by immutable collection classes. We had to modify scala.runtime.Statics.java to do nothing to keep it away from trying to access the method

  • Since currently android only supports MethodHandle.invoke() for API version 26 or higher, we had to set minSdk version to 26

We managed to compile and run our app on Android with Scala 2.13 by troubleshooting the issues above and pretty satisfied with the result.

Will there be any plans to make it easier to deploy Scala on Android in the future? We’d love to keep Scala going for building our App for coming years.

3 Likes

Scala on Android is possible but has some caveats and the biggest one is that you should nor really be doing the naive thing of just compiling a JAR.

The two current successful approaches include using the react-native approach through ScalaJS or using GraalVM to produce a native image.

More info here: Why Scala (still) hasn't missed the Android opportunity

1 Like

HOW to install Scala in Android device? I inveted this telegram channel @scaladevnotes

1 Like

More on Scala on Android available here by @makingthematrix :

3 Likes

Here is now a discord channel for Scala on android: https://discord.gg/XHMt6Yq4

1 Like
1 Like

That’s really interesting, it seems that you’re the first to manage to run a recent Scala on Android (without using graal native or a different backend), very cool!

No one is actively working on that, there’s Scala on Android · GitHub but it hasn’t been active in a long time, but you could mention in support for Scala 2.12 · Issue #334 · scala-android/sbt-android · GitHub that you got Scala 2.13 working and see if that motivates people to help.

I don’t think that can be worked around from Scala’s side without breaking binary compatibility, is there an Android bug report about this?

Looks like Statics already has a fallback on sun.misc.Unsafe#storeFence if the VarHandle class is not found: scala/Statics.java at 889d5c685f4e3c2ae399d861a7afad438c187ab1 · scala/scala · GitHub, it seems that in Android the class exists bu the method doesn’t, so maybe we just need to do catch (ClassNotFoundException | NoSuchMethodException e) in that first catch clause, if that works I would encourage you to send a PR with that change.

5 Likes

We’d love to publish a blog post on Blog | The Scala Programming Language about this, if someone out there wants to write one.

(I think it almost never occurs to anybody to submit a “guest” blog post, but they are sometimes welcome, depending on the topic and content.)

1 Like

Why not, if (as @onsq has apparently shown) it works?

1 Like

A pull request adding that to Community | The Scala Programming Language would be very welcome (at Pull requests · scala/scala-lang · GitHub).

1 Like

Hi,
I’m always happy to write something.
I think maybe my original blog entry can be made into a guest post? Scala on Android – Making the matrix
but if you prefer something different (more technical?) I’m open to suggestions.

1 Like

@makingthematrix Yes, I think either your post or William’s post could be the basis of something for the Scala blog, once it was updated to discuss not only the GraalVM and Scala.js options, but also the new one @onsq has pioneered, with pros and cons to help the reader choose.

I think an ideal blog post would probably be pretty high level and not super long, but it would include links to more detailed resources covering all three options.

1 Like

I encountered the releaseFence problem when trying to make Scala compile in GraalVM native image.
https://mvnrepository.com/artifact/org.scalameta/svm-subs

Here’s a discussion about it that I have found while looking into this issue: Regression with Scala 2.13: cannot compile GraalVM native-image · Issue #11634 · scala/bug · GitHub
And in the end I solved it with a small dependency from @olafurpg : https://mvnrepository.com/artifact/org.scalameta/svm-subs (it’s explained how it works in the discussion).

So you are able to use Scala 2.13 with Android SDK? Wow. That’s interesting. In my work at Wire I’m stuck with Scala 2.11 (because we still support older Androids). Recently I talked about this on ScalaLove (there’s a link in previous comments :slight_smile: ). Right now I think that trying to work with Scala on standard Android SDK is too much hacks and an uneven fight with Google, so I look more into GraalVM and/or React Native + Scala.js. But it’s very good to know that someone is trying :slight_smile:

1 Like

@SethTisue : Great!

@onsq : Could you write more about your work? Here, or maybe can I contact you otherwise?

From what I understood it required a lot of troubleshooting.
When I said the naive thing I meant exactly that. Create an sbt project, add sbt-assembly, write some code, execute sbt assembly, install the uber jar, and that is it.

Of course, it is very exciting to see that, even if with some caveats, we would have a more “natural” of creating Android apps using Scala than using GraalVM or Reac Native.

2 Likes

@SethTisue Should I create an Scala on Android channel on the official Scala Discord?

We cover js and native so good addition.

1 Like

Would love a sample project to try it out on, @onsq .

@bjornregnell thank you for sharing my article!

3 Likes

@bjornregnell wdyt, does it make the most sense to have the separate server, or a channel on the main server, or both?

@SethTisue for greater clarity the discord server @bjornregnell has linked is NOT a dedicated discord for Scala android. Its a discord server setup by @leobenkel to help people learn and get better at scala. The Android channel is one of the newer channels added to it. Join it! Its a cool discord.

Yes. I asked Leo to create it just a week ago. I’m going to post there links to blog notes and example apps I’m making. So far I got a few questions, and - before the channel was set up - one person made a Scala app based on an example in Java. I hope more will come :slight_smile:

3 Likes