The Swift SDK for Android, recently released as a nightly build, is designed to help developers to port their Swift packages to Android, making it easier to share code across platforms. While the SDK is still in preview, over 25% of packages in the Swift Package Index can already be compiled for Android.
At its core, the Swift SDK for Android consists of a Swift toolchain tailored for the Android platform, i.e., the compiler and an implementation of the Swift standard library running on Android, along with a collection of bindings that provide access to Android APIs from Swift. The Swift toolchain, that uses the swift-java-project for Java/Swift interoperability, can create shared objects that are linked into an .apk archive and accessed through the JNI.
The Swift SDK for Android takes a non-partisan approach to UIs. Instead of committing to a single UI framework, the team behind it will support existing ones, specifically Android’s native toolkits like Jetpack Compose and the classical XML-based system, Flutter’s UI engine, as well as third-party bridging solutions like Skip. Another possible approach, not explicitly mentioned by the Swift Android team is to implement the UI in Swift using OpenGL, Vulkan, or other rendering engines, and then use a NativeActivity to integrate it in an Android app.
The Skip framework, in particular, re-implements SwiftUI for Android by bridging it to Jetpack Compose. This approach provides a seamless way for iOS developers to write both their app’s business logic and UI in the same codebase, with minimal additional effort.
An open-source alternative providing SwiftUI-like API for UI across macOS, Linux, Windows and some nascent support for Android is SwiftCrossUI.
While the prospect of using Swift for both iOS and Android apps may sound enticing, creating great cross-platform apps is not just a matter of toolchain support and native toolkits, as andrekandre notes on Hacker News:
[I] have gone down this route before, and tbh the biggest issue is dev ux (ios devs cant debug easily, model mismatches vs kotlin e.g kotlin exceptions cant be caught from swift) and in the end even kotlin multi platform isn’t the same as kotlin for android, so in a sense you are still introducing a 3rd language after a fashion…
The initial release of the Swift SDK for Android comes roughly six months after Apple announced the establishment of an Android working group within the Swift project, aimed at ensuring that Swift can be compiled for Android without relying on unofficial forks, as well as enhancing Swift’s standard libraries for better compatibility with Android APIs.
Prior to the creation of the Swift SDK for Android, developers could use third-party solutions like Scade.io to compile their Swift code to run on Android. Scade is based on Swift4j and focuses on non-UI application logic.
For Android developers looking to port their apps to iOS, a more mature option is Kotlin Multiplatform with Jetpack Compose Multiplatform, which InfoQ has covered extensively in the past.
