Why Scala (still) hasn't missed the Android opportunity

And what you should do in light of this.

Rewritten on originally published on

Kotlin and Android tied the knot back in 2017. While on the surface it looked like a disappointment to Scala, it was a blessing in disguise.

A screenshot of a tweet by @posco, suggesting that Scala and Lightbend missed a big opportunity
Quite a few people had similar thoughts: did Scala really miss this opportunity?

My experience with Scala & Android

I first tried to make an Android app with Scala in 2015 and the experience was not something to be repeated. The Android ecosystem really prefers Gradle as a build tool rather than SBT, and the Dalvik Java engine does not support Java 8's Lambdas that were released in 2014 (required to run Scala 2.12). Lastly, at the time all the documentation and libraries targeted Java users and a Java-like coding style which made using Scala very challenging.

Since the original article in 2017, the mobile development ecosystem has changed a lot, and many of Scala's features have been adopted in the main programming languages of both iOS and Android platforms. Let's recap what happened in the last few years.

Why did Google make Kotlin official on Android?

While we don't really know the internal discussions at Google, we can speculate why adopting Kotlin made a lot of sense:

  • The legal battle between Google and Oracle over Java APIs.
  • Language development strategy: there are features that Kotlin and Scala have had for years beore they were made available in Java, such as records (data/case classes).

These two things combined, on top of the fierce competition from Apple with its excellent Swift language and development ergonomics, would have definitely made Google feel a bit itchy. At the time, you would have had 3 candidates on the JVM: Groovy, Scala and Kotlin. Scala is still under heavy development and going in its own direction, Groovy had lost its sponsorship from Pivotal, and then there's Kotlin, backed by JetBrains, who are behind the platform that powers Android Studio IDE. I know what I would choose under this environment.

Impact on Scala

There has been speculation whether this would kill Scala, just like when Java 8 came out. Short answer in 2017 was no, and it still remains so.

Long answer: any improvement in other languages is an improvement to Scala. The biggest challenge to Scala's adoption has been its learning curve. Improvements in a popular programming language typically bring Scala's ideas to the mainstream (look at PHP 8!), thereby decreasing Scala's learning curve, which is always a positive.

Another reason why Scala is not really losing out to much of its competition is because it is not competing as a "better Java" or as a "better C". It is in a league of its own, being the most popular programming language with generic type programming, which gives you the ability to eliminate boilerplate by pushing it down to compile-time statically verified macro calls. For example, you can generate HTML tables from data automatically, and Scala's doobie SQL library makes SQL injection impossible.

The importance of focus and strategy

Had Lightbend and Scala spent any effort gaining adoption in Android, I assure you, Scala 3 would not be having its launch party in April 2021. Prioritisation is crucial and to be successful you have to pick your battles wisely: there's a reason after all why Apple and Google completely dominate the mobile market. Scala's current battle is not in adoption, nor in relieving the pains of Java; it's in creating the most quality functional-OOP programming language with solid interoperation with its platforms.

Big mobile developments to date

From Scala's perspective, these very significant developments to both major mobile platforms very much focus on the more declarative and immutable style, which is exactly Scala. These developments completely change the way mobile apps are being developed:

How can I use Scala for mobile development today?

Hold your horses! :-) Let's instead explore the question if you should! It really depends on your goals and resources at your disposal.

What should I use for app development today in 2021?

Use this questionnaire:

Do you plan to use the latest iOS/Android features for your apps, OR performance is very important?

Unreservedly use Swift/SwiftUI and Kotlin/Jetpack Compose and develop both separately. By directly using the platforms and frameworks provided by Apple or Google, you will always be able to leverage the latest developments on the platforms as part of Apple's & Google's developer advocacy strategies. Using the platform-provided frameworks you will also have the the most comprehensive documentation and tutorials. When I was learning Swift, I found an incredible amount of high quality resources to learn from and was basically never stuck.

Are you experienced in Scala & the JVM?

There are 2 major possibilities at the moment which would enable you to run Scala on mobile.

react-native based, using Scala.js

A photo of an iPhone running the Slinky demo app
It's simple and it works!

This option has existed for a while however took a lot of refinement and work from various parties to make it really accessible.

The react-native framework, created by Facebook, can be used by Scala.js to run native apps with Scala.

What is excellent about react-native is the Expo framework that enables over-the-air updates and other huge enhancements that simplify the whole lifecycle around releases and deployments to the Play store and the App store.

You will be using SBT for this and be able to leverage some of Scala's most powerful functional programming libraries as well as many JavaScript libraries from the NPM platform.

Example project

By @shadajl, expo-template-scala:
"Make React Native apps in Scala with Scala.js, Slinky & Expo".


A photo of an Android running JavaFX app
JVM in your hands!

GraalVM-based native executables of JVM bytecode

The GraalVM JDK enables you to build native executables of JVM apps. Java on iOS & Android became a reality in 2019 when Gluon adopted JavaFX and extended GraalVM with the 'Gluon Client Tools'.

Example project

By @makingthematrix, scalaonandroid:
"A tutorial and examples of how to write Android apps in Scala 2.13".

You will be using Maven for this and you should be able to reuse most existing JVM libraries.

To make it work is much more involved (currently, to build for Android, Linux is required), and to use Gluon Mobile without a nag screen you will need a subscription. Nonetheless, this option literally gives you all of JVM on iOS and Android so the leverage is very substantial. Find out more on Maciej Gorywoda's article.

Again, only if you are experienced in the JVM do these make sense, because there really is a lot to learn. But when you are experienced, the reward of getting it done quickly using a familiar environment including your IDE is immense.

As an aside, I would be very excited about having scala-native on iOS and Android because this would mean direct access to the underlying libraries & frameworks, eliminating that delay of framework updates. However we may have to wait a while for that.

Do you have limited resources?

Consider using Microsoft Xamarin or Kotlin Multiplatform. These frameworks enable you to write the code in C# or Kotlin, and deploy to multiple platforms from a single codebase.

Otherwise

Go native. There are only advantages in tailoring very specific user experiences to each platform. How Android does things is different from iOS; I personally prefer to use iOS for some tasks and Android for others, as they are that different.

What about the back-end?

Here I can afford to be more opinionated as Scala is really strong on things that demand high scalability.

Do you have plans for high scalability or big data & data science, or are you already familiar with Scala?

Use Scala.

Are you developing the mobile app with Xamarin or Kotlin?

In both cases, you will gain hugely if your developers are able to use the same programming language across the stack, because each feature developed can be done by 1 person, significantly reducing project management and synchronisation efforts. Xamarin being C#/F# based will enable you to leverage the .NET platform.

Are you developing with react-native in JavaScript/TypeScript?

It might be easier to stick to those same technologies on the back-end as well based on the same reasons as Xamarin/Kotlin above.

Otherwise

Consider using either Scala or something that you already know very well. The final answer will be very specific to your situation, eg how many resources you have to invest, how much help you can get, and so forth - like learning any new set of technologies.