Skip to content

European Union (and Scotland) example

Maciej Gorywoda edited this page Jan 17, 2021 · 11 revisions

The European Union (+ Scotland) example is an expansion on the original "dummy app" based on a Fifty States example from Gluon. I made it to test a bit JavaFX widgets, especially:

You can download the compiled APK from here. And here's a screenshot.

What I learned

  • Early in the process of working on this example, I was bitten by this bug: java.lang.NoSuchMethodException: java.lang.invoke.VarHandle.releaseFence(). You can follow the sources to learn more, but the simplest method to fix it is to add one tiny dependency to your Maven's `pom.xml:
        <dependency>
            <groupId>org.scalameta</groupId>
            <artifactId>svm-subs</artifactId>
            <version>101.0.0</version>
        </dependency>

It was made by Ólafur Páll Geirsson (https://github.com/olafurpg) from ScalaMeta. As I understand, the issue should be fixed in the Scala compiler at some point in the future?... (maybe in Scala 3 :D ). Anyway, it shows one thing that is very impressive to me. To write an Android app this way, I put together work of many brilliant people who do not necessarily even know about each other. Some of them work on Scala, some one GraalVM, others on Gluon Mobile and JavaFX. And there are also people from Android and yet others who develop other libraries I plan to use. They create new features and fix bugs for a lot of reasons not really connected to me writing an Android app in Scala. And yet, it all fits together.

  • One of the main functionalities in any Android app is to download pictures and display them. Here, I just used JavaFX - to download and display countries' flags from Wikipedia. And this is also how I learned that working with JavaFX listeners proved to be a bit tricky: they use Java generics and that doesn't translate very well to Scala. To spare you the details, in the end I wrote a thin wrapper over the one I used, ChangeListener, which clears the pics cache if the downloading of a picture ends with an error. It wasn't difficult, but it can be tedious on a larger scale, so I will look into alternatives.

  • ... Although simply learning scala.jdk converters might help a lot. I'd like to write as much in Scala as possible, but of course in many places cooperation with Java is necessary. Besides, Java 15 is much nicer than OpenJDK (Java 7, basically) I had to use when writing standard Android apps.

Next steps:

  • I will try to use Gluon's SceneBuilder to write the same UI. In this example I created the whole UI programatically in Scala. I understand that the same is possible with a mix of programming and FXML files, so in a manner similar to standard Android's widgets library, and that Scene Builder works as a WYSIWYG editor for FXML.

  • I see that I can also use CSS. I'm not a big fan but I'd like to at least read more.

  • The APK crashes on Google Pixel 5 with Android 11 and I want to know why.

  • Because of quirkiness of JavaFX listeners (mentioned above) I'd like to look into Glide as an alternative for downloading and displaying pictures. Another step on this trail would be to look into OkHttp as a http requests library for Android. Glide and OkHttp work well together and they are both very popular in Android app development. Unfortunately, they are both in Java and OkHttp v4 is in Kotlin (v3 is on long time support). I'd prefer to have Scala solutions for both.

  • Because of the same problem, I may look into ScalaFX. Gluon Mobile uses JavaFX directly, and scala.jdk converters are pretty cool, but anyway I want to experiment with ScalaFX a bit. Maybe I will find something in it that will convince me to use it instead of JavaFX + converters.

  • Gluon lets me create a cache for storing pictures downloaded from Wikipedia, but for more complicated purposes I need to know how to (1) access Android's file storage directly, and also how to (2) access Android's database.

Even later steps:

  • Push notifications (Google Play Services? MicroG? Something else?)
  • Web sockets
  • Access to the camera and microphone
  • Android's KeyStore (e.g. for storing encrypted passwords)
  • Intents from other apps (e.g. for file sharing)
  • The app lifecycle, esp. how to manage going to the background and waking up, receiving an event while in the background, etc.
Clone this wiki locally