Kotlin Academy · Lesson

Sharing Business Logic

Reuse across iOS and Android.

Lesson 3 of 413 steps

Sharing Business Logic is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Sharing Business Logic” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Sharing Business Logic”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. KMM Project Structure
  2. expect and actual
  3. Sharing Business Logic
  4. Platform APIs
← Back to Kotlin Academy