Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ This repository contains example applications that demonstrate how to use the [S

## Available Examples

- **[hello-swift](hello-swift/)** - basic Swift integration that calls a Swift function.
- **[hello-swift-callback](hello-swift-callback/)** - demonstrates bidirectional communication with Swift timer callbacks updating Android UI.
- **[hello-swift-library](hello-swift-library/)** - shows how to package Swift code as a reusable Android library component.
Examples using [swift-java](https://github.com/swiftlang/swift-java) to generate necessary Swift/Java bridging:
- **[hello-swift-java](hello-swift-java/)** - application that demonstrates how to call Swift code from an Android app with automatically generated Java wrappers and JNI code using [swift-java](https://github.com/swiftlang/swift-java).

Examples using raw JNI, without generated bridging sources:
- **[hello-swift-raw-jni](hello-swift-raw-jni/)** - basic Swift integration that calls a Swift function.
- **[hello-swift-raw-jni-callback](hello-swift-raw-jni-callback/)** - demonstrates bidirectional communication with Swift timer callbacks updating Android UI.
- **[hello-swift-raw-jni-library](hello-swift-raw-jni-library/)** - shows how to package Swift code as a reusable Android library component.
- **[native-activity](native-activity/)** - complete native Android activity with OpenGL ES rendering written entirely in Swift.
- **[swift-java-hashing-example](swift-java-hashing-example/)** - application that demonstrates how to call Swift code from an Android app with automatically generated Java wrappers and JNI code using [swift-java](https://github.com/swiftlang/swift-java).
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# swift-java on Android

This example contains a sample Android application that demonstrates how to call Swift code from a Android app.
The example consists of an Android application (`hashing-app`) and a Swift library (`hashing-lib`) that performs a SHA256 hash on a given string.
The Swift library uses [swift-java](https://github.com/swiftlang/swift-java) and the new JNI mode to automatically
generate Java wrappers for calling into the Swift library.

The example consists of an Android application (`hashing-app`) and a Swift library (`hashing-lib`) that performs a SHA256 hash on a given string. The Swift library uses [swift-java](https://github.com/swiftlang/swift-java) and the new JNI mode to **automatically generate Java wrappers** for calling into the Swift library.

![IDE Screenshot](resources/ide.png)

Expand All @@ -27,7 +26,7 @@ Before you can build and run this project, you need to have the following instal

### Prepare Swift Android SDK and matching Swift

Currently these examples utilize very recent nightly Swift Android SDK versions. In order to install these, you can use Swiftly (the Swift toolchain installer):
Currently, these examples utilize very recent nightly Swift Android SDK versions. In order to install these, you can use Swiftly (the Swift toolchain installer):

You can follow [these instructions](https://www.swift.org/documentation/articles/swift-sdk-for-android-getting-started.html) to install an appropriate Swift SDK for Android.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ android {
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
Expand All @@ -49,7 +48,7 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(project(":hashing-lib"))
implementation(project(":hello-swift-java-hashing-lib"))
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Swift to Android Callbacks
# Swift to Android Callbacks using Raw JNI

This example demonstrates bidirectional communication between Swift and Android. The Swift code runs a timer that calls back to Android every second to update the UI with the current time (HH:MM:SS format).
This example demonstrates bidirectional communication between Swift and Android, without the use of swift-java interoperability support, just by using raw JNI. The Swift code runs a timer that calls back to Android every second to update the UI with the current time (HH:MM:SS format).

![Screenshot](screenshot.png)

Expand All @@ -23,16 +23,16 @@ Before you can build and run this project, you need to have the following instal

1. Open the `swift-android-examples` project in Android Studio.

2. Select the `hello-swift-callback` Gradle target.
2. Select the `hello-swift-raw-jni-callback` Gradle target.

3. Run the app on an Android emulator or a physical device.

## Building from command line

```bash
# Build the example
./gradlew :hello-swift-callback:assembleDebug
./gradlew :hello-swift-raw-jni-callback:assembleDebug

# Install on device/emulator
./gradlew :hello-swift-callback:installDebug
./gradlew :hello-swift-raw-jni-callback:installDebug
```
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class MainActivity : AppCompatActivity() {
external fun stopTicks()

companion object {
// Used to load the 'hello-swift-callback' library on application startup.
// Used to load the 'hello-swift-raw-jni-callback' library on application startup.
init {
System.loadLibrary("hello-swift-callback")
System.loadLibrary("hello-swift-raw-jni-callback")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import PackageDescription

let package = Package(
name: "hello-swift-callback",
name: "hello-swift-raw-jni-callback",
products: [
.library(name: "hello-swift-callback", type: .dynamic, targets: ["hello-swift-callback"]),
.library(name: "hello-swift-raw-jni-callback", type: .dynamic, targets: ["hello-swift-raw-jni-callback"]),
],
targets: [
.target(name: "hello-swift-callback")
.target(name: "hello-swift-raw-jni-callback")
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct JMethodID: @unchecked Sendable {
let id: jmethodID
}

let queue = DispatchQueue(label: "hello-swift-callback")
let queue = DispatchQueue(label: "hello-swift-raw-jni-callback")
var workItem: DispatchWorkItem? = nil
var activityRef: jobject? = nil

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Swift as Android Library

This example demonstrates how to package Swift code as a reusable Android library. It shows how to create a Swift library that can be consumed by other Android applications, making Swift functionality available as a standard Android library component.
This example demonstrates how to package Swift code as a reusable Android library, without the use of swift-java interoperability support, just by using raw JNI. It shows how to create a Swift library that can be consumed by other Android applications, making Swift functionality available as a standard Android library component.

## Overview

Expand All @@ -21,27 +21,27 @@ Before you can build and run this project, you need to have the following instal

1. Open the `swift-android-examples` project in Android Studio.

2. Select the `hello-swift-library` Gradle target.
2. Select the `hello-swift-raw-jni-library` Gradle target.

3. Build the library (it doesn't have a runnable app).

## Building from command line

```bash
# Build the library
./gradlew :hello-swift-library:assembleDebug
./gradlew :hello-swift-raw-jni-library:assembleDebug

# Build the AAR file
./gradlew :hello-swift-library:bundleReleaseAar
./gradlew :hello-swift-raw-jni-library:bundleReleaseAar
```

After a successful build, the Android library will be located at `hello-swift-library/build/outputs/aar/hello-swift-library-release.aar`.
After a successful build, the Android library will be located at `hello-swift-raw-jni-library/build/outputs/aar/hello-swift-raw-jni-library-release.aar`.

## Using the library in other projects

1. Copy the generated AAR file to your project's `libs/` directory
2. Add the dependency in your `build.gradle`:
```gradle
implementation files('libs/hello-swift-library-release.aar')
implementation files('libs/hello-swift-raw-jni-library-release.aar')
```
3. Use the `SwiftLibrary` class in your code
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SwiftLibrary {

companion object {
init {
System.loadLibrary("hello-swift-library")
System.loadLibrary("hello-swift-raw-jni-library")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import PackageDescription

let package = Package(
name: "hello-swift-library",
name: "hello-swift-raw-jni-library",
products: [
.library(name: "hello-swift-library", type: .dynamic, targets: ["hello-swift-library"]),
.library(name: "hello-swift-raw-jni-library", type: .dynamic, targets: ["hello-swift-raw-jni-library"]),
],
targets: [
.target(name: "hello-swift-library")
.target(name: "hello-swift-raw-jni-library")
]
)
8 changes: 4 additions & 4 deletions hello-swift/README.md → hello-swift-raw-jni/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Basic Swift Integration

This example demonstrates the most basic Swift integration pattern with Android. The app calls a Swift function that returns a "Hello from Swift ❤️" message and displays it in the Android UI.
This example demonstrates the most basic Swift integration pattern with Android, without the use of swift-java interoperability support, just by using raw JNI. The app calls a Swift function that returns a "Hello from Swift ❤️" message and displays it in the Android UI.

![Screenshot](screenshot.png)

Expand All @@ -23,16 +23,16 @@ Before you can build and run this project, you need to have the following instal

1. Open the `swift-android-examples` project in Android Studio.

2. Select the `hello-swift` Gradle target.
2. Select the `hello-swift-raw-jni` Gradle target.

3. Run the app on an Android emulator or a physical device.

## Building from command line

```bash
# Build the example
./gradlew :hello-swift:assembleDebug
./gradlew :hello-swift-raw-jni:assembleDebug

# Install on device/emulator
./gradlew :hello-swift:installDebug
./gradlew :hello-swift-raw-jni:installDebug
```
File renamed without changes.
File renamed without changes
20 changes: 13 additions & 7 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ dependencyResolutionManagement {
}

rootProject.name = "Swift Android Examples"
include(":hello-swift")
include(":hello-swift-callback")
include(":hello-swift-library")

// swift-java examples
include(":hello-swift-java-hashing-lib")
project(":hello-swift-java-hashing-lib").projectDir = file("hello-swift-java/hashing-lib")
include(":hello-swift-java-hashing-app")
project(":hello-swift-java-hashing-app").projectDir = file("hello-swift-java/hashing-app")

// raw-jni examples
include(":hello-swift-raw-jni")
include(":hello-swift-raw-jni-callback")
include(":hello-swift-raw-jni-library")

// native-only examples
include(":native-activity")
include(":hashing-lib")
project(":hashing-lib").projectDir = file("swift-java-hashing-example/hashing-lib")
include(":hashing-app")
project(":hashing-app").projectDir = file("swift-java-hashing-example/hashing-app")