Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Swift Android Examples

This repository contains sample apps that use the [Swift Android SDK](https://www.swift.org/).
This repository contains sample applications that demonstrate how to use Swift with Android development using the [Swift Android SDK](https://www.swift.org/). Each sample showcases different integration patterns and use cases for Swift on Android.

# Build and run
## Available Samples

1. Setup Swift Android SDK
2. Clone this repository
3. Open the whole project in Android Studio
4. Select the sample you want to run in the top bar (you may need to sync gradle first)
5. Click the play button to run the sample

You can also build the samples from the command line if you prefer. Use `./gradlew` build to build everything. For individual tasks, see `./gradlew tasks`. To see the tasks for an individual sample, run the tasks task for that directory. For example, `./gradlew :hello-swift:tasks` will show the tasks for the hello-swift app.

You can build all sample apps for both the debug and release build types by running `./gradlew assemble`.
- **[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.
- **[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).
38 changes: 38 additions & 0 deletions hello-swift-callback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Swift to Android Callbacks

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

![Screenshot](screenshot.png)

## Overview

The project consists of:

1. **Android App**: A Kotlin activity that starts/stops a timer and displays the current time. It includes a callback method `updateTimer()` that gets called from Swift.
2. **Swift Code**: Implements a background timer using `DispatchQueue` that calls back to the Android activity every second to update the timer display.

## Prerequisites

Before you can build and run this project, you need to have the following installed:

* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
* **Swift SDK for Android**: You need to install the [Swift Android SDK](https://github.com/swift-android-sdk/swift-android-sdk/releases)

## Running the example

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

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

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

## Building from command line

```bash
# Build the sample
./gradlew :hello-swift-callback:assembleDebug

# Install on device/emulator
./gradlew :hello-swift-callback:installDebug
```
Binary file added hello-swift-callback/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions hello-swift-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.

## Overview

The project consists of:

1. **Swift Library**: A `SwiftLibrary` class that declares external Swift functions and loads the native library.
2. **Swift Implementation**: Implements the library functions using JNI conventions for consumption by other Android projects.

## Prerequisites

Before you can build and run this project, you need to have the following installed:

* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
* **Swift SDK for Android**: You need to install the [Swift Android SDK](https://github.com/swift-android-sdk/swift-android-sdk/releases)

## Building the library

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

2. Select the `hello-swift-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

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

After a successful build, the Android library will be located at `hello-swift-library/build/outputs/aar/hello-swift-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')
```
3. Use the `SwiftLibrary` class in your code
38 changes: 38 additions & 0 deletions hello-swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.

![Screenshot](screenshot.png)

## Overview

The project consists of:

1. **Android App**: A simple Kotlin activity that declares an external function `stringFromSwift()` and loads the native library.
2. **Swift Code**: Implements the function using `@_cdecl` attribute to match JNI naming convention, returning a greeting message.

## Prerequisites

Before you can build and run this project, you need to have the following installed:

* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
* **Swift SDK for Android**: You need to install the [Swift Android SDK](https://github.com/swift-android-sdk/swift-android-sdk/releases)

## Running the example

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

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

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

## Building from command line

```bash
# Build the sample
./gradlew :hello-swift:assembleDebug

# Install on device/emulator
./gradlew :hello-swift:installDebug
```
Binary file added hello-swift/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions native-activity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Full Native Activity

This example demonstrates a complete native Android activity written entirely in Swift. It showcases Android integration including OpenGL ES rendering, native app lifecycle management, and the Android Native App Glue framework.

![Screenshot](screenshot.png)

## Overview

The project consists of:

1. **Native Activity**: A complete Swift implementation that creates an OpenGL ES context and renders a continuously changing colored background.
2. **Android Native App Glue**: Uses the Android Native App Glue framework to handle Android lifecycle and integrates with Choreographer for smooth rendering at 60fps.

## Prerequisites

Before you can build and run this project, you need to have the following installed:

* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
* **Swift SDK for Android**: You need to install the [Swift Android SDK](https://github.com/swift-android-sdk/swift-android-sdk/releases)
* **Device/emulator with OpenGL ES support**

## Running the example

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

2. Select the `native-activity` Gradle target.

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

## Building from command line

```bash
# Build the sample
./gradlew :native-activity:assembleDebug

# Install on device/emulator
./gradlew :native-activity:installDebug
```
Binary file added native-activity/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions swift-android.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import org.gradle.api.file.DuplicatesStrategy

// Configuration class for Swift builds
data class SwiftConfig(
var apiLevel: Int = 29, // Default API level
var apiLevel: Int = 28, // Default API level
var debugAbiFilters: Set<String> = setOf("arm64-v8a"),
var debugExtraBuildFlags: List<String> = emptyList(),
var releaseAbiFilters: Set<String> = setOf("arm64-v8a", "armeabi-v7a", "x86_64"),
var releaseExtraBuildFlags: List<String> = emptyList(),
var swiftlyPath: String? = null, // Optional custom swiftly path
var swiftSDKPath: String? = null, // Optional custom Swift SDK path
var swiftVersion: String = "6.2.0", // Swift version
var androidSdkVersion: String = "6.2-RELEASE-android-0.1" // SDK version
var swiftVersion: String = "main-snapshot-2025-10-16", // Swift version
var androidSdkVersion: String = "DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1" // SDK version
Copy link
Member

Choose a reason for hiding this comment

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

Use latest Oct. 17 tag instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ve synced it with @madsodgaard example
I think we should update it in a separate PR across all examples

)

// Architecture definitions
Expand Down