Skip to content

Commit f63a277

Browse files
authored
Merge pull request #2 from swiftlang/swift-java-example
Add example that uses `swift-java`
2 parents 6116109 + 84134bf commit f63a277

40 files changed

+1127
-1
lines changed

gradle/libs.versions.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ espressoCore = "3.7.0"
88
appcompat = "1.7.1"
99
material = "1.12.0"
1010
constraintlayout = "2.2.1"
11+
lifecycleRuntimeKtx = "2.9.2"
12+
activityCompose = "1.10.1"
13+
composeBom = "2024.09.00"
1114

1215
[libraries]
1316
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -17,9 +20,19 @@ androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-co
1720
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
1821
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
1922
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
23+
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
24+
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
25+
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
26+
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
27+
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
28+
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
29+
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
30+
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
31+
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
32+
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
2033

2134
[plugins]
2235
android-application = { id = "com.android.application", version.ref = "agp" }
2336
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
2437
android-library = { id = "com.android.library", version.ref = "agp" }
25-
38+
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

settings.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pluginManagement {
22
repositories {
3+
mavenLocal()
34
google {
45
content {
56
includeGroupByRegex("com\\.android.*")
@@ -14,6 +15,7 @@ pluginManagement {
1415
dependencyResolutionManagement {
1516
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
1617
repositories {
18+
mavenLocal()
1719
google()
1820
mavenCentral()
1921
}
@@ -24,3 +26,7 @@ include(":hello-swift")
2426
include(":hello-swift-callback")
2527
include(":hello-swift-library")
2628
include(":native-activity")
29+
include(":hashing-lib")
30+
project(":hashing-lib").projectDir = file("swift-java-hashing-example/hashing-lib")
31+
include(":hashing-app")
32+
project(":hashing-app").projectDir = file("swift-java-hashing-example/hashing-app")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.DS_STORE
2+
3+
# Swift
4+
.build/
5+
.swiftpm/
6+
Package.resolved
7+
8+
# Gradle files
9+
.gradle/
10+
build/
11+
12+
# Local configuration file (sdk path, etc)
13+
local.properties
14+
15+
# Log/OS Files
16+
*.log
17+
18+
# Android Studio generated files and folders
19+
captures/
20+
.externalNativeBuild/
21+
.cxx/
22+
*.aab
23+
*.apk
24+
output-metadata.json
25+
26+
# IntelliJ
27+
*.iml
28+
.idea/
29+
misc.xml
30+
deploymentTargetDropDown.xml
31+
render.experimental.xml
32+
33+
# Keystore files
34+
*.jks
35+
*.keystore
36+
37+
# Google Services (e.g. APIs or Firebase)
38+
google-services.json
39+
40+
# Android Profiling
41+
*.hprof
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# swift-java on Android
2+
3+
This example contains a sample Android application that demonstrates how to call Swift code from a Android app.
4+
The example consists of an Android application (`hashing-app`) and a Swift library (`hashing-lib`) that performs a SHA256 hash on a given string.
5+
The Swift library uses [swift-java](https://github.com/swiftlang/swift-java) and the new JNI mode to automatically
6+
generate Java wrappers for calling into the Swift library.
7+
8+
![IDE Screenshot](resources/ide.png)
9+
10+
## Overview
11+
12+
The project is structured into two main parts:
13+
14+
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](https://github.com/swiftlang/swift-java) project to create the necessary JNI bindings.
15+
16+
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.
17+
18+
## Prerequisites
19+
20+
Before you can build and run this project, you need to have the following installed:
21+
22+
* **Java Development Kit (JDK)**: We recommend using JDK 21. Ensure the `JAVA_HOME` environment variable is set to your JDK installation path.
23+
* **Swiftly**: You need to install [Swiftly](https://www.swift.org/install/)
24+
* **Swift SDK for Android**: You need to install the [Swift Android SDK](https://swift.org/install)
25+
26+
## Setup and Configuration
27+
28+
### Publish `swift-java` packages locally
29+
As the `swift-java` project does not yet publish the neccessary Java packages needed at runtime, we need to do it ourself, by performing the following steps:
30+
31+
1. Enter the `hashing-lib` directory
32+
```bash
33+
cd hashing-lib
34+
```
35+
2. Resolve Swift Packages
36+
```bash
37+
swift package resolve
38+
```
39+
3. Publish the `swift-java` packages to local Maven repo
40+
```bash
41+
./.build/checkouts/swift-java/gradlew --project-dir .build/checkouts/swift-java :SwiftKitCore:publishToMavenLocal
42+
```
43+
44+
## Running the example
45+
46+
1. Open the `swift-android-examples` project in Android Studio.
47+
48+
2. Select the `hashing-app` Gradle target.
49+
50+
3. Run the app on an Android emulator or a physical device.
51+
52+
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.
53+
54+
### Only building the Swift Library (`hashing-lib`)
55+
56+
1. Navigate to the `hashing-lib` directory:
57+
```bash
58+
cd hashing-lib
59+
```
60+
61+
2. Run the Gradle assemble command. 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.
62+
```bash
63+
./gradlew assembleRelease
64+
```
65+
66+
3. After a successful build, the Android library will be located at `hashing-lib/build/outputs/aar/hashing-lib-release.aar`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.kotlin.compose)
5+
}
6+
7+
android {
8+
namespace = "com.example.hashingapp"
9+
compileSdk = 36
10+
11+
defaultConfig {
12+
applicationId = "com.example.hashingapp"
13+
minSdk = 28
14+
targetSdk = 36
15+
versionCode = 1
16+
versionName = "1.0"
17+
18+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
isMinifyEnabled = false
24+
proguardFiles(
25+
getDefaultProguardFile("proguard-android-optimize.txt"),
26+
"proguard-rules.pro"
27+
)
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility = JavaVersion.VERSION_11
32+
targetCompatibility = JavaVersion.VERSION_11
33+
}
34+
kotlinOptions {
35+
jvmTarget = "11"
36+
}
37+
buildFeatures {
38+
compose = true
39+
}
40+
}
41+
42+
dependencies {
43+
44+
implementation(libs.androidx.core.ktx)
45+
implementation(libs.androidx.lifecycle.runtime.ktx)
46+
implementation(libs.androidx.activity.compose)
47+
implementation(platform(libs.androidx.compose.bom))
48+
implementation(libs.androidx.ui)
49+
implementation(libs.androidx.ui.graphics)
50+
implementation(libs.androidx.ui.tooling.preview)
51+
implementation(libs.androidx.material3)
52+
implementation(project(":hashing-lib"))
53+
testImplementation(libs.junit)
54+
androidTestImplementation(libs.androidx.junit)
55+
androidTestImplementation(libs.androidx.espresso.core)
56+
androidTestImplementation(platform(libs.androidx.compose.bom))
57+
androidTestImplementation(libs.androidx.ui.test.junit4)
58+
debugImplementation(libs.androidx.ui.tooling)
59+
debugImplementation(libs.androidx.ui.test.manifest)
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
package com.example.hashingapp
14+
15+
import androidx.test.platform.app.InstrumentationRegistry
16+
import androidx.test.ext.junit.runners.AndroidJUnit4
17+
18+
import org.junit.Test
19+
import org.junit.runner.RunWith
20+
21+
import org.junit.Assert.*
22+
23+
/**
24+
* Instrumented test, which will execute on an Android device.
25+
*
26+
* See [testing documentation](http://d.android.com/tools/testing).
27+
*/
28+
@RunWith(AndroidJUnit4::class)
29+
class ExampleInstrumentedTest {
30+
@Test
31+
fun useAppContext() {
32+
// Context of the app under test.
33+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
34+
assertEquals("com.example.hashingapp", appContext.packageName)
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.HashingApp">
14+
<activity
15+
android:name=".MainActivity"
16+
android:exported="true"
17+
android:label="@string/app_name"
18+
android:theme="@style/Theme.HashingApp">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
26+
27+
</manifest>

0 commit comments

Comments
 (0)