https://kotlinlang.org logo
#ios
Title
# ios
g

Garrison Henkle

12/06/2023, 5:19 PM
Is there a way to hack a Swift Package Manager package into KMP? I’m using something similar to https://kotlinlang.slack.com/archives/C9JM6Q2UX/p1701117852979319 for most of my dependencies, but I have a single SPM-only dependency that I’ve been having issues integrating. I initially tried to create an iOS framework, import the dependency (Stytch) using SPM + write Obj-C-compatible Swift that calls it, and then tried to link that framework to KMP via Cocoapods, but the Swift compilation is unable to find anything from SPM. I’ve kinda overcomplicated this because I wanted to separate everything out into separate gradle modules, so maybe it’s just not possible atm without just adding the dependency directly to the iOS app 😅
p

Pablichjenkov

12/06/2023, 8:36 PM
If you are ok with having a common interface and implement it in each platform language and inject the implementation into KMP land. Here is a good article describing how to do it. https://medium.com/@mofeejegi/calling-platform-specific-dependencies-in-kotlin-multiplatform-69dbc9a841a2
g

Garrison Henkle

12/06/2023, 9:40 PM
I think I found a ruby script that adds SPM support to Cocoapods, but I’ll try this out if the script doesn’t work out. Thanks!
👍 2
a

Anonymike

12/13/2023, 3:19 PM
Would love to know which script you are using if its not trouble! We've also been exploring options for moving certain swift dependencies that explicitly go with our multiplatform code into a single package, but haven't found a great option yet. Ideally, we wouldn't have to remember to copy certain files into our project and add certain dependencies just to wrap something like Keychain usage where we have written kmp code to hide the individual implementations. I'll speak up if we ever find a solution we are happy with.
g

Garrison Henkle

12/13/2023, 4:34 PM
I wasn’t able to get my attempt working unfortunately. I kept running into linker errors, and I’m not familiar enough with iOS to resolve them kodee sad . The main pieces of code are here in case you want to look them over: https://gist.github.com/garrison-henkle/2d62832c3997fb6036ac5cee11f27e60 I ended up just doing what the medium article above recommends. I created an interface in Kotlin, added the SPM package to iOS project in Xcode (added to the app itself and not the pod project), implemented the Kotlin interface in Swift, and then passed the Swift implementation as a parameter in the Compose UIViewController call. It actually worked out to be fairly clean and IDE support actually works in Xcode if you do it this way (although Xcode barely counts as an IDE, it’s better than nothing).
1
😢 1
It’s honestly pretty terrible code and uses some pretty bad gradle practices to get it working. I was just trying to get anything to work before I cleaned up. My attempt essentially did the following: 1. Override the
podInstallSyntheticIos
task: a. Add SPM support by including the custom ruby file from the SPM dev at the top of the synthetic podfile b. copy over a dummy pbxproj file (the Cocoapods plugin also does this) c. run pod install on the synthetic podfile d. copy over a dummy xcworkspace that has support for a non-legacy build location enabled. SPM cannot use the legacy build location, and I couldn’t find another way to enable it (although that doesn’t mean there isn’t another way, I’m not an iOS dev) 2. Override the
podSetupBuild
tasks: a. Run the
xcodebuild --showBuildSettings
command and write the results using the Cocoapod plugin’s class. This allows the rest of the Cocoapods plugin’s tasks to successfully run b. I honestly don’t remember why I overrode this, so it might be unnecessary. I may have changed the -project flag in the xcodebuild command The above basically gets SPM to automatically install on the synthetic pods project and allows the module to build successfully whenever the cocoapods plugin generates the .def files for the project. The problem is after that when you try to get the actual iOS app to build. It is unable to link the SPM dependencies of the cocoapod, and adding the SPM dependencies to the iosApp itself causes duplicate modules so it fails either way. If there was a way to do what the Kotlin Cocoapods plugin does and “linkOnly” the SPM packages in iosApp, I think everything would work. I updated by gist with a few more things that might be useful. I can’t give my repo unfortunately, but I think I copied everything important over and censored all my work stuff :)
a

Anonymike

12/13/2023, 9:25 PM
I appreciate you sharing what you could. I agree that it goes a bit too far into the underpinnings for me to feel comfortable with it either.
4 Views