This example demonstrates bidirectional communication between Swift and Android, without the use of swift-java interoperability support, just by using raw JNI. The Swift code runs a timer that calls back to Android every second to update the UI with the current time (HH:MM:SS format).
The project consists of:
- Android App: A Kotlin activity that starts/stops a timer and displays the current time. It includes a callback method
updateTimer()that gets called from Swift. - Swift Code: Implements a background timer using
DispatchQueuethat calls back to the Android activity every second to update the timer display.
Before you can build and run this project, you need to have the following installed:
- Java Development Kit (JDK): We recommend using JDK 21. 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
-
Open the
swift-android-examplesproject in Android Studio. -
Select the
hello-swift-raw-jni-callbackGradle target. -
Run the app on an Android emulator or a physical device.
# Build the example
./gradlew :hello-swift-raw-jni-callback:assembleDebug
# Install on device/emulator
./gradlew :hello-swift-raw-jni-callback:installDebug