Is there some option to expose module A cinterops ...
# kotlin-native
b
Is there some option to expose module A cinterops to be visible to module B that consumes module A?
a
using Gradle? I thought that happens automatically with a
implementation(project(":module-a"))
, but maybe IntelliJ takes a while to work it out.
b
Wasn't my experience so far. Module B doesn't compile
a
what’s the error?
b
Unresolved type
a
I’m working on a KN project right now that has a .def file that generates cinterop code in one subproject, and another subproject that adds a dependency on it and I’m writing more user-friendly interop code.
in the cinterop project I have this config. Do you have
binaries.staticLib()
?
Copy code
kotlin {
  targets.withType<KotlinNativeTarget>().configureEach {
    compilations.getByName("main") {
      cinterops {
        val extLib by creating {
          defFileProperty.set(file("$projectDir/ext-lib.def"))
        }
      }
    }
    binaries { binaries.staticLib() }
  }
}
b
Is your module b referencing A cinterop from commonMain?
a
no, not commonMain. I’ve set up a custom nativeMain source set
b
I have all three desktop targets only and write my native code in commonMain
NativeMain didn't work at all for me
a
I’ve changed the consuming project to use commonMain, and that also works
have you disabled the hierarchical source sets in gradle.properties, maybe?
what’s the full error? Maybe a platform type is missing on one of the targets?
b
It's the same error as if I were to reference org.totally.non.existing.Type
I can see the cinterop in commonMain of A
a
oh, I also have a workaround in the consuming project. For some reason cinterop didn’t commonize a typealias https://youtrack.jetbrains.com/issue/KT-56102
b
Aha! Must be it!
Thanks
What's your workaround?
a
oh, just to set up the actual/expects manually
the problem was was a function that was generated in all targets, but not commonized
Copy code
package cinterop.internal

public external fun SetTraceLogCallback(...)
so in commonMain I created an expect
Copy code
expect fun setTraceLogCallbackInternal(...)
and then in linuxX64Main, macosX64Main, mingwX64Main I created actual functions that deferred to the generated functions for each target
Copy code
actual fun setTraceLogCallbackInternal(...) {
  cinterop.internal.SetTraceLogCallback(...)
}
v
Have you tried with
api
instead of
implementation
?
b
I HAVEN'T tried implementation 😀
v
with cinterop I don't see much difference between api and implementation. If I do something like ``````
Copy code
dependencies {
  implementation(project(":my-cinterop-bindings"))
  api(project(":another-cinterop-bindings"))
}
Both approaches seem to make all the cinterop stub functions and types available
ah but I don't use commonMain for anything. It's all nativeMain
b
Hmm, at least I now know that cinterops should be visible to consumers. I'll try creating minimal repro to see when things start breaking for me.
commonMain is supposed to be better supported than nativeMain for commonisation (since nativeMain support came much later)
a
oh, I’ve manually set up nativeMain. I didn’t realise there was a new default in 1.8.20.