Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

swift-java on Android

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.

IDE Screenshot

Overview

The project is structured into two main parts:

  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 project to create the necessary JNI bindings.

  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.

Prerequisites

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_HOME environment 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

Setup and Configuration

Prepare Swift Android SDK and matching Swift

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.

Publish swift-java packages locally

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" | bash

Now 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:

  1. Enter the hashing-lib directory
    cd hashing-lib
  2. Resolve Swift Packages
    swift package resolve
  3. Publish the swift-java packages to local Maven repo
    ./.build/checkouts/swift-java/gradlew --project-dir .build/checkouts/swift-java :SwiftKitCore:publishToMavenLocal

Running the example

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

  2. Select the hashing-app Gradle target.

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

  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.

Only building the Swift Library (hashing-lib)

  1. 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 jextract plugin, and package everything into an .aar file.

    ./gradlew :hello-swift-java-hashing-lib:assembleRelease
  2. After a successful build, the Android library will be located at swift-java-hashing-example/hashing-lib/build/outputs/aar/hashing-lib-release.aar.

Exploring swift-java

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

Once the hashing-app build completes, the newly generated Java code will be available to use in the Android project.

Troubleshooting

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.

New Swift Code is not available in Java once Android build completes:

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.

Crash: Library not found

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

  1. Locate the file build.gradle (Module :hello-swift-java-hashing-lib
  2. Locate the defined list of libraries def swiftRuntimeLibs
  3. 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
]