Skip to content

Getting Started

Alexandr Esilevich edited this page Oct 11, 2024 · 13 revisions

This article will guide you through using the Swift Toolchain for Android to create, build, and run Swift SPM projects on Android. You’ll learn how to set up a new project, build it for the Android platform, and run it on a connected Android device or emulator.

Installing Swift Toolchain for Android

The easiest way to install the Swift Toolchain for Android is by installing scd, the SCADE Build Tool, via the Homebrew package manager. The scd tool will install all the necessary components for building Swift code for Android. To install scd via Homebrew, run the following command:

brew install scade-platform/toolchain/scd

Creating Swift SPM project

To create a new SPM project containing an executable product, use the swift package init command from the standard Swift toolchain for macOS:

mkdir AndroidHelloWorld
cd AndroidHelloWorld
swift package init --type executable

Building Swift SPM project for Android

To build the project for the Android platform, run the scd build command and specify the desired Android platform to build for:

scd build --platform android-arm64-v8a

Running Swift SPM project on Android

To run the project on a connected Android device or emulator, execute the scd run command and specify the correct Android platform and the connected device or AVD name. If a device name is not specified, the scd run command will attempt to detect the connected device automatically:

scd run --platform android-arm64-v8a

This command will build the project, install it on the connected device or emulator, and run the executable. You should see the output of the executable, such as Swift Hello World!:

$ scd run --platform android-arm64-v8a
Building for debugging...
[1/1] Write swift-version--1B4E18939B7FC918.txt
Build complete! (0.14s)
Installing archive /Users/user/AndroidHelloWorld/run/android-arm64-v8a-Debug/lib to device...

Starting executable...
Swift Hello World!