Skip to main content

Command Palette

Search for a command to run...

Swift Android Gradle Plugin

Updated
3 min read
Swift Android Gradle Plugin

Introduction

Last week, the Swift for Android working group announced that Swift now supports Android. This is an exciting milestone for developers who want to leverage Swift's powerful features in Android development. To help developers explore this new capability, I created a Swift Android Gradle Plugin that simplifies the integration of Swift libraries into Android projects.

I've also built a sample application that demonstrates the plugin in action. The app generates fractal images using Swift and renders them with Jetpack Compose. You can read more about it here: https://charlesmuchene.com/swift-android-gradle-plugin-sample-app

Understanding Cross-Compilation for Android

To build a Swift package for Android, we need to cross-compile Swift code to target the Android platform. Cross-compilation is the process of building code on one platform (like your development machine) to run on a different platform (Android devices).

This process requires several tools working together:

  1. Swift Toolchain: The core binaries needed to build and run Swift code

  2. Swift SDK for Android: Contains all the resources needed to generate and run Swift code for Android (this is what was recently released)

  3. Android Native Development Kit (NDK): Cross-compilation tools for native Android development, widely used for building C/C++ code for Android

  4. Swift Android Gradle Plugin: Automatically configures build tasks to invoke the required cross-compilation tools

The setup guide in the plugin repository provides detailed instructions for installing these tools on your development machine.

Key Features

The Swift Android Gradle Plugin extends Gradle's functionality by adding new tasks and DSL elements to projects containing Swift code. Here's what makes it powerful:

Seamless Integration with Android Build System
The plugin hooks into the Android project task graph to compile and package Swift sources automatically. It builds for all configured architectures, copies artifacts to the appropriate directories, and cleans up artifacts when the project is cleaned.

Flexible Configuration DSL
The plugin exposes a DSL for build configuration, allowing you to set the target API level, specify which architectures to build for, and customize other build parameters to suit your project's needs.

Android Studio Integration
It works with Android Gradle Plugin (AGP) APIs to suggest Swift source sets in Android Studio, providing a smoother development experience.

Adding the Plugin to Your Project

The Swift Android Gradle Plugin is distributed via JitPack. Here's how to add it to your project:

// build.gradle.kts
plugins {
   // android app/lib plugin must be applied first

   id("com.charlesmuchene.swift-android-gradle-plugin") version "0.1.0-alpha"
}

// settings.gradle.kts
pluginManagement {
   repositories {
      // ...
   }

   // NOTE: Plugin is not published yet!!
   // Clone and add plugin as an included build
   includeBuild("../swift-android-gradle-plugin")
}

Looking Ahead

The Swift SDK for Android is still in its early stages. Currently, there are numerous tools and configuration steps required before building. As the project matures, we can expect this process to become more streamlined and developer-friendly.

That said, you now have everything you need to start experimenting: the SDK, the Swift Android Gradle Plugin, and a sample project to learn from. The foundation is here—now it's time to build.

Happy coding! 😎

Resources