How to compile and run an example - makingthematrix/scalaonandroid GitHub Wiki

  1. We will create the app with the help of Gluon Mobile - a platform which uses JavaFX to build Java client apps. It looks like this will be the way to build Android apps with GraalVM in foreseeable future (https://github.com/oracle/graal/issues/632#issuecomment-643816258). No standard Android widgets - JavaFX instead. It has a lot of interesting implications but that's a story for another day. https://gluonhq.com/products/mobile/ . You don't need to download anything directly from Gluon Mobile. Maven will do it for you. So, yes, we're using Maven, not SBT, because we need Maven plugins which don't have their SBT equivalents. Install mvn if you don't have it yet. By the way, here's a much more detailed explanation how to write a client app with Gluon (in Java): https://docs.gluonhq.com/client/0.1.31/#_overview . A big advantage of this approach is that - in theory at least - we should be able to use a lot of the same stuff for writing apps for Android, iOS, and desktop clients. In my company we have separate teams for that, writing different versions of the same app in different technologies, which is costly and means that we can't help each other as much as we could. On the other hand, we tried to use the same tech stack in the past, and there were reasons why we decided to abandon this approach... Okay, this is also a topic for another day.

  2. Now, download the code :) Currently there are two examples to choose from:

  • Dummy App, based on HelloGluon from Gluon samples: https://gluonhq.com/developers/samples/ .
  • And European Union (+ Scotland) based on FiftyStates from the same repository. You can choose which one you want to run by opening pom.xml and changing the contents of the <main.class> tag. It should point to the Main class of one or the other example.
  1. By the way, in pom.xml you will find a list of plugins and dependencies the app uses (well, d'oh, this is the place where we put them, after all). I'd like to say a few words about them.

    • Scala 2.13.4. Not much to say here apart from that I'm very happy I see this version number here.

    • JavaFX 15, the UI widgets library. https://openjfx.io/ .

    • Gluon Glisten. I'm not exactly sure yet what's the relation between this and JavaFX. Something something user interface something. https://docs.gluonhq.com/#_glisten_apis .

    • Then comes a bunch of dependencies that I think are there to enable the app to interact with Android in a number of ways. I decided to keep them, just to know that they exist, but the dummy app only uses "display" and "util" from the list. (it's also possible that I don't understand something here - when I tried to remove some of them, compilation failed).

    • scala-maven-plugin (https://github.com/davidB/scala-maven-plugin) - Thank you, David!

    • maven-compiler-plugin - I don't know much about it, tbh.

    • javafx-maven-plugin - to compile JavaFX with Maven, I guess?...

    • And finally client-maven-plugin for Gluon. Here you can see an argument for native-image: --report-unsupported-elements-at-runtime. Without it, compiling Scala ends with an error:

      Error: Unsupported type java.lang.invoke.MemberName is reachable: All methods from java.lang.invoke should have been replaced during image building.
      

      The issue is known and it should be fixed eventually: https://github.com/oracle/graal/issues/2761 .

    • In the profiles section, I keep both the desktop and Android profile. This allows for running the app on the computer without the need to have my Android phone around all the time.

  2. First try to build the app with mvn client:build and solve the problems along the way. If it works, mvn client:run will run the desktop version of the dummy app. If you run "Dummy App" you will see a window with the title "Hello, Gluon Mobile!", a picture of this weird Java mascot, the Duke, and a button with a magnifying glass icon. Clicking on it does not really let you search for anything, however, but will print "log something with Scala" to the console. In future, if you change something in the code that does not touch Android, and just want to check quickly the results on the desktop, you can do that with mvn javafx:run. It will compile and run the app but won't build the native image.

  3. When that works, you can try to compile an Android APK:

    mvn -Pandroid client:build client:package
    

    This again may at first produce a list of errors. Be brave. When it finishes with success, in target/client/aarch64-android/gvm you will find the compiled app:Scala 2.13 on Android.apk. Connect your Android phone to the computer with an USB cable, give the computer permission to send files to the phone, and type adb devices to check if your phone is recognized. It should display something like this in the console:

    > adb devices
    List of devices attached
    16b3a5c8	device
    

    Then, adb install Scala 2.13 on Android.apk should install the app on the phone and a moment later you should be able to see it on your phone's main screen. When you click on its icon it should open approximately the same screen as its desktop version.

    Installation might not work for a number of reasons, one of the most popular being that your Android simply does not allow installing apps this way. Go to Settings, find "Developers options", and there enable "USB debugging" and "Install via USB". If you can't find "Developers options" anyway then it might mean you need to do something funny to make it appear, like tapping the field with your Android OS version number ten times.

    If everything works and you see the dummy app's screen on your phone, type adb logcat | grep GraalCompiled to see the log output of the dummy app. Now if you click on the button with the magnifying glass icon, you should see "log something from Scala" printed to the console. (I'm sure there are plugins for that for IntelliJ or maybe even VS Code - after all, there is one for Android Studio)

⚠️ **GitHub.com Fallback** ⚠️