Sharing a Domain Layer, Keeping Native UI
The low-drama KMP position: share what has one right answer, keep native what has two. What goes in each bucket, the shared-test win that's the real payoff, the Swift interop frictions nobody mentions, and the distribution decision that determines adoption.
Hessam Rastegari
AI Native Engineer · 12 years shipping Android
TL;DR — Share what has one right answer (business rules, validation, money and date logic,
networking, caching policy) and keep native what has two (UI, navigation, platform integration). The real
payoff isn't lines saved — it's that commonTest proves your business rules behave identically on both
platforms, which removes the divergence bugs that actually reach users. Budget for three interop frictions:
sealed classes lose exhaustiveness in Swift, default arguments don't cross, and nullability of collections
gets loose. And the decision that determines adoption isn't technical: publish a versioned XCFramework from
CI so the iOS team never runs Gradle.
The split
An earlier post in this series argued that sharing logic and sharing UI are different bets. This is the concrete version of the first bet — the one I'd defend on most teams.
One right answer → share it.
- Domain models and their invariants
- Validation rules
- Business logic: pricing, discounts, eligibility, state transitions
- Networking (Ktor) and the API contract
- Caching and offline policy
- Serialization
- Money arithmetic and rounding
- Date and time logic — especially "what counts as a day"
- Feature-flag evaluation
- Analytics event definitions
Two right answers → keep it native.
- UI, in every respect
- Navigation
- Platform integration: notifications, widgets, share sheets, biometrics
- Anything a platform HIG has an opinion about
The judgement call: the presentation layer. A shared ViewModel is technically fine and genuinely useful on some teams; on others it's the thing that makes iOS engineers feel the architecture was imposed on them. It isn't a technical question, and it's worth deciding explicitly rather than by drift.
The real payoff is shared tests
The pitch is usually "share 40% of your code". That's the wrong number to lead with, because nobody's problem is that they typed too much.
The actual problem is divergence: two implementations of the same rule that agree for a year and then don't. A refund cap applied on Android and not on iOS. A discount that rounds differently. An order that's cancellable on one platform and not the other.
// commonTest — runs on JVM AND on iOS targets
class RefundPolicyTest {
@Test
fun `refund is capped at the original charge`() {
assertEquals(5000, policy.refund(charge = 5000, ask = 8000))
}
@Test
fun `partial refunds round to the customer's benefit`() {
assertEquals(334, policy.refund(charge = 1000, ask = 333.4))
}
}
One test suite, two platforms, and the rules are now provably identical rather than hopefully identical. That's the argument that lands with product people too, because divergence bugs are the ones that produce refund discrepancies and support tickets.
Module structure
Don't build one giant :shared.
:shared:model pure Kotlin, no dependencies
:shared:domain use cases, business rules
:shared:data Ktor, SQLDelight/Room, caching
:shared:umbrella assembles the XCFramework
Separate modules keep the dependency direction honest, keep :shared:model free of anything platform-adjacent,
and let you share the model layer with a backend later if that ever becomes interesting.
The umbrella module exists purely because Xcode wants one framework. Keep it thin.
The boundary API
Expose Flow and suspend functions, and be deliberate about what Swift sees.
// commonMain
class OrderRepository(private val api: OrderApi) {
fun observe(id: String): Flow<OrderState> = …
suspend fun cancel(id: String): CancelResult = …
}
Android consumes this natively. Swift needs help, and SKIE is worth adopting on day one — it maps Flow
to AsyncSequence, restores exhaustive switch over sealed classes, and handles default arguments. Without
it you'll hand-write wrappers and the iOS team will conclude, reasonably, that the shared layer is awkward.
Three frictions the pitch doesn't mention
1. Sealed classes lose exhaustiveness
sealed interface OrderState {
data object Pending : OrderState
data class Shipped(val eta: Instant) : OrderState
data class Cancelled(val reason: String) : OrderState
}
In Swift, without SKIE, that's a class hierarchy with no compiler-enforced exhaustive switch. Your carefully
modelled state machine stops being checked at exactly the boundary where a missed case is most likely.
Mitigate with SKIE, or expose an enum discriminant at the boundary for the states Swift must branch on.
2. Default arguments don't cross
fun search(query: String, limit: Int = 20, offset: Int = 0)
Swift sees one function requiring all three parameters. Every ergonomic default you designed becomes an explicit argument at every call site. SKIE handles this too; otherwise, provide explicit overloads for the combinations Swift actually uses rather than making the iOS team pass everything.
3. Collection nullability gets loose
Kotlin's nullability guarantees don't survive fully into Objective-C interop for generics. A
List<String> can arrive in Swift as [String]?. Not dangerous, but it produces defensive unwrapping
throughout the Swift codebase, which reads as friction caused by the shared layer.
Worth knowing so you can flag it as a known cost rather than letting it accumulate as a vague complaint.
The decision that actually determines adoption
Not technical: how the iOS team consumes the shared code.
If building the iOS app requires running Gradle, the adoption fails. Not because it's hard, but because it makes the shared module a daily tax on people who didn't choose it, and every unrelated build problem gets attributed to Kotlin.
CI on merge to main:
./gradlew :shared:umbrella:assembleXCFramework
→ version it (semver, tied to the commit)
→ publish to SPM (or a CocoaPods spec repo)
Now iOS engineers add a versioned package like any other dependency. They pull a new version when they want one. They can pin an old one if something breaks. They only need Kotlin tooling if they're changing shared code — which some of them will start doing, voluntarily, once the boundary is comfortable.
Handle this first, not last. Teams routinely get the architecture right and lose the adoption on distribution.
Measuring whether it worked
Not "percentage of code shared". That number is easy to move and means nothing.
- Divergence bugs per quarter — the count of bugs where the two apps behaved differently. This should drop sharply, and it's the reason you did it.
- Time to ship a rule change on both platforms. Previously two tickets, two reviews, two release cycles.
- Does the iOS team open the shared module voluntarily? The honest adoption signal.
The rule
Share what has one right answer; keep native what has two. Business rules are the same everywhere, so
implementing them twice is a bug waiting for a deadline — and commonTest turns "hopefully identical" into
"provably identical". Ship it as a versioned framework so nobody has to run your build, and count divergence
bugs rather than percentages.