Hello, Is there a FAQ for KMP? What's the recommen...
# multiplatform
a
Hello, Is there a FAQ for KMP? What's the recommended architecture patterns for the navigation, UI sharing, logic sharing? The questions are quite fundamental so I am expecting there should be already answers to that. Thanks
z
I personally recommend MVVM architecture
👆 1
p
The recommendation is the same as Google architecture docs. Just koin instead of hilt. The standard Google
CLEAN+MVVM arch
with
navigation-compose
+
lifecycle-viewmodel
+
koin
p
There are other questions to ask - How do you want to connect the Android and iOS apps with the shared code? • CocoaPods / Swift Package Manager • Direct integration • External integration using KMMBridge How many shared modules do you have • Single shared module between Android and iOS • Multiple, using an umbrella module to expose them Repo Structure - • Single monorepo with Android and iOS apps together • Separate repo • Separate teams, sharing external framework using KMMBridge (see https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-project-configuration.html)
👆 1
u
there option to share via SPM?
👌 1
p
Plenty of ways to integrate with SPM
u
1st party?
p
Yeup. Apple first party.
Direct integration with SPM has always existed
u
how so? I thought kmp only suppoeted cocoapods + direct
p
Although JB is working on improving it
Cocoapods has better integration with 3rd party libraries that are objective-c compatible
With swift you gotta jump into the DI game
u
okay so how? for kmp to produce a framework and then create myself a spm package and include that framework there somehow?
p
SPM has an option to include binaryTarget() from a local file. Then you pass the directory where your xcframework lives. It only supports .xcframework not .framework
u
so I need to turn kmp framrwork to .xcframework?
p
Yes, this is a bare SPM integration. JB is working on the KMP plugin to improve some stuff.
u
is there some script to turn it to a xcfr?
so I can just stick it at the end of the gradle task?
p
I suggest you look at Touchlab's KMMBridge
u
yea im aware now but jb are working on this story am I correct?
p
I follow here: https://youtrack.jetbrains.com/issue/KT-53877 The easiest way is just to use XcFramework() api in Gradle directly. When SPM support is out maybe the XcFramework() is not needed anymore.
u
dont they need to do swift support first?
I though spm was swift only
p
My experience with binaryTarget is not bad but one has to run the gradleTask
assembleAppleXcframework
which feels a bit slower than the
embedAndSign
...
u
btw, how is build speed for you? I've never build a kmp produnction app other than the demos. Is it on par or slower than pure android compilation?
p
I though spm was swift only
SPM just sees an .xcframework it doesn't know what generated it
In short it is a bit slower in my perception. Acceptable but slow. I am not on the latest hardware, that matters too. Sometimes is not Gradle but Xcode build stuff
u
so how is your usual workflow look like? do you build for android primarily, and then sometimes build the ios?
p
Well I haven't shipped to production yet but to your question. I normally developed for desktop because it is faster. Once I complete the feature then test Android/iOS/web in that order and fix whatever minor platform alignment is needed. Then run assembleAppleXcframework task and jump into Xcode. Xcode is slow to pick up the changes because it has to do full compilation every time you do a change in KMP. But normally the KMP interfaces don't change too often.
u
right, so ios is not built every build you do, correct?
p
I try not to. But yes any minimum change in the KMP interfaces will require to rebuild the xcframework. But as I said, I try to minimize changes in the interface APIs that bridge the 2 platforms. Changes in pure swift pick up instantaneously as expected. And also changes in kotlin gets picked up by Android Studio. Is the interfaces that bridge the 2 platforms what should minimize changing often. But even when they do, is not traumatic slow the full rebuild.
u
right, so if I only build android - then the build speed is as if I wasn't doing kmp at all, or not?
p
Correct 💯 , unnoticeable
u
okay, thats great
and for the ios..ballpark..50% slower?
p
I don't have numbers but I would dare to say so yes
t
Using XCframeworks is a ton slower to build, because it usually builds for all your platforms unless you set it up in a custom way. We recommend using the
embed...
task directly from Xcode's Run Script and update build settings in xcode to find the framework correctly. That way you always build only what's needed and don't need to do anything extra. Just hit build or run in Xcode and Gradle will take care of building the correct framework if there were changes. With Gradle Configuration Cache, when there are no changes the
./gradlew embed...
invocation is usually under 500ms
💯 1
👍 1
u
neat, thanks!
p
Thanks for confirming. I really can tell the difference in speed. I found out the hard way 🥴