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 an Android app. It shows you how to call async Swift functions from Kotlin/Java and also how it is possible to implement a Swift protocol using a Java class and pass that back to Swift.

The example consists of an Android application (weather-app) and a Swift library (weather-lib) that fetches the weather for the current location. 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:

weather-lib:

A Swift package that uses swift-openapi-generator to call the Open-Meteo Weather API. It is configured with a Gradle build script (build.gradle). This module utilizes the swift-java project to create the necessary JNI bindings.

The Swift library exposes a Swift protocol named LocationFetcher, which is used by WeatherClient to retrieve the current user location. This is a traditional API design in Swift, and allows any consumers of the library to provide their own mechanism of retrieving the location. For iOS, this could mean using CLLocationManager.

swift-java allows us to implement the LocationFetcher protocol using a Java class, and therefore use Android APIs to retrieve the user location and pass that back to Swift. This is implemented in the LocationService class.

After fetching the user location, the WeatherClient will fetch the weather using the automatically generated OpenAPI bindings. This method is marked as async and we can easily call that from Java/Kotlin, where it will return a CompletableFuture

weather-app:

A standard Android application written in Kotlin using Jetpack Compose.

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 to download the latest open-source Swift toolchain
  • 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.

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 weather-lib
  2. Resolve Swift Packages
    swift package resolve
  3. Publish the swift-java packages to a 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 weather-app Gradle target.

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

  4. Press the "Fetch Weather" button.