3

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.

enter image description here

Citut
  • 847
  • 2
  • 10
  • 25

1 Answers1

7

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

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
  • That's the direction I was thinking I would need to go, thanks for the links! In the case above, does Shared Package also need to be a KMM library to allow the consuming KMM project to generate the proper iOS binaries? Or can it be a pure kotlin library and consumed in the `commonMain` module of KMM Project A and B? – Citut Dec 08 '21 at 04:06
  • You'll need to add the iOS target(s) to "Shared Package", but you can then just use common source sets and not add any platform-specific code. It will need to be KMM and add the targets for sure, though. – Kevin Galligan Dec 08 '21 at 15:14
  • 1
    Is this wrapper umbrella module always going to be needed, or is there a plan to improve multiple frameworks with common ancestors? – Eliezer Aug 05 '22 at 22:00