-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Add C++ to Swift to Android integration example #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NakaokaRei
wants to merge
4
commits into
swiftlang:main
Choose a base branch
from
NakaokaRei:feat/hello-cpp-swfit-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,18 @@ | ||
| *.iml | ||
| .gradle | ||
| **/.gradle/ | ||
| /local.properties | ||
| /.idea | ||
| .DS_Store | ||
| /build | ||
| build/ | ||
| /captures | ||
| .externalNativeBuild | ||
| .cxx | ||
| **/.cxx/ | ||
| local.properties | ||
| jniLibs | ||
|
|
||
| # C++ build artifacts | ||
| **/prebuilt/ | ||
|
|
||
| # Swift build artifacts | ||
| **/.build/ | ||
| **/Package.resolved |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # C++ to Swift to Android Integration | ||
|
|
||
| This example demonstrates how to call C++ code from Android through Swift. The app uses a C++ library that provides basic calculator functions (add and multiply), wraps them in Swift, and calls them from an Android Kotlin app. | ||
|
|
||
| ## Overview | ||
|
|
||
| The project is structured into three main parts: | ||
|
|
||
| 1. **`cpp-lib`**: A C++ library with basic calculator functions (`add` and `multiply`). This is built using CMake and packaged as an artifactbundle for consumption by Swift. | ||
|
|
||
| 2. **`swift-lib`**: A Swift package that wraps the C++ functions and exposes them to Android using [swift-java](https://github.com/swiftlang/swift-java). The Swift code calls the C++ functions and provides JNI bindings automatically. | ||
|
|
||
| 3. **`app`**: A standard Android application written in Kotlin that calls the Swift-wrapped C++ functions and displays the results. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you can build and run this project, you need to have the following installed: | ||
|
|
||
| * **Basic setup**: Follow the Prerequisites and Setup instructions in [hello-swift-java/README.md](../hello-swift-java/README.md) to install JDK, Swiftly, Swift SDK for Android, and publish the swift-java packages locally. | ||
| * **Android NDK**: Required to build the C++ library. Set the `ANDROID_NDK_HOME` environment variable to your NDK installation path. | ||
|
|
||
| ## Setup and Configuration | ||
|
|
||
| ### 1. Build the C++ Library | ||
|
|
||
| Before building the Android app, you need to build the C++ library: | ||
|
|
||
| ```bash | ||
| cd hello-cpp-swift/cpp-lib | ||
| ./build-android-static.sh | ||
| ``` | ||
|
|
||
| This will create the `prebuilt/HelloWorldCpp.artifactbundle` directory containing the compiled C++ static libraries for all Android architectures (arm64-v8a, armeabi-v7a, x86_64). | ||
|
|
||
| ## Running the example | ||
|
|
||
| 1. Open the `swift-android-examples` project in Android Studio. | ||
|
|
||
| 2. Select the `hello-cpp-swift:app` Gradle target. | ||
|
|
||
| 3. Run the app on an Android emulator or a physical device. | ||
|
|
||
| 4. The app will display the results of C++ calculations (10 + 5 and 10 × 5) called through Swift. | ||
|
|
||
| ## Building from command line | ||
|
|
||
| ```bash | ||
| # From the project root directory | ||
| ./gradlew :hello-cpp-swift:app:assembleDebug | ||
|
|
||
| # Install on device/emulator | ||
| ./gradlew :hello-cpp-swift:app:installDebug | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. **C++ Layer** (`cpp-lib/src/calculator.cpp`): | ||
| ```cpp | ||
| int add(int a, int b) { | ||
| return a + b; | ||
| } | ||
| ``` | ||
|
|
||
| 2. **Swift Layer** (`swift-lib/Sources/HelloCppSwift/Calculator.swift`): | ||
| ```swift | ||
| import HelloWorldCpp | ||
|
|
||
| public func addNumbers(_ a: Int32, _ b: Int32) -> Int32 { | ||
| return add(a, b) | ||
| } | ||
| ``` | ||
|
|
||
| 3. **Android/Kotlin Layer** (`app/MainActivity.kt`): | ||
| ```kotlin | ||
| val sum = com.example.hellocppswift.HelloCppSwift.addNumbers(10, 5) | ||
| ``` | ||
|
|
||
| The Swift code is automatically wrapped with JNI bindings using the `swift-java` JExtract plugin, making it callable from Kotlin/Java code. | ||
|
|
||
| ## Rebuilding the C++ Library | ||
|
|
||
| If you make changes to the C++ code, you need to rebuild the artifactbundle: | ||
|
|
||
| ```bash | ||
| cd hello-cpp-swift/cpp-lib | ||
| ./build-android-static.sh | ||
| ``` | ||
|
|
||
| Then rebuild the Android app to pick up the changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.application) | ||
| alias(libs.plugins.kotlin.android) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.hellocppswift" | ||
| compileSdk = 36 | ||
|
|
||
| defaultConfig { | ||
| applicationId = "com.example.hellocppswift" | ||
| minSdk = 28 | ||
| targetSdk = 36 | ||
| versionCode = 1 | ||
| versionName = "1.0" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| } | ||
| } | ||
|
|
||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11) | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.androidx.core.ktx) | ||
| implementation(libs.androidx.appcompat) | ||
| implementation(libs.material) | ||
| implementation(libs.androidx.constraintlayout) | ||
| implementation("org.swift.swiftkit:swiftkit-core:1.0-SNAPSHOT") | ||
| implementation(project(":hello-cpp-swift:swift-lib")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <application | ||
| android:allowBackup="true" | ||
| android:label="@string/app_name" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> | ||
| <activity | ||
| android:name=".MainActivity" | ||
| android:exported="true"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| </application> | ||
|
|
||
| </manifest> |
32 changes: 32 additions & 0 deletions
32
hello-cpp-swift/app/src/main/java/com/example/hellocppswift/MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.example.hellocppswift | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.Gravity | ||
| import android.widget.FrameLayout | ||
| import android.widget.TextView | ||
| import androidx.appcompat.app.AppCompatActivity | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| val textView = TextView(this).apply { | ||
| textSize = 24f | ||
| textAlignment = TextView.TEXT_ALIGNMENT_CENTER | ||
| setPadding(32, 32, 32, 32) | ||
| gravity = Gravity.CENTER | ||
| } | ||
|
|
||
| val sum = com.example.hellocppswift.HelloCppSwift.addNumbers(10, 5) | ||
| val product = com.example.hellocppswift.HelloCppSwift.multiplyNumbers(10, 5) | ||
|
|
||
| textView.text = "C++ via Swift Calculations:\n\n10 + 5 = $sum\n10 × 5 = $product" | ||
|
|
||
| val container = FrameLayout(this).apply { | ||
| setPadding(0, 200, 0, 0) | ||
| addView(textView) | ||
| } | ||
|
|
||
| setContentView(container) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <resources> | ||
| <string name="app_name">Hello C++ Swift</string> | ||
| </resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| cmake_minimum_required(VERSION 3.18) | ||
| project(HelloWorldCpp VERSION 1.0.0 LANGUAGES CXX) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
|
||
| set(SOURCES | ||
| src/calculator.cpp | ||
| ) | ||
|
|
||
| set(HEADERS | ||
| include/calculator.h | ||
| ) | ||
|
|
||
| add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) | ||
|
|
||
| target_include_directories(${PROJECT_NAME} | ||
| PUBLIC | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include> | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| cd "$SCRIPT_DIR" | ||
|
|
||
| if [ -z "$ANDROID_NDK_HOME" ]; then | ||
| echo "Error: ANDROID_NDK_HOME environment variable is not set" | ||
| echo "Please set it to your Android NDK installation path" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -d "$ANDROID_NDK_HOME" ]; then | ||
| echo "Error: ANDROID_NDK_HOME points to non-existent directory: $ANDROID_NDK_HOME" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ANDROID_TOOLCHAIN="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" | ||
|
|
||
| if [ ! -f "$ANDROID_TOOLCHAIN" ]; then | ||
| echo "Error: Android toolchain file not found at: $ANDROID_TOOLCHAIN" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ANDROID_API=28 | ||
|
|
||
| ABIS=( | ||
| "arm64-v8a:android-aarch64" | ||
| "armeabi-v7a:android-armv7" | ||
| "x86_64:android-x86_64" | ||
| ) | ||
|
|
||
| echo "Building HelloWorldCpp static libraries for Android..." | ||
|
|
||
| for ABI_ENTRY in "${ABIS[@]}"; do | ||
| ABI="${ABI_ENTRY%%:*}" | ||
| echo "Building for $ABI..." | ||
|
|
||
| BUILD_DIR="build/android-static/$ABI" | ||
| mkdir -p "$BUILD_DIR" | ||
|
|
||
| cmake -S . -B "$BUILD_DIR" \ | ||
| -DCMAKE_TOOLCHAIN_FILE="$ANDROID_TOOLCHAIN" \ | ||
| -DANDROID_ABI="$ABI" \ | ||
| -DANDROID_PLATFORM="android-$ANDROID_API" \ | ||
| -DCMAKE_BUILD_TYPE=Release \ | ||
| -DCMAKE_CXX_STANDARD=17 | ||
|
|
||
| cmake --build "$BUILD_DIR" --config Release | ||
|
|
||
| echo "✓ Built $ABI" | ||
| done | ||
|
|
||
| echo "" | ||
| echo "Creating artifactbundle..." | ||
|
|
||
| ARTIFACTBUNDLE_DIR="prebuilt/HelloWorldCpp.artifactbundle" | ||
|
|
||
| for ABI_ENTRY in "${ABIS[@]}"; do | ||
| ABI="${ABI_ENTRY%%:*}" | ||
| BUNDLE_DIR="${ABI_ENTRY##*:}" | ||
|
|
||
| mkdir -p "$ARTIFACTBUNDLE_DIR/$BUNDLE_DIR/Headers" | ||
|
|
||
| cp "build/android-static/$ABI/libHelloWorldCpp.a" "$ARTIFACTBUNDLE_DIR/$BUNDLE_DIR/" | ||
|
|
||
| cp include/calculator.h "$ARTIFACTBUNDLE_DIR/$BUNDLE_DIR/Headers/" | ||
| cp include/module.modulemap "$ARTIFACTBUNDLE_DIR/$BUNDLE_DIR/Headers/" | ||
|
|
||
| echo "✓ Created artifactbundle for $BUNDLE_DIR" | ||
| done | ||
|
|
||
| cat > "$ARTIFACTBUNDLE_DIR/info.json" << 'EOF' | ||
| { | ||
| "schemaVersion": "1.0", | ||
| "artifacts": { | ||
| "HelloWorldCpp": { | ||
| "version": "1.0.0", | ||
| "type": "staticLibrary", | ||
| "variants": [ | ||
| { | ||
| "path": "android-aarch64/libHelloWorldCpp.a", | ||
| "supportedTriples": ["aarch64-unknown-linux-android"], | ||
| "staticLibraryMetadata": { | ||
| "headerPaths": ["android-aarch64/Headers"], | ||
| "moduleMapPath": "android-aarch64/Headers/module.modulemap" | ||
| } | ||
| }, | ||
| { | ||
| "path": "android-armv7/libHelloWorldCpp.a", | ||
| "supportedTriples": ["armv7-unknown-linux-android"], | ||
| "staticLibraryMetadata": { | ||
| "headerPaths": ["android-armv7/Headers"], | ||
| "moduleMapPath": "android-armv7/Headers/module.modulemap" | ||
| } | ||
| }, | ||
| { | ||
| "path": "android-x86_64/libHelloWorldCpp.a", | ||
| "supportedTriples": ["x86_64-unknown-linux-android"], | ||
| "staticLibraryMetadata": { | ||
| "headerPaths": ["android-x86_64/Headers"], | ||
| "moduleMapPath": "android-x86_64/Headers/module.modulemap" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| echo "✓ Generated info.json" | ||
| echo "" | ||
| echo "All Android static libraries built successfully!" | ||
| echo "Artifactbundle created at: $ARTIFACTBUNDLE_DIR" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #ifndef CALCULATOR_H | ||
| #define CALCULATOR_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| int add(int a, int b); | ||
| int multiply(int a, int b); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module HelloWorldCpp { | ||
| header "calculator.h" | ||
| export * | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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