Hi Folks, I have generated a klib library with cin...
# kotlin-native
a
Hi Folks, I have generated a klib library with cinterop and it works in my tests but when i import this library in another project it doesn’t contain the framework files although it has the static library that I generated! Any ideas ?
@louiscad can you help here ?
l
Did you look at an open source examples that achieves what you want to?
a
Yes, traversed all github repos 🙂
l
Alright, did you try one in your project to see if it works there?
a
My library project works, i verified with tests, but this library once shared via mavenLocal doesn’t contain the framework file which is used by the cinterop xcode project.
this contains the static lib but not the framework lib.
l
I don't think the framework is supposed to be in mavenLocal at all.
Xcode won't understand klibs either
You most likely want to create an XCFramework and import it into Xcode
a
So when i run the app which uses this library it throws this error
_$s9SwiftyRSA10PrivateKeyC4dataAC10Foundation4DataV_tKcfc
and here SwiftyRSA is the framework 😞
Copy code
Undefined symbols for architecture x86_64:
  "_$s9SwiftyRSA10PrivateKeyC4dataAC10Foundation4DataV_tKcfc", referenced from:
      _$s12capillaryios12CapillaryIOSC19privateKeyFromBytes4dataSo03SecE3RefaSg10Foundation4DataV_tFZ in libcapillaryios.a(capillaryios.o)
      _$s12capillaryios12CapillaryIOSC19privateKeyFromBytes4dataSo03SecE3RefaSg10Foundation4DataV_tFZTo in libcapillaryios.a(capillaryios.o)
  "_$s9SwiftyRSA10PrivateKeyC9referenceACSo03SecD3Refa_tKcfc", referenced from:
      _$s12capillaryios12CapillaryIOSC7decrypt4data10privateKey10Foundation4DataVSgAI_So03SecG3RefatFZ in libcapillaryios.a(capillaryios.o)
      _$s12capillaryios12CapillaryIOSC7decrypt4data10privateKey10Foundation4DataVSgAI_So03SecG3RefatFZTo in libcapillaryios.a(capillaryios.o)
      _$s12capillaryios12CapillaryIOSC19bytesFromPrivateKey03secG010Foundation4DataVSo03SecG3Refa_tFZ in libcapillaryios.a(capillaryios.o)
      _$s12capillaryios12CapillaryIOSC19bytesFromPrivateKey03secG010Foundation4DataVSo03SecG3Refa_tFZTo in libcapillaryios.a(capillaryios.o)
      _$s12capillaryios13RSAKeyManagerC15getMyPrivateKey7chainId9SwiftyRSA0fG0CSgSS_tF in libcapillaryios.a(RSAKeyManager.o)
l
klibs are for the Kotlin toolchain only. Also, you shouldn't really need to fiddle with these files yourself
I don't know SwiftRSA and I don't understand what you mean exactly
a
So SwiftyRSA is my framework which is used by the cinterop xcode project
nevermind, i found a gist which mentioned your name so I tagged you, can’t seem to find a hold of it, but its related to this https://gist.github.com/Ribesg/95a172ceb2177d74cf5b15993d40092e
@ribesg Can you help here ? 1. I have a library which has nativeInterop and uses a xcode project which generates a static library and uses an external SPM 2. Once i release this library via MavenLocal, the klib produced from this doesn’t know anything about the swift framework but i have used linkerOpts to link the frameworks. It works fine in the tests but since its not there in klib dependency it fails to compile 3. I am configuring cinterop in my library project here https://github.com/oianmol/capillary-kmp/blob/development/build.gradle.kts#L84
r
Man this gist is more than 3 years old, it's completely outdated
Just use Cocoapods
a
I am not using it directly @ribesg I just thought it would be useful to me.
the pod library is in swift and it doest have objc headers, so had to go with cinterop
I tried cocoapod and added pod into build.gradle.kts but it doesn’t generate what i need 🤷
r
I don't know, what you're doing is too weird for me. I never tried to use pure swift libs in kotlin native because it just doesn't work
Your lib looks small. Rewrite it in Kotlin/Native maybe
a
thanks @ribesg !
k
I didn’t go too deep here, but the summary I think is cinterop just creates the kotlin definitions from the C header. Linking told the test compiler where to find the binary library, but that doesn’t package it in your klib, so you have no binary. So, wherever you’re later trying to use the klib, you’d also need to link there as well, or you need to stuff the binary in the klib, which can be tricky to config. If it is a small library, a rewrite is probably easier. If it’s not a library that you’re trying to distribute to anybody else, you can just link the framework in your downstream Xcode project.
a
Thanks Kevin, i ended up using it directly in my main project instead of using it as a maven dependency since it was a small piece of code.
k
I gave a talk on this (sort of) earlier this year: https://www.droidcon.com/2022/06/28/sdk-design-and-publishing-for-kotlin-multiplatform-mobile/. the website has the wrong title, but the talk goes into cinterop and binaries.
cinterop is conceptually pretty simple, but the binary/linking can be tough.
If the library is small, I try to avoid cinterop altogether 🙂 (there’s a section in the talk about that)
a
It depends on a swift library via cocoapods which can’t be directly imported because it doesn’t have proper header files for obj and direct cinterop fails because of that , hence i had to go with custom nativeInterop route by defining a *.def file and all that
429 Views