Skip to content

Conversation

@NakaokaRei
Copy link

@NakaokaRei NakaokaRei commented Nov 24, 2025

This PR adds the hello-cpp-swift example, demonstrating how to call C++ code from Android through Swift.

The example shows:

  • Packaging a C++ library as an artifactbundle for Swift consumption
  • Importing the C++ library via .binaryTarget of Swift Package Manager
  • Exposing Swift-wrapped C++ functions to Android using swift-java for automatic JNI generation

This pattern is useful for integrating existing C++ libraries into Android applications with Swift as an intermediate
layer, leveraging swift-java's automatic bridging capabilities.

@finagolfin
Copy link
Member

Thanks, we'll take a look. 👍

@marcprux
Copy link
Contributor

Very neat! Out of curiosity, why do you have the C++ project use a separate CMakeLists.txt and build-android-static.sh script, rather than just including the C++ in a Swift module and letting SwiftPM build it?

I was able to successfully do this in another sample (in a separate repository) at https://github.com/swift-android-sdk/swift-android-samples/tree/main/cpp-demo

@NakaokaRei
Copy link
Author

Thanks for taking a look @marcprux

Out of curiosity, why do you have the C++ project use a separate #24 (comment) and #24 (comment) script, rather than just including the C++ in a Swift module and letting SwiftPM build it?

That's good question. Many existing C++ projects already use CMake to manage targets and library dependencies, and retrofitting a Package.swift into them can be quite challenging. That's why we've opted for a setup where we pre-build the C++ library as a binary. In fact, I think this approach is more common in practice than having Swift PM build the C++ code directly.

#define CALCULATOR_H

#ifdef __cplusplus
extern "C" {
Copy link
Author

@NakaokaRei NakaokaRei Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now although I used extern "C" to wrap C++ function since the build while enabling C++ interop feature is failed, once following PR is merged, Im going to remove this and to enable C++ interop in other PR!

swiftlang/swift-java#463

@dschaefer2
Copy link
Member

Thanks for taking a look @marcprux

Out of curiosity, why do you have the C++ project use a separate #24 (comment) and #24 (comment) script, rather than just including the C++ in a Swift module and letting SwiftPM build it?

That's good question. Many existing C++ projects already use CMake to manage targets and library dependencies, and retrofitting a Package.swift into them can be quite challenging. That's why we've opted for a setup where we pre-build the C++ library as a binary. In fact, I think this approach is more common in practice than having Swift PM build the C++ code directly.

This is an area I'm investigating for SwiftPM, how to integrate CMake built libraries into the SwiftPM build more naturally and more directly. It would be awesome if SwiftPM could call out to an external build system to produce artifacts that it can then integrate into its own build. Someone even suggested SwiftPM could load up the CMake metadata and build those targets itself. But the main point is that this is a use case we really want to support. This is another great example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants