Scala 2.13 on Android

@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.

2 Likes

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

Yes I think a discord channel for scala-android on the official scala server would be good, and at least eventually also a Gitter channel alongside the others. But it depends on if people are prepared to hang out there… but why not give it a try.

When we have decided what to do (?) I can make a PR. The “learning scala”-discord does not seem to be on that page, only the official discord. Community | The Scala Programming Language. It makes sense to add the whole server invite perhaps? And should there be a gitter channel on scala-android?

I created a new scala-andoid channel on the official scala discord.

To join the official discord join via https://discord.gg/scala

2 Likes

Not very sure about this one, it’s probably about if Google’s willing to support more of Java 8 or above, currently the whole class definition’s missing. Currently we’re just putting a slightly modified ClassValue.java copied from original OpenJDK source to patch it into the compilation.

Cannot say for sure without testing, but I think the log hints this probably is true. Typically when it hits the part and crashes the log looks something like this

W/.client.androi: Accessing hidden method Ljava/lang/invoke/VarHandle;->releaseFence()V (blacklist, reflection, denied)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
        at scala.runtime.Statics$VM.mkHandle(Statics.java:172)
        at scala.runtime.Statics$VM.<clinit>(Statics.java:155)
        at scala.runtime.Statics.releaseFence(Statics.java:148)
        
        ...
        
     Caused by: java.lang.NoSuchMethodException: java.lang.invoke.VarHandle.releaseFence []
        at java.lang.Class.getMethod(Class.java:2072)
        at java.lang.Class.getDeclaredMethod(Class.java:2050)
        at java.lang.invoke.MethodHandles$Lookup.findStatic(MethodHandles.java:782)
        at scala.runtime.Statics$VM.mkHandle(Statics.java:161)
        at scala.runtime.Statics$VM.<clinit>(Statics.java:155) 
        at scala.runtime.Statics.releaseFence(Statics.java:148) 

        ...

Android explicitly notifies the method is blacklisted so it’s bit uncertain if catching NoSuchMethodException to route to the fallback will work if Android is just shutting down VM for trying to poke the method.

The hack was pretty minimal but packaging all the working stuff into a project must be something else, there are some difficulties with producing a clean minimal working project to be shared for following reasons -

  • As mentioned above, java.lang.ClassValue.java has to be patched in. This is normally just easy as putting a source file under src/java/lang, but there are more stuff to consider, because this doesn’t work if Gradle is running with JDK 9 or higher, due to newly introduced Java platform module system(JPMS) and we are yet to find a working configuration to actively patch with JDK 9 or higher so currently requires JDK 8 to work.
  • We currently are not using sbt but Gradle with the following plugin GitHub - dsvdsv/scala-android-plugin but the original repo is written and compiled with Java 11, which conflicts with the JDK8 requirement of above. This could easily be solved with porting the code to Java 8 and publishing for local.
  • Scala library itself also has to be patched for the scala.runtime.Statics.java mentioned above. However, this can’t be done with just putting it under the source file folder, but had to be done with making a small project to make a patched fat-jar of scala-library. Once patched and published for local this also was just good to go.

We’re happy to give out more details of the works as much as we can, please ask about any specifics you wish to know :smile:

1 Like

Well, you already answered some of my questions. Thanks :slight_smile:
Now the most important thing is - can you share the source code of your project? I work at https://wire.com where we still use Scala 2.11 for the Android client of our product. For sure we will not reuse your code or even update to a newer Scala version (which I find very saddening, personally, but well, too late to do anything about that) but it would be very interesting for me to compare what you’ve done with what I’m using at work.

We’ve managed to find a configuration to easily reproduce it with a small testing app and uploaded it to github. Please check the following repo:

4 Likes

https://github.com/scala/scala/pull/9752 has been merged in time to make it to 2.13.7 :tada:

2 Likes

I am interested in publishing a Blog post about upcoming developments in Scala

Okay — tell us more about what you have in mind?

FYI, according to Google Issue Tracker java.lang.ClassValue has been implemented in upstream Android

1 Like

There is a new Gradle plugin for Scala on Android:

Example project using this plugin:

More discussion on Discord ( Scala ):

Discord