Is it possible to add a Swift package manager depe...
# touchlab-tools
t
Is it possible to add a Swift package manager dependency to your Xcode project for KMP and then reference that code from Kotlin via an import? Similar to how you can import cocoapods.<some lib> or import platform.<lib>?
k
cinterop with SPM? No.
👍 1
t
Do you know what mechanism maps libraries that are imported with dependencies into the "cocoapods" package? So that they can be imported like "import cocoapods.lib.classname"?
k
The Kotlin Gradle plugin runs cinterop for you, using CocoaPods to download the dependencies. JetBrains hasn't added SPM support to do the same thing (nor have we, or anybody else that I've seen). In summary, what the CocoaPods support in the Gradle plugin does is: • Run CocoaPods to download the iOS dependency • Configure Gradle to run Kotlin/Native cinterop on that It does more under the hood. Such as configure linking to be able to run tests, but essentially it just uses CocoaPods to get the dependency, then auto-configured cinterop. That's a huge simplification of the Kotlin Gradle plugin's CocoaPods support. It's complex to build that, so SPM support hasn't been added. I'm not entirely of how straightforward SPM would be. SPM is restrictive.
Now, here's the confusing part. Is this a public library and/or is there a way to use CocoaPods? You can use CocoaPods as "link only"
That will handle the cinterop, but not force you to actually use CocoaPods in the downstream iOS project.
t
I just need to pull in AppAuth for iOS so my project can use it. I was trying to remove all references to cocoapods, since their recent blog post that basically said it was officially in maintenance mode.
k
I was trying to remove all references to cocoapods, since their recent blog post that basically said it was officially in maintenance mode.
Well, I'd say you might notice some issues related to "maintenance mode" in 5 years or so. They're not adding new features, that's basically it. I think KMP needs proper SPM support, and soon, but still. For your case, I'd highly suggest just using CocoaPods, especially if this is just for the Kotlin. If CocoaPods cinterop works, it's the difference between 30 minutes of work or ~4 hours, if you're familiar with cinterop (depending on complexity of AppAuth. I have no idea). If you're not familiar with cinterop, linking, Xcode cli tools, etc, you'll be learning quite a bit about that first.
The title of the video is wrong, but I did a cinterop talk a couple years ago. https://www.droidcon.com/2022/06/28/sdk-design-and-publishing-for-kotlin-multiplatform-mobile/
t
AppAuth for iOS is all objective-C, so we have been using it via cocoapods. I'm switching our project from the KaMPKit structure from a year or so to the newer composeApp structure, since we are using compose multiplatform. I am taking this opportunity to switch to using a framework instead of cocoapods to integrate the shared code. Do you know of a sample/docs that just uses the cocoapods declaration to pull in the cocoapods dependency for cinterop?
I'll check out your talk if that explains that. Just figured I would finish my thought.
Thanks for the advice Kevin!
k
I'll check out your talk if that explains that. Just figured I would finish my thought.
Only watch that if you're going to try to do it manually. It doesn't explain how to do link-only with CocoaPods. It skips that because the talk is a deep-dive on cinterop
In the Kotlin gradle config, in the
pod
dependency, there should be a config option for
linkOnly
https://kotlinlang.org/docs/native-cocoapods-dsl-reference.html#pod-function
Actually, never mind. You don't want
linkOnly
.
Just set up the Kotlin dependency with
pod
, but in Xcode, add AppAuth with SPM. That should keep the linker happy, but I haven't tested it much.
t
Ok, I'll give that a try
I'm really close on getting this to work. Is there a way to pull in kermit-simple without cocoapods? Maybe via SPM since that code is only used within Swift code?
Also, the solution you mentioned requires a little more work in Kotlin 2.0. You have to add
kotlin.apple.deprecated.allowUsingEmbedAndSignWithCocoaPodsDependencies=true
to gradle.properties. I ended up doing the cinterop myself so I wouldn't have to use cocoapods at all. I just would like the simplified syntax for Kermit and I'm all set. I found that info about the flag here: https://youtrack.jetbrains.com/issue/KT-64096/Diagnostic-when-embedAndSign-used-for-framework-with-cocoapods-dependencies
k
The
kermit-simple
module? It doesn't use cinterop. The idea of that module is the Kotlin compiler can't export default parameters, so the standard Kermit API is ugly to call.
kermit-simple
is designed to be used in places that don't have default params (like Swift and JS). https://github.com/touchlab/Kermit/blob/main/kermit-simple/src/nonKotlinMain/kotlin/co/touchlab/kermit/Logger.kt
I wrote that module. I'm 99% sure nobody else on the team added anything.
👍 1
440 Views