Hello! Is there any way to share Kotlin code betwe...
# multiplatform
r
Hello! Is there any way to share Kotlin code between iOS frameworks? In architecture like on screenshot, we have conflict with classes or interfaces located in "Shared Kotlin library". If in shared library there would be a class "Foo" then in iOS we would have 2 classess FrameworkAFoo and FrameworkBFoo and we have to map between them Is there any way to link kotlin libraries instead of "merging" them into framework?
c
no unfortunately, I don't believe this is currently possible with KMP
each framework will generate its own set of classes that will not be able to be shared across other frameworks. Even the dependencies of the shared module itself (for example kotlin Flow will become FrameworkAFlow and FrameworkBFlow)
this blog by touchlab does a good deep dive into the topic. You can theoretically have multiple frameworks, but you cannot share models across those frameworks
r
I see, thank you
j
So, we can have multiple libraries, they just can't share code among themselves?
r
Yes, even if we have same class "Foo" then iOS will read this like FrameworkAFoo and FrameworkBFoo and you have to provide mappers in Swift to communicate between libraries
j
Ok, but is there a way for splitting the shared code in different gradle modules and compiling them for iOS as if it was just one artefact? I think multiple namespaces are not possible at the moment, right? This is kinda of a big restriction for some teams..
c
> So, we can have multiple libraries, they just can't share code among themselves? You can share code, but you cannot share models as
Foo
from
frameworkA
will not have type equality of
Foo
from
frameworkB
. > but is there a way for splitting the shared code in different gradle modules and compiling them for iOS as if it was just one artefact? Yes, this is what we currently do. We split our kmp codebase into feature modules and add each of these feature modules as dependencies of a
shared
module that is then used to generate the Framework on iOS. So iOS imports this one framework as one package.
r
So the best approach for now would be to gather all Kotlin libraries into one project and build single framework? And somehow refactor our iOS libraries to use intermediate models instead of Kotlin, something like this:
c
> And somehow refactor our iOS libraries to use intermediate models instead of Kotlin I'm not sure I understand what you mean here, but essentially this is close to what we do. We have
:kmp:feature-a
:kmp:feature-b
:kmp:feature-c
:kmp:shared
. Where
:kmp:shared
depends on each of the feature modules. On Android we then have
:android:feature-a
that depends on
:kmp:feature-a
and then
:android:feature-b
that depends on
:kmp:feature-b
and so on.... But for iOS the
:kmp:shared
generates a Framework which the iOS codebase depends on
And somehow refactor our iOS libraries to use intermediate models instead of Kotlin
Yeah I'd say thats your best bet
r
Yes I understand that, we use it exactly the same but issue is that our "iOS App" depends on kmpshared and also on other frameworks, created by iOS programmers which are also using same Shared Kotlin library. In our company all of our products are using a few common algorithms and data structures that are essential and shared between a lot of libraries. It is not an issue in Android world, but it is in iOS
c
yes gotcha understand now. You'll have difficulty if trying to share classes across these frameworks. You'll need to either use c-interop to provide these to the kotlin shared library, or write some kind of intermediary layer like youve mentioned
r
Sure, that sounds like a good idea, thanks!
🙌🏼 1
c
you bet! Thanks for sharing your use case 👌🏼