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 and the new JNI mode to automatically generate Java wrappers for calling into the Swift library.
The project is structured into two main parts:
-
hashing-lib: A Swift package that usesswift-cryptoto 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 project to create the necessary JNI bindings. -
hashing-app: A standard Android application written in Kotlin using Jetpack Compose. It includes the.aarfile generated byhashing-libas a local dependency and calls the Swifthashfunction when the user presses a button.
Before you can build and run this project, you need to have the following installed:
- Java Development Kit (JDK): This example requires the use of JDK 25. This is only necessary to locally publish the swift-java dependencies, and will not be required in the future. To simplify the build steps, we recommend installing JDK 25 and following all the steps below using the same JDK. Ensure the
JAVA_HOMEenvironment variable is set to your JDK installation path. - Swiftly: You need to install Swiftly
- Swift SDK for Android: You need to install the Swift SDK for Android
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 to install an appropriate Swift SDK for Android.
As the swift-java project does not yet publish the necessary Java packages needed at runtime, we need to do it ourselves, by performing the following steps:
Note: This step will not be necessary once swift-java publishes releases.
In order to publish all artifacts from this library, you must use JDK 25, because some parts of swift-java are built for the most recent Java versions. You will not have to use JDK 25 for the rest of the development process. A simple way to install and manage local Java installations is sdkman:
Note: You will not have to use most recent Java versions for your Android app, and the example currently targets Java language version 11.
Here's how to install sdkman:
curl -s "https://get.sdkman.io" | bashNow restart the terminal so that the sdk utility is added to your path, and then set JDK 25 as your current Java install.
sdk install java 25.0.1-amzn --use # only in order to publish swift-java artifacts locally
export JAVA_HOME="${HOME}/.sdkman/candidates/java/current"Next, let's prepare and publish the swift-java support libraries:
- Enter the
hashing-libdirectorycd hashing-lib - Resolve Swift Packages
swift package resolve
- Publish the
swift-javapackages to local Maven repo./.build/checkouts/swift-java/gradlew --project-dir .build/checkouts/swift-java :SwiftKitCore:publishToMavenLocal
-
Open the
swift-android-examplesproject in Android Studio. -
Select the
hashing-appGradle target. -
Run the app on an Android emulator or a physical device.
-
Enter any text into the text field and press the "Hash" button. The app will call the Swift
hashfunction, and the resulting SHA256 digest will be displayed on the screen.
-
Run the Gradle assemble command from the root directory. This will compile the Swift code for all supported Android ABIs (arm64-v8a, armeabi-v7a, x86_64), run the
jextractplugin, and package everything into an.aarfile../gradlew :hello-swift-java-hashing-lib:assembleRelease
-
After a successful build, the Android library will be located at
swift-java-hashing-example/hashing-lib/build/outputs/aar/hashing-lib-release.aar.
We encourage exploring swift-java by writing Swift code to the file hello-swift-java/hashing-lib/Sources/SwiftHashing/SwiftHashing.swift and building the hashing-app target.\
Tip
The list of supported features can be found at:
https://swiftpackageindex.com/swiftlang/swift-java/main/documentation/swiftjavadocumentation/supportedfeatures#JExtract-calling-Swift-from-Java\
Once the hashing-app build completes, the newly generated Java code will be available to use in the Android project.
Most of these issues are temporary and will be resolved in the future as the swift-java project matures and automates more of the build steps.
If after adding new code to SwiftHashing.swift the same is not available on Android after a build, please delete the swift .build folder located at hello-swift-java/hashing-lib/.build (Attention to the dot before the folder name .build)
Build the Android hashing-app target again and the Swift API should be available in Java.
If the code is still not available, the swift-java project might not support the specific Swift code. Please make sure the feature is listed as supported in the Supported Features documentation.
If the code added to SwiftHashing.swift introduces a new library dependency, forexample import Observation, and the app crashes with the following error:
Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: library "libswiftObservation.so" not found
- Locate the file
build.gradle (Module :hello-swift-java-hashing-lib - Locate the defined list of libraries
def swiftRuntimeLibs - Include the library specified in the error message
In the example log above, Observation cannot be found. This is how the swiftRuntimeLibs show look after including the missing library.
def swiftRuntimeLibs = [
"swiftCore",
"swift_Concurrency",
"swift_StringProcessing",
"swift_RegexParser",
"swift_Builtin_float",
"swift_math",
"swiftAndroid",
"dispatch",
"BlocksRuntime",
"swiftSwiftOnoneSupport",
"swiftDispatch",
"Foundation",
"FoundationEssentials",
"FoundationInternationalization",
"_FoundationICU",
"swiftSynchronization",
"swiftObservation", <===================== NEW LIBRARY
]
