Kotlin 2.5.0-Beta2 has a new Swift Export DSL and ...
# swift-export
a
Kotlin 2.5.0-Beta2 has a new Swift Export DSL and can export your Kotlin code as a Swift package. The DSL moves configuration to
kotlin { export { swift { ... } } }
. There's no
export(...)
list anymore, because the dependency graph decides what's exported. A library sets its own
moduleName
and
rootPackage
(the old
flattenPackage
), and consumers pick them up automatically. A consumer can override them with
configure(dependency) { ... }
inside
xcodeIntegration {}
or
swiftPackageIntegration {}
, and can set a dependency's visibility to
SwiftExportVisibility.EXPOSED
or
HIDDEN
. The old
swiftExport {}
block is deprecated. With
swiftPackageIntegration {}
, run
./gradlew :shared:exportDebugSwiftPackage
(or
exportReleaseSwiftPackage
). You get a local Swift package with the generated Swift modules and the compiled Kotlin code as an
XCFramework
, which you add to Xcode or another package like any local dependency. It works alongside
xcodeIntegration {}
. The generated code needs iOS 18 or macOS 15, and in 2.5.0 it can't be combined with
swiftPMDependencies {}
(planned for 2.5.20). The Swift Export sample now covers both, plus most Swift Export language features, with an iOS and macOS app and a Swift package that consumes the export: github.com/Kotlin/swift-export-sample Feedback and bug reports are welcome in this thread or in YouTrack.
swift 7
♥️ 4
🔥 18
kodee intensifies 8
a
Thanks for the Beta2 release! I tried
swiftPackageIntegration {}
on a real production KMP module (three modules, networking + domain, Ktor/Koin/Apollo) and consumed the package from an existing iOS app: it builds, links and runs, and the native enums, sealed types, typed errors and
async
functions are a big improvement over the ObjC framework.
SwiftExportVisibility.HIDDEN
on the internal libraries (Koin, Ktor, kotlinx-io/serialization) cut the generated Swift by about a quarter with config only. The only Swift-side change was dropping an alias to Kotlin’s
CancellationException
, which is fine since cancellation already arrives as
CancellationError
. While porting I hit three bugs, all reproduced on 2.5.0-Beta2 with minimal repros attached: • KT-90498: the generated bridge doesn’t compile for a
suspend
function-type parameter returning a generic class (
suspend () -> Response<D>
), which breaks the whole export • KT-90497: member properties named
description
/
hash
/
hashValue
are silently dropped, and Swift then reads `NSObject`’s
toString()
/
hashCode()
instead. It compiles fine, so the wrong value is easy to miss • KT-90499: a
List<T?>
created in Swift with
nil
elements crashes with “Could not cast value of type ‘NSNull’” when read back One small thing: a dependency without a
rootPackage
logs “No name for packageRoot and will be ignored” many times (KT-90101).
a
Thank you @Ahsan Ullah Rasel, we will process these issues
gratitude thank you 1
g
Using
swiftPackageIntegration()
on 2.5.0-Beta2, the exported package (`exportDebugSwiftPackage`/`exportReleaseSwiftPackage`) builds and links fine in an Xcode app target when the app is built in Debug, but fails to link in Release:
Copy code
Undefined symbols for architecture arm64:
    "_KotlinRuntimeSupport_linkAnchor", referenced from:
        _KotlinRuntimeSupport_ensureLinked in libComposablesKotlin.a[...](libROOT...a.o)
  ld: symbol(s) not found for architecture arm64
The generated
KotlinRuntimeSupport.swift
inside the exported package does declare it:
Copy code
// MARK: - Link anchor
  /// Anchor that keeps this compilation unit in the linked image. Without it this module gets stripped
  @_cdecl("KotlinRuntimeSupport_linkAnchor")
  func _kotlinRuntimeSupportLinkAnchor() {}
Looks like the KotlinRuntimeSupport target gets dead-stripped by Xcode’s more aggressive Release optimization before the linker ever sees that
libComposablesKotlin.a
references that anchor. The generated
Package.swift
itself is correct and identical in structure between Debug/Release, only the build configuration changes. Is this a known limitation, or worth filing as a new issue?
a
That's a new issue, if you have the ability to file it - please do 🙂
👍 1
g
a
@Guilherme Delgado Thanks, fixed in master 🙂
kodee happy 1
g
fixed in 2.5.20-dev-2313 or 2.5.20-dev-2331?
(only 2331 is deployed, perhaps a typo in the ticket? 😅 )
confirmed, 2.5.20-dev-2331 fixes it 😉
kodee intensifies 1
a
> 2.5.20-dev-2313 This is probably the first CI build, they may not be deployed, only the nightly builds are deployed(if the master is green). Basically one should read the "fixed in" number as a "any deployed build with a higher number"
👌 1
I have this small script to check the build numbers for user projects, very handy when you don't want to build the compiler locally very often 😄
kotlin find-kotlin-dev-build.main.kts 2.5.20 --dev --latest
♥️ 1
g
Thanks 😊
a
As a workaround for 2.5.0-Beta2 you can add the following to the build script 🙂
Copy code
// Workaround for KT-90738 (fixed in 2.5.20-Beta1): make the link anchor `package` so Release links.
tasks.withType<Sync>().named { it == "exportReleaseSwiftPackage" }.configureEach {
    doLast {
        val anchor = destinationDir.resolve("Sources/KotlinRuntimeSupport/KotlinRuntimeSupport.swift")
        anchor.writeText(anchor.readText().replace("\nfunc _kotlinRuntimeSupportLinkAnchor", "\npackage func _kotlinRuntimeSupportLinkAnchor"))
    }
}
kodee shocked 1
ghost broom 1
g
Is it planned to land in a 2.5.0-RC-x?
👌 2
2.5.0-RC-278 contains this fix kodee happy