Sharing Business Logic
Reuse across iOS and Android.
Sharing Business Logic is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Share Business Logic?
UI differs per platform, but rules like validation, pricing, and data flow are identical. Sharing them in commonMain removes duplication and bugs.
Shared Data Models
Define your domain models once in common code. With kotlinx.serialization they also serialize identically on both platforms.
import kotlinx.serialization.Serializable
@Serializable
data class Product(val id: Int, val name: String, val price: Double)Shared Use-Cases
Pure business rules live in common code and need no platform APIs.
class CartTotal {
fun total(items: List<Product>): Double =
items.sumOf { it.price }
}Shared Networking with Ktor
Ktor client is multiplatform. You write the API layer once; each platform supplies an HTTP engine.
import io.ktor.client.HttpClient
class Api(private val client: HttpClient) {
// suspend fun products(): List<Product> = client.get(...)
}Shared Coroutines
Coroutines work in common code, so asynchronous repositories are shared too. Suspend functions are exposed to both platforms.
class Repository(private val api: Api) {
suspend fun load(): List<Product> = api.products()
}The Repository Pattern
A common pattern: Repository coordinates network and cache, exposing clean methods. Both apps call the same repository.
Shared ViewModels
With libraries like Decompose or a shared ViewModel, presentation logic and state can also be shared, leaving only rendering native.
Exposing State as Flow
Expose state with StateFlow so Android collects it directly and iOS observes it via a wrapper.
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
class ProductState {
private val _items = MutableStateFlow<List<Product>>(emptyList())
val items: StateFlow<List<Product>> = _items
}Suspend Functions and iOS
Kotlin suspend functions are exposed to Swift as completion-handler or async methods. iOS calls them with Swift's async/await.
// Swift
// let products = try await repository.load()Keep UI Out of Shared Code
Resist sharing UI. Sharing logic but rendering natively gives the best user experience while still removing duplicated rules.
A Runnable Logic Example
The shared use-case is plain Kotlin. Here it runs standalone to compute a cart total.
data class Product(val name: String, val price: Double)
fun total(items: List<Product>): Double = items.sumOf { it.price }
fun main() {
val cart = listOf(Product("Pen", 2.5), Product("Book", 12.0))
println("Total: " + total(cart))
}Quick Check
In a well-structured KMM app, which of these is the best candidate to share, and which should stay native?
Recap
You can share the logic layer:
- Models, use-cases, repositories, Ktor networking, coroutines
- Expose state via
StateFlow; iOS uses async/await for suspend functions - Keep UI and navigation native
Next: accessing platform APIs.
Frequently asked questions
Is the “Sharing Business Logic” lesson free?
Yes — the full text of “Sharing Business Logic” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sharing Business Logic”?
Reuse across iOS and Android. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sharing Business Logic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- KMM Project Structure
- expect and actual
- Sharing Business Logic
- Platform APIs