Skip to content
Skip
3.1k

FOSDEM 2026 Swift Talk

  • iOS
  • macOS
  • Server
  • Embedded
  • Windows
  • WebAssembly
  • Android
  • Other
  • Most widely-used operating system in the world
  • ~4 billion Android handset device users
  • OS for myriad of IoT/embedded/auto devices
  • Linux Kernel (GPL)
  • Android Open Source Project (AOSP) (Apache)
  • Android Certified (proprietary)
    • Google Mobile Services + Google Play Services
iOSAndroid
Modern LanguageSwiftKotlin
Legacy LanguageObjective-CJava
Build SystemSwiftPMGradle
Platform SDKObj-C (+C & Swift)Java (+C)
OS/KernelXNU/Darwinnon-GNU/Linux
Native LibrariesMach-O .dylibELF .so

Android for iOS Developers: App Development

Section titled “Android for iOS Developers: App Development”
iOSAndroid
IDEXcodeAndroid Studio, IntelliJ, …
Virtual DeviceiOS SimulatorAndroid Emulator
UI FrameworkSwiftUI (Swift)Jetpack Compose (Kotlin)
UI (Legacy)UIKit (Obj-C)XML Views (Java)
App Packaging.ipa.apk
Distribution PortalApp Store ConnectGoogle Play Console
App MarketplaceApple App StoreGoogle Play Store

See Native Swift on Android, Part 1, September 2024

Flash Poll 2: Swift Cross-Compilation SDK Experience

Section titled “Flash Poll 2: Swift Cross-Compilation SDK Experience”
  • Static Linux (musl)
  • Webassembly (wasm)
  • Android
  • SE-0387: Swift SDKs for Cross-Compilation implemented in Swift 6.1 (2024)
  • Self-contained .artifactbundle archive
  • Enables building for a separate target than the host
  • e.g., build for Android from macOS or Linux
  • Like building for iOS from macOS
  • Other Swift SDKs: WebAssembly (wasm), Static Linux (musl)

Unlike the other Swift SDKs, the Swift SDK for Android is not self-contained. It requires an external “Native Development Kit”

  1. Host Toolchain (OSS via swiftly, not Xcode)
  2. Swift SDK for Android
  3. Android Native Development Kit (NDK)

Structure of the Swift SDK for Android: 1. The Host Toolchain

Section titled “Structure of the Swift SDK for Android: 1. The Host Toolchain”
  • Host Toolchain (OSS via swiftly, not Xcode)
    • Lives in:
      • ~/Library/Developer/Toolchains/swift-6.2.3-RELEASE.xctoolchain/
    • (NOT: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain)
    • Provides: swiftc

Structure of the Swift SDK for Android: 2. The Swift SDK for Android

Section titled “Structure of the Swift SDK for Android: 2. The Swift SDK for Android”
  • Swift SDK for Android
    • Lives in: ~/Library/org.swift.swiftpm/swift-sdks/swift-6.2.3-RELEASE_android.artifactbundle/

Structure of the Swift SDK for Android: 3. The Android Native Development Kit (NDK)

Section titled “Structure of the Swift SDK for Android: 3. The Android Native Development Kit (NDK)”
  • Android Native Development Kit (NDK)
    • Lives somewhere like:
      • ~/Library/Android/sdk/ndk/27.0.12077973/
    • Provides:
      • aarch64-linux-android28-clang, x86_64-linux-android35-clang, ld
    • Outputs: lib/arm64-v8a/libMySwiftCode.so
  • Build for ARM (64):
swift build --swift-sdk aarch64-unknown-linux-android28
  • Build for X86 (64):
swift build --swift-sdk x86_64-unknown-linux-android28
  • arm64-v8a (64-bit ARM): handsets, most popular
  • armeabi-v7a (32-bit ARM): older handsets, IoT
  • x86_64 (64-bit Intel): Chromebooks, dev emulators
  • x86 (32-bit Intel)
  • mips (old RISC)
  • riscv (new RISC)

The Swift SDK for Android comes with a number of built-in .swiftmodules

  • Swift standard library
  • Foundation
  • Dispatch
  • Observation
  • Testing & XCTest
  • Android (instead of Darwin)

Pretty much everything else…

  • CoreGraphics, CoreLocation, etc.

  • CloudKit, StoreKit, GameKit, HealthKit, etc.

  • UIKit, SwiftUI

  • Rule of thumb: if it isn’t available on Linux, then it won’t be available on Android.

  • swiftpackageindex.com: search “platform:android
  • Adding Android compatibility testing blog post
  • ~30% of their ~9k packages build for Android
    • swift-collections, swift-crypto, swift-log, …
    • Alamofire, Factory, Yams, GraphQL, Supabase, …

How can we get our own Swift Packages working on Android?

#if canImport(Darwin)
import Darwin // macOS, iOS, etc.
#elseif canImport(Glibc)
import Glibc // for Linux
#elseif canImport(Android)
import Android // or Bionic
#endif
import Foundation
// non-Darwin Foundation doesn't include FoundationNetworking
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Terminal window
swift package init --name DemoCmd --type executable
swift run DemoCmd # builds and runs on host
swiftly run swift build --swift-sdk aarch64-unknown-linux-android28 --static-swift-stdlib
adb push .build/aarch64-unknown-linux-android28/debug/DemoCmd /data/local/tmp/
adb push …/libc++_shared.so /data/local/tmp/
adb shell /data/local/tmp/DemoCmd

https://www.swift.org/documentation/articles/swift-sdk-for-android-getting-started.html

  • Local Testing
Terminal window
swift package init --type library --name DroidLib
swift test
skip android test
  • CLIs and tests aren’t an app
  • Android apps need to work with the Android SDK…
    • …which is Java
    • …and that means the Java Native Interface (JNI)
  • Mads is up next to talk about that…