Swift Android Gradle Plugin

Software Engineer
Search for a command to run...

Software Engineer
No comments yet. Be the first to comment.
In my previous article, I introduced the Swift Android Gradle Plugin that simplifies integrating Swift code into Android projects. To demonstrate the plugin in action, I built a sample Android app that generates fractal images using Swift and renders...

Taking Flight with Android XR: Building a Jetliner Demo I spent the last few days exploring Android XR, Google's latest platform for extended reality applications. After working through the documentation and experimenting with the SDK, I built a demo...

This article shows you how to eliminate the rebuild-test cycle when working with Android preferences. Pref Editor lets you modify values instantly.

The Iterator design pattern is a well-known and often-used pattern. It allows us to access elements in a collection without the need to know about the underlying storage details. We get this functionality from an object known as an iterator of the co...

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

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:
Swift Toolchain: The core binaries needed to build and run Swift code
Swift SDK for Android: Contains all the resources needed to generate and run Swift code for Android (this is what was recently released)
Android Native Development Kit (NDK): Cross-compilation tools for native Android development, widely used for building C/C++ code for Android
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.
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.

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")
}
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! 😎