Interoperability with Kotlin/Java

How is the Android Workgroup addressing seamless interoperability between Swift and existing Kotlin/Java codebases on Android? What are the best practices for integrating Swift into a hybrid Android app?

2 Likes

Right now we haven't written out any best practices yet. Though there is some good prior art that we've been very inclined to expand upon, or at least something similar to it.

Swift-Java provides interop with the JVM, in particular using either Project Panama (Oracle SDK) or JNI (any Java SDK).

Using that, @ColemanCDA has created Swift bindings for the Android APIs using codegen: GitHub - PureSwift/Android: Swift library for Android

Swift-Java not only exposes Java classes to Swift, but also can do the inverse - so your Swift business logic can be called from Java and compatible languages like Kotlin.

As for Kotlin interop, we haven't had enough discussion on that to paint a picture. Java interop is a great start, but lacks some of the finer naunces we can't yet express like concurrency and sealed classes.

7 Likes

Have you considered working with JetBrains as they are also working on Kotlin/Swift interop for Kotlin Multi Platform

3 Likes

I second this and its been a while Jetbrains are working on this

2 Likes

KMP is Kotlin compiled by LLVM, completely different runtime from the Kotlin/JVM used on Android.

1 Like

Java interoperability is an ongoing effort with it's dedicated category over here: Java interoperability - Swift Forums

We're making steady progress and a lot of functionality doesn't need "special" kotlin handling since on bytecode level it's all "just" java classes. Special things which Kotlin had but cannot natively express in java bytecode will need special handling someday, but we're not there yet and there are no concrete plans about that.

Having that said, you can get pretty far with just interoperating "simpler" types which just are expressible using Java concepts. Calling down into Swift specifically I don't think would need any kotlin special sauce at all, since Kotlin can just call generated Java bindings. As for calling Kotlin code which has some tricky semantics which are not directly mapped to Java but erased in bytecode or represented using various sneaky techniques, that remains to be see when / if at all that would be supported, and when we'd do that.

For now, the focus is entirely on providing a safe and efficient baseline for JVM / Swift interoperability, which already has a number of challenges, so we'd like to solve this properly first.

4 Likes

Following this closely and I might try out an early branch. I don’t mind Kotlin at all, but Swift is a bit closer to the metal, more like C++