Hi everyone! I’m working on a <large Kotlin/Native...
# multiplatform
r
Hi everyone! I’m working on a large Kotlin/Native library project, and I need help with my multiplatform setup. Current Situation • I currently enable only one target at a time, dynamically, based on the OS (e.g.,
linuxX64
on Linux or
macosArm64
on macOS). • When building in isolation, the generated artifacts work fine on both platforms, and I can publish them separately. What I Want to Achieve • I want to publish a single multiplatform artifact that supports both
linuxX64
and
macosArm64
. What I Tried • I enabled both targets in the project. • I also tried enabling
kotlin.mpp.enableCInteropCommonization=true
to unify the cinterop definitions. The Issue • When enabling multiple targets, the common
nativeMain
code doesn’t build anymore
because the cinterop
.def
files are managed per specific target. • With CInterop Commonization enabled, the generated code fails to compile on Linux. • Example: Functions like
g_module_build_path
and
g_module_close
are no longer provided by cinterop. • I suspect this happens because cinterop parses the headers in a "common" environment, and Linux-specific functions might be filtered out due to platform-specific macros or compiler flags. Why Moving Away From
nativeMain
Doesn’t Work
• Duplicating the bindings per target is not a viable option. • A lot of shared code in
nativeMain
depends on native functions (e.g.,
g_free
and
g_type_interfaces
). • It also uses type aliases (e.g.,
GType
) and generated bindings classes (e.g.,
GLib
and
GObject
). • If I move away from
nativeMain
, I would need to duplicate all this code for each platform, which is not maintainable. Questions What are my options for handling this scenario? How can I make multiplatform cinterop work with shared
nativeMain
while supporting platform-specific native functions? Are there other options to end up with a single multiplatform artifact on MavenCentral? Any advice would be greatly appreciated.