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.
The project is structured into two main parts:
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
A standard Android application written in Kotlin using Jetpack Compose.
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 to download the latest open-source Swift toolchain
- Swift SDK for Android: You need to install the Swift SDK for Android
Currently, these examples utilize very recent nightly Swift Android SDK versions.
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 weather-lib - Resolve Swift Packages
swift package resolve
- Publish the
swift-javapackages to a 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
weather-appGradle target. -
Run the app on an Android emulator or a physical device.
-
Press the "Fetch Weather" button.
