{"id":50897872,"url":"https://github.com/scade-platform/swift4j","last_synced_at":"2026-07-22T07:01:17.125Z","repository":{"id":153085459,"uuid":"366834187","full_name":"scade-platform/swift4j","owner":"scade-platform","description":"Run native Swift Code across multiple platforms using Java or Kotlin","archived":false,"fork":false,"pushed_at":"2026-04-02T16:25:41.000Z","size":369,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"main","last_synced_at":"2026-04-03T01:34:17.319Z","etag":null,"topics":["java","java-android","kotlin","kotlin-android","swift","swift-android","swift-multi-language","swift-multiplatform"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scade-platform.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-05-12T19:40:21.000Z","updated_at":"2026-04-02T16:25:38.000Z","dependencies_parsed_at":"2024-08-09T11:14:11.196Z","dependency_job_id":"faa8672f-cd00-4478-a350-f8fd2fe5daa3","html_url":"https://github.com/scade-platform/swift4j","commit_stats":null,"previous_names":["scade-platform/swift4j"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/scade-platform/swift4j","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scade-platform%2Fswift4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scade-platform%2Fswift4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scade-platform%2Fswift4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scade-platform%2Fswift4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scade-platform","download_url":"https://codeload.github.com/scade-platform/swift4j/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scade-platform%2Fswift4j/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35751644,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-07-20T02:08:10.276Z","status":"online","status_checked_at":"2026-07-22T02:00:06.236Z","response_time":124,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","java-android","kotlin","kotlin-android","swift","swift-android","swift-multi-language","swift-multiplatform"],"created_at":"2026-06-16T01:31:30.084Z","updated_at":"2026-07-22T07:01:17.120Z","avatar_url":"https://github.com/scade-platform.png","language":"Swift","funding_links":[],"categories":["java"],"sub_categories":[],"readme":"# Swift4j Toolkit\n\nThe **swift4j** toolkit is a set of libraries and tools making possible a seamless interoperability between Swift and Java/Kotlin. Besides the **swift4j** Swift package presented in this repository there are also a set of Gradle plugins and the Swift Toolchain for Android allowing smooth integration of the Swift libraries into the desktop and mobile applications written in Java/Kotlin.       \n\n## Quick Example\n\nUsing the *swift4j* Toolkit we can access, for example, the following Swift class:\n\n```swift\n@jvm class Arrays {\n  static func mapReversed(_ arr: [Int], mapping: (Int) -\u003e Int) -\u003e [Int] {\n    return arr.reversed().map(mapping)\n  } \n}\n\n```\n\nfrom Kotlin:\n\n\n```kotlin\nval arr = longArrayOf(1, 2, 3)\nArrays.mapReversed(arr) {\n  it + 1\n}\n```\n\nby just adding a single `@jvm` annotation to the Swift class. \n\n## Description\n\nThe **swift4j** packages contains of a set of libraries and tools drastically simplifying interoperability between Swift and JVM. The central part of the package is built around the Swift macro system and the Swift package plugins, both are based on the library abstracting away the details of the Java Native Interfaces (JNI), that is used for the communication between JVM and the native code. \n\nThe main goal of the **swift4j** package is to annotate the Swift code and generate bridgings for the Swift/JVM interoperability. In order to simplify the integration into the native Java/Kotlin development enviroments there are two [SwiftPM Gradle](https://github.com/scade-platform/spm-gradle-plugin) plugins:\n\n* `io.scade.gradle.plugins.swiftpm` - for generic Java/Kotlin projects  \n\n* `io.scade.gradle.plugins.android.swiftpm` - for Java/Kotlin projects on the Android platform\n\nThe **SwiftPM Gradle** plugin for Android takes care of the installation of the [Swift for Android](https://github.com/scade-platform/swift-android-toolchain) that allows to compile Swift code for the Android platform as well as packaging all required parts into a ready to use application. For more details, please follow the [Usage](#usage) section or take a look at [swift4j-examples](https://github.com/scade-platform/swift4j-examples).   \n\nFor more details on plugins configuration please refer to the plugin's [README](https://github.com/scade-platform/spm-gradle-plugin/blob/main/README.md)\n\n\n## Getting Started\n\n### Prerequisites\n\n- macOS, Linux\n\t- Swift \u003e= 5.9\n\t- Java \u003e= 1.8\n\n- Android\n\t- NDK 25.x (can be installed from the Android Studio)\n\t- [Swift for Android](https://github.com/scade-platform/swift-android-toolchain) \u003e= 5.10 (will be installed automatically when used with the SwiftPM Gradle plugin)\n \n### Usage\n\n#### Swift code annotations\n\nTo make the Swift code available for the JVM platform, we first need to annotate our Swift code by macros defined in the **swift4j**. \n\nTo do this, we add a dependency on the **swift4j** package into the `Package.swift` file:\n\n```swift\nlet package = Package(\n  ...\n  dependencies: [\n    .package(url: \"https://github.com/scade-platform/swift4j.git\", from: \"1.0.0\")\n  ]\n  ...\n)\n```\n\nNow, we can add a target dependency on the **Swift4j** product from the **swift4j** package to all targets in our `Package.swift` file that are going to be exposed to Java/Kotlin. For example to expose a *swift4j-examples* target, do the following:\n\n```swift\n.target(\n  name: \"swift4j-examples\",\n  dependencies: [\n    .product(name: \"Swift4j\", package: \"swift4j\")              \n  ]\n)\n```\n\n**NOTE:** due to limitations of JNI, only Swift targets that are parts of dynamic libraries can be accessed from Java/Kotlin. \n\nAfter that we can annotate our code using Swift macro `@jvm`. For example:\n\n```swift\nimport Swift4j // import @jvm macro and supporting types \n\n// @jvm automatically exposes the annoted class together with\n// all methods that can be exposed\n@jvm \npublic class GreetingService {\n  func greet(name: String, _ response: (String) -\u003e Void) {        \n    response(\"Swift greets \\(name)\")\n  }\n}\n```\n\nAnd that is everything what is needed to expose the class `GreetingService`. After adding the macro `@jvm` to the class definition, all necessary bridging code will be generated for the class as well as for all methods that can be exposed. \n\nFor every method that cannot be exposed a warning will be generated suggesting to add a `@nonjvm` macro to ignore export. The macro can be also used just to not expose some methods. For example:\n\n```swift\n@jvm \npublic class GreetingService {\n  ...\n  @nonjvm \n  func greet(person: Person, _ response: (String) -\u003e Void) {        \n    response(\"Swift greets \\(person.name)!\")\n  }\n}\n```\n\nIn this case the type `Person` used in the `greet` method's signature is not exposed to Java/Kotlin and hence the method cannot be exposed as well.  We can either ignore the whole method by adding  `@nonjvm` macro to it or expose the type `Person`. \n\n**NOTE**: due to some significant differences between Swift and JVM not all Swift types and constructs can be exposed to Java/Kotlin whereas some constructs are not supported yet and will be added in future releases. More details on exposable types and constructs can be found in the [Language Features Support](#language-features-support) section\n\n#### Call Swift code from Java/Kotlin\n\nThe easiest way to integrate the exposed Swift code into the Java/Kotlin projects is to use one of the [SwiftPM Gradle](https://github.com/scade-platform/spm-gradle-plugin) plugins depending on the target platform.\n\nFor example, in order to use the exposed Swift code in an Android application, just add the `io.scade.gradle.plugins.android.swiftpm` plugin to your `build.gradle.kts` file:    \n\n```kotlin\nplugins {\n  id(\"io.scade.gradle.plugins.swiftpm\") version \"1.0.3\" \n}\n```\n\ntogether with the plugin's configuration section:\n\n```kotlin\nswiftpm { \n  // Path to the Swift package\n  path = file(\"\u003cPACKAGE LOCATION\u003e\")\n  // Name of the package's product (dynamic) containing exposed Swift targets\n  product = \"\u003cPRODUCT NAME\u003e\"\n}\n```\n\nNow you can access all exposed classes from Java/Kotlin by importing them just like normal Java classes. The **swift4j** tool tries to generate bridgings as closed to Java coding conventions as possible while preserving the original Swift design. \n\nFor example to call the previously exposed `GreetingService` from Kotlin do the following:\n\n```kotlin\nimport swift4j_examples.GreetingService\n\n// The Swift dynamic library has to be loaded once prior\n// the use of any exposed type  \nSystem.loadLibrary(\"swift4-examples\")\n\n// Create an instance of the exposed class \nval greetings = GreetingService()\n// And call the 'greet' method passing a Kotlin lambda  \ngreetings.greet(\"Android\") { resp -\u003e\n  print(resp)    \n}\n// Output: Swift greets Android!\n```\n\nIn the code snippet above the exposed class imported from the package named by the name of the original Swift target, i.e. the `swift4-examples` (a hyphen is replaced by an undescore as it is not allowed in Java names). After the library is loaded, we can finally access all exposed and imported types as they would be originally written in Java or Kotlin. For example, Swift lambdas are mapped to Kotlin lambdas making it really easy to call Swift code that accepts callbacks.\n\n#### Generate bridgings manually (without Gradle plugins)\n\nIt is also possible to generate Java bridgings without the SwiftPM Gradle plugins using either the *swift4j-cli* CLI tool or the Swift package command line plugin. Both are included in the *swift4j* package.\n\n**NOTE:** the CLI tool generates bridgings from Swift files only while the SPM plugin generates for the whole product  \n\nTo generate bridgings using the CLI tool, execute the following command:\n\n```shell\nswift4j-cli --package \u003cJAVA_PACKAGE\u003e --java-version \u003cJAVA_VERSION\u003e -o \u003cOUTPUT_FOLDER\u003e \u003cINPUT SWIFT FILES\u003e\n```\n\nParameters:\n\t\n- **JAVA_PACKAGE** (required): Java package name for the generated classes\n- **JAVA_VERSION** (optional): Java version the generated source has to be compatible with (default: 11). For Android has to be adjusted depending on the minimal API level\n- **OUTPUT_FOLDER** (optional): Output folder (default: outputs classes to the standart output)\t \n\nTo generate bridgings using the SPM package plugin, execute the following command in the folder where your `Package.swift` is located:\n\n```shell\nswift package plugin generate-java-bridging --product \u003cPRODUCT\u003e --java-version \u003cJAVA_VERSION\u003e\n```\n\nParameters:\n- **PRODUCT** (required): Product name from your package for which the bridgings has to be generated \n- **JAVA_VERSION** (optional): Same as for the CLI version \n\nThe SPM plugin recursively iterates over all targets of the product and generates bridgings for all classes marked by the `@jvm` macro. It uses a target name as a Java package name for all classes within the target. That's why it does not need a Java package name as an input. The generated classes are written into the Swift package's build folder (default: `.build/plugins/generate-java-bridging/outputs`). For every target a separate folder is created in the output folder.\n\nAfter the generation, the briging files can be built and used as normal Java sources in any Java/Kotlin project by including them into the source tree. The binary can be built using the standard Swift build process for the target platform and can be then included into the final product.\n\n\n## Language Features Support\n\nNon-generic types (marked by `@jvm` macro):\n\n- Classes\n\n- Structs\n\n- Enums without associated types\n\n- Nested types (if parent type is also exposed)\n\nBuilt-in types:\n\n- Primitive types\n\n- Arrays of exposed types\n\n- Lambdas (if all signature types are exposed)\n  \nNon-private type members (if and only if its signatures only contain exposed types):\n\n- Initialisers\n\n- Instance methods\n  \n  - `async` methods (called asynchronously)\n  - `inout` parameters\n\n- Static methods\n\n- Properties\n\n  \nWork in Progress:\n\n- Overloading\n\n- Error handling (`throws`)\n\n- Extensions\n  \n## Examples\n\n[swift4j-examples](https://github.com/scade-platform/swift4j-examples) \n\n\n## Contact\n\n[Join our Discord channel](https://discord.gg/seAYea9r)\n\n\n## License\n\nPlease see [LICENSE](LICENSE.txt) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscade-platform%2Fswift4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscade-platform%2Fswift4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscade-platform%2Fswift4j/lists"}