You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example contains a sample Android application that demonstrates how to call Swift code from a Android app.
4
+
The example consists of an Android application (`hashing-app`) and a Swift library (`hashing-lib`) that performs a SHA256 hash on a given string.
5
+
The Swift library uses [swift-java](https://github.com/swiftlang/swift-java) and the new JNI mode to automatically
6
+
generate Java wrappers for calling into the Swift library.
7
+
8
+

9
+
10
+
## Overview
11
+
12
+
The project is structured into two main parts:
13
+
14
+
1.**`hashing-lib`**: A Swift package that uses `swift-crypto` to provide a hashing function. It is configured with a Gradle build script (`build.gradle`) that compiles the Swift code into an Android Archive (`.aar`) file. This module utilizes the [swift-java](https://github.com/swiftlang/swift-java) project to create the necessary JNI bindings.
15
+
16
+
2.**`hashing-app`**: A standard Android application written in Kotlin using Jetpack Compose. It includes the `.aar` file generated by `hashing-lib` as a local dependency and calls the Swift `hash` function when the user presses a button.
17
+
18
+
## Prerequisites
19
+
20
+
Before you can build and run this project, you need to have the following installed:
21
+
22
+
***Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
23
+
***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)
25
+
26
+
## Setup and Configuration
27
+
28
+
### Publish `swift-java` packages locally
29
+
As the `swift-java` project does not yet publish the neccessary Java packages needed at runtime, we need to do it ourself, by performing the following steps:
30
+
31
+
1. Enter the `hashing-lib` directory
32
+
```bash
33
+
cd hashing-lib
34
+
```
35
+
2. Resolve Swift Packages
36
+
```bash
37
+
swift package resolve
38
+
```
39
+
3. Publish the `swift-java` packages to local Maven repo
1. Open the `swift-android-examples` project in Android Studio.
47
+
48
+
2. Select the `hashing-app` Gradle target.
49
+
50
+
3. Run the app on an Android emulator or a physical device.
51
+
52
+
4. Enter any text into the text field and press the "Hash" button. The app will call the Swift `hash` function, and the resulting SHA256 digest will be displayed on the screen.
53
+
54
+
### Only building the Swift Library (`hashing-lib`)
55
+
56
+
1. Navigate to the `hashing-lib` directory:
57
+
```bash
58
+
cd hashing-lib
59
+
```
60
+
61
+
2. Run the Gradle assemble command. This will compile the Swift code for all supported Android ABIs (arm64-v8a, armeabi-v7a, x86_64), run the `jextract` plugin, and package everything into an `.aar` file.
62
+
```bash
63
+
./gradlew assembleRelease
64
+
```
65
+
66
+
3. After a successful build, the Android library will be located at `hashing-lib/build/outputs/aar/hashing-lib-release.aar`.
0 commit comments