Use local (cocoapods) libraries in KMP. I'm runn...
# multiplatform
b
Use local (cocoapods) libraries in KMP. I'm running some experiments with KMP and CMP and need some advice. My goal is to create a local library with a native Swift implementation with Swift library dependencies, that can be accessed from Kotlin. Here's the approach I'm considering 1. In my KMP project, create an iOS CocoaPod library (with cocoapod dependencies) containing Swift code with
@objc
annotations. 2. Add this CocoaPod as a dependency in my KMP library using
pod("MyLib")
. 3. Make my application depend on this library.
Copy code
composeApp
   ├── src
   │     └── commonMain        <- Use MyLibrary
   └── build.gradle.kts        <- implementation(projects.libraries.MyLibrary)

iosApp
   └── Podfile                 <- I need to add "IosLibrary" here

libraries
   └── MyLibrary
       ├── IosLibrary          <- Native CocoaPod library
       │   └── IosLibrary.podspec
       ├── src
       │   ├── commonMain
       │   ├── iosMain         <- "import cocoapod.IosLibrary"
       │   └── jvmMain
       └── build.gradle.kts    <- pod("IosLibrary") with "source"
Benefits I can open the iOS library in Xcode for development with code completion and test with an example project. And then use it directly in my KMP project. Later I expect the libraries can be moved to maven and cocoapod repositories. Concerns I managed to make it work, but I’m unsure if this is a viable solution. Two things I don’t like about this solution 1. I need to duplicate the
pod("IosLibrary")
declaration in
composeApp/build.gradle.kts
. If omitted, I encounter the error: "id: framework ‘IosLibrary’ not found" when running from xcode. 2. I have to add my
IosLibrary
to
iosApp/Podfile
, otherwise i get the error pod install error: “Please, check that each target depended on ComposeApp contains following dependencies: pod 'IosSocketIoLibrary', :path =>..” Are there any ways to get around these? I suspect that 2) is caused by the library being local, but can I solve this by export? Or is there a general better (and easier) way to integrate native libraries I should look into? I have no prior experience with iOS development and CocoaPods. An (incomplete) demo: https://github.com/bschlie/cocoapodlibdemo
t
@Andrey Yastrebov could you provide some thoughts on this?
👀 1
a
Hi @Benjamin Schlie, take a look at our official sample. It demonstrates how to use local cocoapods dependency with pure Swift dependencies. https://github.com/Kotlin/kmp-with-cocoapods-library-sample Answering your questions: 1. Yes, you need to link it. Refer to compose + cocoapods sample: https://github.com/Kotlin/kmp-with-cocoapods-compose-sample 2. That's because it's a local pod
b
Great, it sounds like I'm on the right track with this approach. It's reassuring to know that my declarations of the pod in
composeApp
and
Podfile
are necessary and not due to any misconfiguration. Thanks for the confirmation and samples, Andrey!