Code Generation
swift4j generates Kotlin/Java proxy classes from your Swift source files. There are two ways to run the generator: as an SPM command plugin (the primary workflow) or directly via the CLI tool.
SPM plugin
Section titled “SPM plugin”The generate-java-bridging command plugin ships with swift4j. Run it from your package directory:
swift package plugin generate-java-bridging --product <ProductName>Replace <ProductName> with the name of the SPM product whose targets you want to export. The plugin processes all targets in the product recursively.
Output location
Section titled “Output location”Generated files are written to:
.build/plugins/generate-java-bridging/outputs/<ProductName>/main/java/The directory structure under main/java/ follows the Java package naming convention, using the target name as the package name (hyphens replaced with underscores):
main/java/ MyLibrary/ GreetingService.kt Greeting.kt Counter.ktCopying Java sources
Section titled “Copying Java sources”If your Swift targets include bundled .java files (such as the base classes shipped with swift4j itself), pass --copy-java-sources to include them in the output:
swift package plugin generate-java-bridging --product MyLibrary --copy-java-sourcesAndroid ViewModel generation
Section titled “Android ViewModel generation”For Android projects using @Observable types, pass --generate-android-viewmodels to generate ViewModel and ViewModelFactory subclasses:
swift package plugin generate-java-bridging \ --product MyLibrary \ --generate-android-viewmodelsSee Observable for details.
Including generated files in your project
Section titled “Including generated files in your project”Android (Gradle)
Copy or symlink the generated main/java/ directory into your Android module’s source set. In build.gradle:
android { sourceSets { main { java.srcDirs += [ "path/to/.build/plugins/generate-java-bridging/outputs/MyLibrary/main/java" ] } }}JVM (Gradle)
sourceSets { main { kotlin.srcDirs += [ "path/to/.build/plugins/generate-java-bridging/outputs/MyLibrary/main/java" ] }}Java version
Section titled “Java version”The generator defaults to Java 11 compatibility. Pass --java-version to target a different version:
swift package plugin generate-java-bridging --product MyLibrary --java-version 17Re-running generation
Section titled “Re-running generation”Run the plugin again whenever you add, remove, or rename exported Swift types. The output directory is replaced entirely on each run — do not edit the generated files by hand.