Skip to content

Commit 72e2a21

Browse files
authored
Merge pull request #4 from swiftlang/update-documentation
Update documentation
2 parents be267e2 + 4b4b2af commit 72e2a21

File tree

10 files changed

+173
-15
lines changed

10 files changed

+173
-15
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# Swift Android Examples
22

3-
This repository contains sample apps that use the [Swift Android SDK](https://www.swift.org/).
3+
This repository contains example applications that demonstrate how to use the [Swift SDK for Android](https://swift.org/install). Each example showcases different integration patterns for Swift on Android.
44

5-
# Build and run
5+
## Available Examples
66

7-
1. Setup Swift Android SDK
8-
2. Clone this repository
9-
3. Open the whole project in Android Studio
10-
4. Select the sample you want to run in the top bar (you may need to sync gradle first)
11-
5. Click the play button to run the sample
12-
13-
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.
14-
15-
You can build all sample apps for both the debug and release build types by running `./gradlew assemble`.
7+
- **[hello-swift](hello-swift/)** - basic Swift integration that calls a Swift function.
8+
- **[hello-swift-callback](hello-swift-callback/)** - demonstrates bidirectional communication with Swift timer callbacks updating Android UI.
9+
- **[hello-swift-library](hello-swift-library/)** - shows how to package Swift code as a reusable Android library component.
10+
- **[native-activity](native-activity/)** - complete native Android activity with OpenGL ES rendering written entirely in Swift.
11+
- **[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).

hello-swift-callback/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Swift to Android Callbacks
2+
3+
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).
4+
5+
![Screenshot](screenshot.png)
6+
7+
## Overview
8+
9+
The project consists of:
10+
11+
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.
12+
2. **Swift Code**: Implements a background timer using `DispatchQueue` that calls back to the Android activity every second to update the timer display.
13+
14+
## Prerequisites
15+
16+
Before you can build and run this project, you need to have the following installed:
17+
18+
* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
19+
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
20+
* **Swift SDK for Android**: You need to install the [Swift SDK for Android](https://swift.org/install)
21+
22+
## Running the example
23+
24+
1. Open the `swift-android-examples` project in Android Studio.
25+
26+
2. Select the `hello-swift-callback` Gradle target.
27+
28+
3. Run the app on an Android emulator or a physical device.
29+
30+
## Building from command line
31+
32+
```bash
33+
# Build the example
34+
./gradlew :hello-swift-callback:assembleDebug
35+
36+
# Install on device/emulator
37+
./gradlew :hello-swift-callback:installDebug
38+
```
35.3 KB
Loading

hello-swift-library/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Swift as Android Library
2+
3+
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.
4+
5+
## Overview
6+
7+
The project consists of:
8+
9+
1. **Swift Library**: A `SwiftLibrary` class that declares external Swift functions and loads the native library.
10+
2. **Swift Implementation**: Implements the library functions using JNI conventions for consumption by other Android projects.
11+
12+
## Prerequisites
13+
14+
Before you can build and run this project, you need to have the following installed:
15+
16+
* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
17+
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
18+
* **Swift SDK for Android**: You need to install the [Swift SDK for Android](https://swift.org/install)
19+
20+
## Building the library
21+
22+
1. Open the `swift-android-examples` project in Android Studio.
23+
24+
2. Select the `hello-swift-library` Gradle target.
25+
26+
3. Build the library (it doesn't have a runnable app).
27+
28+
## Building from command line
29+
30+
```bash
31+
# Build the library
32+
./gradlew :hello-swift-library:assembleDebug
33+
34+
# Build the AAR file
35+
./gradlew :hello-swift-library:bundleReleaseAar
36+
```
37+
38+
After a successful build, the Android library will be located at `hello-swift-library/build/outputs/aar/hello-swift-library-release.aar`.
39+
40+
## Using the library in other projects
41+
42+
1. Copy the generated AAR file to your project's `libs/` directory
43+
2. Add the dependency in your `build.gradle`:
44+
```gradle
45+
implementation files('libs/hello-swift-library-release.aar')
46+
```
47+
3. Use the `SwiftLibrary` class in your code

hello-swift/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Basic Swift Integration
2+
3+
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.
4+
5+
![Screenshot](screenshot.png)
6+
7+
## Overview
8+
9+
The project consists of:
10+
11+
1. **Android App**: A simple Kotlin activity that declares an external function `stringFromSwift()` and loads the native library.
12+
2. **Swift Code**: Implements the function using `@_cdecl` attribute to match JNI naming convention, returning a greeting message.
13+
14+
## Prerequisites
15+
16+
Before you can build and run this project, you need to have the following installed:
17+
18+
* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
19+
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
20+
* **Swift SDK for Android**: You need to install the [Swift SDK for Android](https://swift.org/install)
21+
22+
## Running the example
23+
24+
1. Open the `swift-android-examples` project in Android Studio.
25+
26+
2. Select the `hello-swift` Gradle target.
27+
28+
3. Run the app on an Android emulator or a physical device.
29+
30+
## Building from command line
31+
32+
```bash
33+
# Build the example
34+
./gradlew :hello-swift:assembleDebug
35+
36+
# Install on device/emulator
37+
./gradlew :hello-swift:installDebug
38+
```

hello-swift/screenshot.png

34.1 KB
Loading

native-activity/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Full Native Activity
2+
3+
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.
4+
5+
![Screenshot](screenshot.png)
6+
7+
## Overview
8+
9+
The project consists of:
10+
11+
1. **Native Activity**: A complete Swift implementation that creates an OpenGL ES context and renders a continuously changing colored background.
12+
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.
13+
14+
## Prerequisites
15+
16+
Before you can build and run this project, you need to have the following installed:
17+
18+
* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
19+
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
20+
* **Swift SDK for Android**: You need to install the [Swift SDK for Android](https://swift.org/install)
21+
* **Device/emulator with OpenGL ES support**
22+
23+
## Running the example
24+
25+
1. Open the `swift-android-examples` project in Android Studio.
26+
27+
2. Select the `native-activity` Gradle target.
28+
29+
3. Run the app on an Android emulator or a physical device.
30+
31+
## Building from command line
32+
33+
```bash
34+
# Build the example
35+
./gradlew :native-activity:assembleDebug
36+
37+
# Install on device/emulator
38+
./gradlew :native-activity:installDebug
39+
```

native-activity/screenshot.png

32.9 KB
Loading

swift-android.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import org.gradle.api.file.DuplicatesStrategy
77

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

2121
// Architecture definitions

swift-java-hashing-example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Before you can build and run this project, you need to have the following instal
2121

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

2626
## Setup and Configuration
2727

0 commit comments

Comments
 (0)