Lets say I have a package, Shared Package that is shared with two KMM projects: KMM Project A and KMM Project B.
So we have Shared Package -> KMM Project A and Shared Package -> KMM Project B.
Now I want to use both KMM Project A and KMM Project B in my iOS app. How does that work? Is Shared Package bundled with both frameworks (i.e. I am including the same dependency twice?). Furthermore, does the Shared Package need to be a KMM Project to allow KMM Project A and B to generate the relevant iOS frameworks? Or can it be a pure Kotlin project?
Here is a diagram that might give more explanation of the situation I am trying to understand.
CodePudding user response:
You need a wrapper "unbrella" module that depends on KMM Project A and KMM Project B, and have that generate your Xcode Framework.
You can technically generate 2 frameworks, one for each of the KMM Project A and B, but they will both have a copy of "Shared Package", along with relevant portions of the Kotlin standard lib, and most importantly, those 2 frameworks will be distinct on a binary level, so they can't communicate.
By that I mean, if "Shared Package" has a data class Foo
, and you get that as a result from a call to "KMM Project A", say fun makeAFoo():Foo
, and you have a function in "KMM Project B" that's defined as fun takeAFoo(foo:Foo)
, the Foo
instance you get from makeAFoo()
cannot be passed into takeAFoo(foo:Foo)
.
So, the short answer is you need a wrapper module that pulls in both of the "Shared Package" modules. You'll also need to export them through the umbrella.
See: https://touchlab.co/multiple-kotlin-frameworks-in-application/ and https://kotlinlang.org/docs/mpp-build-native-binaries.html#export-dependencies-to-binaries