Environment & Swift Package Setup

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Before writing Swift code for Android, you need to set up your development environment and create a Swift package. This segment covers the complete toolchain installation and introduces Swift Package Manager.

Getting Started

Access the starter project from the Materials section in the right sidebar. Open the Starter project in Android Studio.

Installing Swift SDK for Android

The Swift SDK for Android enables cross-compiling Swift code for Android devices. You’ll install the complete toolchain on your development machine.

Prerequisites

To follow along, you’ll need:

Installing swiftly

swiftly is the official Swift toolchain manager. It allows you to install and switch between different Swift versions easily.

curl -O https://download.swift.org/swiftly/darwin/swiftly.pkg && \
installer -pkg swiftly.pkg -target CurrentUserHomeDirectory && \
~/.swiftly/bin/swiftly init --quiet-shell-followup && \
. "${SWIFTLY_HOME_DIR:-$HOME/.swiftly}/env.sh" && \
hash -r
source ~/.zshrc  # if using zsh (default on macOS)
# OR
source ~/.bashrc  # if using bash
swiftly --version
1.1.1

Installing Swift 6.3

Now install Swift itself using swiftly:

swiftly install 6.3-snapshot
swiftly use 6.3-snapshot
swift --version
Apple Swift version 6.3-dev (LLVM 478f55c39d6bc2c, Swift a15423cb66d4749)
Target: arm64-apple-macosx15.0
Build config: +assertions

Installing Swift SDK for Android

This is the critical component that enables cross-compiling Swift code for Android devices.

swift sdk install https://download.swift.org/swift-6.3-branch/android-sdk/swift-6.3-DEVELOPMENT-SNAPSHOT-2026-01-16-a/swift-6.3-DEVELOPMENT-SNAPSHOT-2026-01-16-a_android.artifactbundle.tar.gz --checksum 080da5553cdd12d286f715d86527089e7c924093733f8f4e1195f2bd2137d45c
swift sdk list
swift-6.3-DEVELOPMENT-SNAPSHOT-2026-01-16-a_android (installed)
  aarch64-unknown-linux-android28
  armv7-unknown-linux-android28
  x86_64-unknown-linux-android28

Setting Up Android Studio and NDK

The Swift SDK for Android requires the Android Native Development Kit (NDK) to link against Android’s C++ libraries. You’ll install the NDK through Android Studio’s SDK Manager.

export ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/27.2.12479018
export PATH=$PATH:$ANDROID_HOME/platform-tools
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin
source ~/.zshrc  # if using zsh (default on macOS)
# OR
source ~/.bashrc  # if using bash
echo $ANDROID_NDK_HOME
ls $ANDROID_NDK_HOME/toolchains/llvm/prebuilt/

Linking NDK to Swift SDK

Critical: After installing both the Swift SDK and Android NDK, you need to run a setup script that links them together. This creates a symlink from the Swift SDK’s expected NDK path to your installed Android NDK.

cd ~/Library/org.swift.swiftpm || cd ~/.swiftpm
./swift-sdks/swift-6.3-DEVELOPMENT-SNAPSHOT-2026-01-16-a_android.artifactbundle/swift-android/scripts/setup-android-sdk.sh
setup-android-sdk.sh: success: ndk-sysroot linked to Android NDK at android-ndk-r27d/toolchains/llvm/prebuilt

Understanding Swift Package Manager

Now that your toolchain is installed, you’ll learn about Swift Package Manager (SPM) which is the build system you’ll use to create your Swift library.

What is Swift Package Manager?

Swift Package Manager is Swift’s official build system and dependency manager. It provides:

Why Use SPM for Android?

You might wonder why you’re using Swift’s build system for Android development. Here’s why:

Creating Your Swift Package

Now you’ll create a Swift package for your task validation logic. This package will compile to a dynamic library that Android loads at runtime.

Creating the Package Directory

Navigate to your Starter project directory and create the package:

cd path/to/Starter
mkdir taskmanager-lib
cd taskmanager-lib

Initializing the Swift Package

Run Swift Package Manager’s initialization command:

swift package init --type library --name TaskManagerKit
taskmanager-lib/
├── Package.swift              ← Package manifest (configuration)
├── Sources/
│   └── TaskManagerKit/        ← Your Swift code goes here
│       └── TaskManagerKit.swift  ← Generated sample file
├── Tests/
│   └── TaskManagerKitTests/   ← Unit tests (optional)
└── .gitignore                 ← Git ignore rules

Understanding Package.swift

Open Package.swift and examine its structure:

// swift-tools-version: 6.2
import PackageDescription

let package = Package(
  name: "TaskManagerKit",
  products: [
    .library(
      name: "TaskManagerKit",
      targets: ["TaskManagerKit"])
  ],
  targets: [
    .target(
      name: "TaskManagerKit"),
    .testTarget(
      name: "TaskManagerKitTests",
      dependencies: ["TaskManagerKit"])
  ]
)

Verifying the Package

Build the package to ensure everything is configured correctly:

swift build
Building for debugging...
[4/4] Compiling TaskManagerKit TaskManagerKit.swift
Build complete!
find .build -name "TaskManagerKit.swiftmodule"
.build/arm64-apple-macosx/debug/Modules/TaskManagerKit.swiftmodule

Understanding the Build Output

The .build/ directory contains all build artifacts:

.build/
├── arm64-apple-macosx/             ← Architecture-specific build (or x86_64-apple-macosx)
│   └── debug/                      ← Debug build products
│       ├── Modules/
│       │   └── TaskManagerKit.swiftmodule ← Swift module interface
│       ├── ModuleCache/            ← Cached standard library modules
│       ├── TaskManagerKit.build/   ← Intermediate build files (.o, .d, .swiftdeps)
│       └── ...                     ← Other build artifacts
└── checkouts/                      ← Downloaded dependencies (empty for now)

Project Structure Diagram

Here’s what your project looks like now:

Starter/
├── app/                           ← Android app (Kotlin/Compose)
│   ├── src/main/kotlin/...
│   └── build.gradle.kts
├── taskmanager-lib/               ← NEW: Swift package
│   ├── Package.swift              ← Package manifest
│   ├── Sources/
│   │   └── TaskManagerKit/        ← Swift source code
│   │       └── TaskManagerKit.swift
│   └── .build/                    ← Build artifacts
│       └── arm64-apple-macosx/    ← Architecture-specific (or x86_64)
│           └── debug/
│               └── Modules/
│                   └── TaskManagerKit.swiftmodule ← Swift module
├── build.gradle.kts               ← Project-level Gradle
└── settings.gradle.kts            ← Gradle modules (will update)

What You’ve Accomplished

In this segment, you’ve:

See forum comments
Download course materials from Github
Previous: Introduction Next: Swift-Java Integration