Cross-compiling for Android with CMake

Hi,

I'm reviving this:

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!

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. Examining the generated toolchain file in a local Fedora x86_64 build shows me this:

> 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, 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:

> 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.

1 Like

Thanks, I will try and let you know.