Scala 2.13 on Android

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