Swift for Android: Call for the Community

I have set up a daily CI on Github Actions that checks out latest trunk of three Swift packages- swift-argument-parser, swift-crypto, and swift-nio- and cross-compiles them with Swift 5.4 and my Android SDKs for Android AArch64 and x86_64, then runs the x86_64 tests on the Android x86_64 emulator. All tests pass for the first two but swift-nio has some failing tests, which I don't see on AArch64 when I natively build on there, so I disabled that for now and will look into it. Anyone interested in cross-compiling for Android or setting up their own Android CI could use that as a starting point.

Any suggestions for other Swift packages to add to the daily CI are welcome, I think I'll add swift-system and swift-collections next.

I plan to set up a daily CI for the 5.5 branch and trunk too, which will cross-compile the Android SDKs too from the latest source snapshots then use them to cross-compile these Swift packages.

I'll see if I can fix the Android armv7 SDK next, which has an issue with the Swift 5.4 compiler optimizing the stdlib incorrectly.

9 Likes

Hi,

really appreciate the discussion and efforts. Our Swift for Android compiler is available for a couple of years now, starting with Swift 4.0 were we were first able to provide a stable Swift compiler and now supporting Swift 5.4 for ARM and x86. Fixing all the issues and making sure it runs stable is not an easy task, but the compiler benefited greatly from ABI stability, Module stability and library evolution support in the last releases.

Viva Swift
Your SCADE team

11 Likes

I just saw this blog post by a small team which translates Swift to Kotlin for Android, including SwiftUI as you wanted, thought you'd be interested. Someone linked it from the HN comments on the Swift blog post about using it with Adwaita, which appears to have blown up, one of the top 10 most upvoted Swift posts on HN since its release.

5 Likes

Thanks for the mention, @Finagolfin! I've been following closely all the great work you've been doing, along with all the other interesting Swift/Android toolchain work done by the community over the years.

As you saw, our approach takes a different tack: rather than using a native Swift-Android toolchain, we instead transpile the Swift into Kotlin using an Xcode build plugin and SwiftSyntax. This has certain advantages (no copying of strings or other types; bridge-less integration with Java and Android APIs; translation of SwiftPM modules into the Gradle build system; support for Java dependencies) and disadvantages (not all Swift concepts can be expressed perfectly in Kotlin; Xcode build plugins have many bugs and limitations).

We think these tradeoffs are worth it, as you can build a complete Android app using Xcode and SwiftUI – and ship it to the Play store – today. For tomorrow, we are closely watching all the great work the community has been doing getting an Android toolchain up and running. Skip already supports integration with C code (see Sharing C code between Swift and Kotlin for iPhone and Android apps), which is how many of our ecosystem modules like SkipSQL and SkipScript are implemented. And we hope that someday this integration will extend to natively-compiled Swift code using an Android toolchain.

This would open up an interesting world where the low-level parts of your app could be compiled Swift for each of the required Android architectures (x86, arm, and – soon – riscv), whereas the high-level UI parts (Jetpack Compose) would continue to be transpiled from SwiftUI into Kotlin, and then the two halves would be fused together with a JNI bridge that is generated by the transpiler. There's a long road ahead for this: not just a working Android toolchain, but also the large swaths of swift-foundation API that remains to be implemented, as well as all the necessary hooks to integrate with the Android native NDK and Java SDK.

For those who are interested in learning more about what can be accomplished with Skip's approach in the here and now, you can watch some Intro Videos, fiddle with the online Transpiler Playground, or check out our recent Compile Swift Podcast interview. You can also try it out your self with the Getting Started guide.

Feel free to reach out – either in this thread or directly to me – if you have any questions or feedback.

1 Like