-
Notifications
You must be signed in to change notification settings - Fork 20
Update documentation #4
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
Merged
Merged
Changes from 1 commit
Commits
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,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). | ||
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,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). | ||
|
|
||
|  | ||
|
|
||
| ## 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) | ||
andriydruk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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 | ||
| ``` | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
| # 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) | ||
andriydruk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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 | ||
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,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. | ||
|
|
||
|  | ||
|
|
||
| ## 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) | ||
andriydruk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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 | ||
| ``` | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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. | ||
|
|
||
|  | ||
|
|
||
| ## 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) | ||
andriydruk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * **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 | ||
| ``` | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use latest Oct. 17 tag instead.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve synced it with @madsodgaard example |
||
| ) | ||
|
|
||
| // Architecture definitions | ||
|
|
||
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.