# Cross-compiling for Android with CMake

**URL:** <https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924>\
**Category:** Android\
**Tags:** cmake, swiftpm\
**Created:** [October 29, 2025, 10:23am UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924 "2025-10-29T10:23:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [October 29, 2025, 10:23am UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/1 "2025-10-29T10:23:21Z")

</div>

Hi,

I'm reviving this:

> [@How to use swiftc with a static toolchain in cmake](https://forums.swift.org/t/how-to-use-swiftc-with-a-static-toolchain-in-cmake/74266/):
>
> I have installed the static toolchain according to: [Swift.org - Getting Started with the Static Linux SDK](https://www.swift.org/documentation/articles/static-linux-getting-started.html) The examples work. I now want to embed swift into my cmake project, for this, and I want to set the flags for swiftc correctly: find\_program(SWIFTC\_EXE NAMES "swiftc") set(CMAKE\_Swift\_COMPILER "${SWIFTC\_EXE}") set(CMAKE\_Swift\_FLAGS "-sdk /root/.swiftpm/swift-sdks/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-07-02-a\_static-linux-0.0.1.artifactbundle/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-07-02-a\_static…

To bring up more or less the same question. Is there a _solid_ way to cross-compile for Android with a Swift SDK without using SwiftPM? If not, is there any work towards it?

So far, I couldn't find a single way to make swiftc work with the Swift Android SDK, so I wonder if this is even possible. CMake is unavoidable for complex mixed-language libraries, so I need to resort to two build systems, which is a maintenance burden. I'd love to avoid SwiftPM, but I can't find a way that's not by rebuilding the entire Swift toolchain for Android.

Thanks!

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [October 29, 2025, 1:28pm UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/2 "2025-10-29T13:28:42Z")

</div>

Did you create a toolchain file, as Evan detailed there? The Swift toolchain build itself currently [generates a CMake toolchain file that works for exactly this purpose, including for Android](https://github.com/swiftlang/swift/blob/09a751f6989b0fd01351408e7a7de0c8c7417044/utils/swift_build_support/swift_build_support/products/product.py#L398). Examining the generated toolchain file in a local Fedora x86\_64 build shows me this:

```swift
> cat cmark-linux-x86_64/BuildScriptToolchain.cmake
set(CMAKE_CXX_COMPILER_TARGET x86_64-unknown-linux-gnu)
set(CMAKE_C_COMPILER_TARGET x86_64-unknown-linux-gnu)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_Swift_COMPILER_TARGET x86_64-unknown-linux-gnu)

```

Switching out the similar Android triple and target config, plus adding `CMAKE_SYSTEM_VERSION` and `CMAKE_ANDROID_NDK` as shown in my above link, will get you most of the way there, but the Swift compiler also needs to know the location of the Android NDK sysroot and the Swift resource directory in your Swift SDK for Android, which can be passed in using `CMAKE_Swift_FLAGS`.

You will want to pass in [the same Swift flags shown in the Android doc to cross-compile hello world with just a stdlib](https://github.com/swiftlang/swift/blob/main/docs/Android.md#2-compiling-helloswift-to-run-on-an-android-device), except the Swift `-resource-dir` is now in the trunk SDK bundle instead, at `swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1.artifactbundle/swift-android/swift-resources/usr/lib/swift-aarch64`.

You will probably also have to link that bundle's clang resource directory to your NDK clang resource directory, which is one of the steps the `setup-android-sdk.sh` script we provide does for you when used with SwiftPM:

```swift
> ln -sf ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18 swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1.artifactbundle/swift-android/swift-resources/usr/lib/swift/clang

```

I know it seems complicated, but that's why SwiftPM saves you so much trouble, particularly since CMake's Swift support is still evolving.

We had some trouble with SwiftPM also, as this is the first Swift SDK bundle not to ship with the C/C++ headers and libraries for the target platform included, ie you have to download the Android C/C++ NDK separately. There are some Swift configuration bugs with such an external C/C++ platform SDK that we avoid with that `setup-android-sdk.sh` script. I'm looking at fixing those now and hopefully we can remove that script in the coming months.

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [October 29, 2025, 2:30pm UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/3 "2025-10-29T14:30:39Z")

</div>

Thanks, I will try and let you know.

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [November 16, 2025, 8:41am UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/4 "2025-11-16T08:41:50Z")

</div>

I tried this CMake config recently and was able to successfully cross-compile the swift-argument-parser package for Android using the official trunk snapshot of the Swift SDK for Android, so let me know if you have any problems.

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [November 17, 2025, 3:36pm UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/5 "2025-11-17T15:36:21Z")

</div>

> [@Finagolfin](#):
>
> I tried this CMake config recently and was able to successfully cross-compile the swift-argument-parser package for Android using the official trunk snapshot of the Swift SDK for Android, so let me know if you have any problems.

Great, and thanks for the update. I'm working on something else right now, and I haven't had the time to try it yet (my setup is not trivial enough to give it a quick shot), but I'll post back here as soon as I have a chance.

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [November 24, 2025, 12:33am UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/6 "2025-11-24T00:33:33Z")

</div>

Man, I did it. Thank you immensely for your support. I will report more details once I wrap up and push the code to my repo.

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [November 24, 2025, 7:03am UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/7 "2025-11-24T07:03:03Z")

</div>

Great, glad to hear that. 😃

Been following your further reports of your [cross-platform use of Swift on your blog](https://davidederosa.com/), keep blogging! 👍

In your latest post, [you say that](https://davidederosa.com/cross-platform-swift/build-system-part-two/) the `--static-swift-stdlib` flag "doesn’t help with Foundation either because it’s not part of the standard Swift library," but that isn't true, at least on linux and Android: the flag is misnamed and will try to statically link _all_ Swift runtime libraries, eg [XCTest too if you're testing](https://github.com/swiftlang/swift-package-manager/issues/5590). We should probably rename the flag to describe what it actually does.

Regarding the broader issues you see with C/C++ interfacing or other SwiftPM difficulties, I encourage you to file your specific error output under [existing issues or open your own](https://github.com/swiftlang/swift-package-manager/issues). There are plans for better CMake integration and so on that would benefit from your concrete feedback.

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [November 25, 2025, 7:52pm UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/8 "2025-11-25T19:52:40Z")

</div>

> [@Finagolfin](#):
>
> In your latest post, [you say that](https://davidederosa.com/cross-platform-swift/build-system-part-two/) the `--static-swift-stdlib` flag "doesn’t help with Foundation either because it’s not part of the standard Swift library," but that isn't true, at least on linux and Android: the flag is misnamed and will try to statically link _all_ Swift runtime libraries, eg [XCTest too if you're testing](https://github.com/swiftlang/swift-package-manager/issues/5590). We should probably rename the flag to describe what it actually does.

Good to know, but I prefer to stay clear of anything that has inconsistent behavior across platforms, and static linking appears to be one of those things. I'd rather not rely on it. In fact, I intend to get rid of Foundation soon.

> [@Finagolfin](#):
>
> Regarding the broader issues you see with C/C++ interfacing or other SwiftPM difficulties, I encourage you to file your specific error output under [existing issues or open your own](https://github.com/swiftlang/swift-package-manager/issues). There are plans for better CMake integration and so on that would benefit from your concrete feedback.

You know, I wouldn't like to report issues without being 100% sure that there _is_ an issue, and gaining such certainty can be very time-consuming in experimental endeavors. Writing those posts is my sweet spot between yapping and reporting official issues, but the need for my project to be production-ready _today_ leaves me with very little room for further analysis, as in "I can't afford to figure out" why SwiftPM doesn't work for my use case. In a sense, picking CMake over SwiftPM is a necessity more than a preference.

Nevertheless, you can tell from my convoluted `Package.swift` how much I experimented with it before throwing in the towel. :D

---

<div class="post-metadata">

**Author:** ![keeshux](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keeshux/32/40485_2.png) [@keeshux](https://forums.swift.org/u/keeshux)\
**Post date:** [November 25, 2025, 8:46pm UTC](https://forums.swift.org/t/cross-compiling-for-android-with-cmake/82924/9 "2025-11-25T20:46:08Z")

</div>

Back on topic, let's see if this helps others looking to resolve a similar problem.

## Context

To give a better context, [my library](https://github.com/partout-io/partout) is a mixed Swift/C codebase, cross-platform, targeted at all supported Swift platforms, including Android with the proper Swift SDK. The library is quite complex in that it has several third-party dependencies, written in other languages (C, C++, Go, and whatnot), and built with other build systems. SwiftPM makes this tricky, so I resorted to CMake.

Swift SDKs suit SwiftPM best. Initially, I built third parties with CMake, and the Swift side with SwiftPM plus an insane amount of unsafe flags. This was obnoxious, hence the question of this post.

## Toolchain file

With the suggestions from @Finagolfin, I drafted the following toolchain file for CMake, feel free to reuse it. For example, `aarch64` could also be made a variable:

> <https://github.com/partout-io/partout/blob/master/toolchains/android.toolchain.cmake>

The toolchain relies on a few environment variables:

```swift
ANDROID_NDK_ROOT=<path_to_your_ndk>
ANDROID_NDK_ARCH
ANDROID_NDK_API
ANDROID_NDK_TOOLCHAIN
ANDROID_NDK_SYSROOT
SWIFT_RESOURCE_DIR

```

But they are partly inferred from [a build script](https://github.com/partout-io/partout/blob/master/scripts/build.sh). The actual input is:

```swift
ANDROID_NDK_ROOT=<path_to_your_ndk>
ANDROID_NDK_API=28
ANDROID_NDK_ARCH=aarch64
ANDROID_NDK_TARGET=darwin-x86_64
SWIFT_ANDROID_SDK=~/.swiftpm/swift-sdks/swift-6.2-RELEASE-android-0.1.artifactbundle

```

### Swiftly

I develop on macOS, and when I use the Swift for Android SDK, I hit compatibility issues with the `swiftc` compiler from Xcode. My workaround is to use `swiftly run swiftc` instead, but for this to work I had to feed a simple wrapper script to CMake:

> <https://github.com/partout-io/partout/blob/master/CMakeLists.txt#L37C9-L37C85>

## Superproject

The CMake project is a superbuild made of these subprojects:

- The core Swift/C library, which maintains the Sources/Tests layout for interop with SwiftPM
- OpenSSL (C)
- WireGuard (Go)
- Wintun (a prebuilt .dll for Windows)

The Android toolchain defines the `ANDROID` variable so that each third party can act accordingly. For example, OpenSSL requires additional flags for Android targets:

```swift
if(ANDROID)
    set(OPENSSL_TARGET "android-arm64") # I could hardcode arm64 here
    set(OPENSSL_SYMBOLS "-D __ANDROID_API__ =${CMAKE_SYSTEM_VERSION}")
else()
    set(OPENSSL_TARGET "")
    set(OPENSSL_SYMBOLS "")
endif()

```

With the same approach, I could eventually integrate my library into Passepartout, the app that uses it. Here I describe the overall layout:

> <https://github.com/partout-io/passepartout/pull/1616>
>
> After splitting app-apple and app-shared properly in #1612, the time has come to… draft a concrete starting point for the Windows/Linux app, with app-android already being set up for Android. In order to mix Swift with other languages and dependencies, and in a cross-platform fashion, the preferred build system is \*\*CMake\*\*. SwiftPM is not convenient yet for such a complex build.
> 
> \## Business logic
> 
> The cross-platform business logic is written in Swift, and lies fully in the \*\*app-shared\*\* folder. This library layer internally depends on Partout and will provide:
> 
> \- A direct Swift ABI to Apple frontends (SwiftUI)
> \- C/JNI bindings to the ABI for non-Swift frontends (Jetpack Compose and wxWidgets)
> 
> \## Apps
> 
> There are three frontend targets:
> 
> \- \*\*app-apple\*\*: (Xcode + SwiftPM) SwiftUI app built with a few Apple-specific targets, then app-shared, and Partout. Third parties (OpenSSL, WireGuard, etc.) come from Partout as XCFrameworks.
> \- \*\*app-android\*\*: (Android Studio + CMake) Depends on Swift up to app-shared (ABI), Jetpack Compose from the ABI up. The ABI is deployed to the app as a dynamic library, then Kotlin takes over at the upper layers.
> \- \*\*app-cross\*\*: (CMake) Like app-android, it depends on app-shared. The app is built together with the ABI and therefore statically linked. The UI is implemented with wxWidgets, which additionally depends on GTK+ on Linux. Windows requires no extra dependencies.
> 
> \## Distribution
> 
> The CMake project generates the output to \`bin/\<platform-arch\>\`, where third parties are distributed as dynamic libraries. The Windows/Linux apps are also generated in the \`app\` subfolder, with CMake also bundling the local Swift runtime. Wintun is also bundled on Windows. 
> 
> Ideally, the \`bin/\<platform-arch\>/app\` output folder includes all the required libraries to run the app on a clean Linux/Windows installation. This was only tested on arm64 machines.
> 
> \## Gotchas
> 
> Not sure about some of these:
> 
> \- Android: Reuse the CMake toolchain and swiftly wrapper from Partout. Make sure to define the required environment variables in \`env.sh\`.
> \- Windows: Use VC 2022 generator for wxWidgets, Ninja is problematic. Force release to avoid the "d" suffix.
> \- Windows: Do not strip the executable, it will break.
> \- Windows: Distribute OpenSSL DLLs from the "bin" folder, not "lib".
> \- Windows: Link to absolute library paths to cope with the inconsitent "lib" prefix.
> \- Linux: Add \`$ORIGIN\` to the runpath to ensure proper library lookup without LD\_LIBRARY\_PATH.
> \- macOS (Development): Importing Partout as a Swift module in app-shared requires the modulemaps of all C modules in the headers search paths.
> \- Not sure at all about wxWidgets compile/linking flags, but they will be clearer along the way.

The Android project is just a stub, yet it has a working example of how the Swift library is invoked via JNI and `@_cdecl`. Look for `passepartoutExampleSum` and `passepartoutExampleJSON` to get into a deep rabbit hole. 🙂
