Setting Up
This page adds Transmission 2.0 to a Kotlin Multiplatform or Android project and wires it into ViewModels, dependency injection and Compose.
Requirements
| Minimum | |
|---|---|
| Kotlin | 2.3 (the library is built with 2.4) |
| JVM and Android bytecode | Java 21 |
| Android | minSdk 23 |
| iOS targets | iosArm64, iosSimulatorArm64 (no iosX64) |
Dependencies
Transmission 2.0 is published to Maven Central under the com.yigitozgumus group.
| Artifact | Needed for |
|---|---|
com.yigitozgumus:transmission |
The library: contracts, transformers, routers |
com.yigitozgumus:transmission-test |
The test DSL (test source sets only) |
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel |
ViewModel and viewModelScope on every platform, if you use ViewModels |
transmission-viewmodel only contains the deprecated RouterViewModel; the builder API doesn't need it. See
Classic API: Setup.
Kotlin Multiplatform
// shared/build.gradle.kts
plugins {
kotlin("multiplatform")
id("com.android.kotlin.multiplatform.library") // AGP 9: the Android target of a KMP library
}
kotlin {
android {
namespace = "com.example.shared"
compileSdk = 37
minSdk = 23
}
jvm()
iosArm64()
iosSimulatorArm64()
sourceSets {
commonMain.dependencies {
implementation("com.yigitozgumus:transmission:2.0.0-beta02")
implementation("org.jetbrains.androidx.lifecycle:lifecycle-viewmodel:2.11.0")
}
commonTest.dependencies {
implementation(kotlin("test"))
implementation("com.yigitozgumus:transmission-test:2.0.0-beta02")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.11.0")
}
}
}
Android Only
// app/build.gradle.kts
dependencies {
implementation("com.yigitozgumus:transmission:2.0.0-beta02")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:<version>")
testImplementation("com.yigitozgumus:transmission-test:2.0.0-beta02")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.11.0")
}
Check GitHub Releases for the latest version.
Project Structure
Group code by feature. Each feature has a contracts file (its messages, state and Contracts object), a transformer
and a ViewModel, as in the shop sample:
shared/src/commonMain/kotlin/com/example/shop/
├── cart/
│ ├── CartContracts.kt # CartSignal, CartEffect, CartState, object Cart : Contracts()
│ ├── CartTransformer.kt # fun cartTransformer() = transformer(Cart) { … }
│ └── CartViewModel.kt # viewModelScope.router("cart", …)
├── catalog/
├── checkout/
└── di/
└── ShopModule.kt # creates transformers and ViewModels
A ViewModel
A ViewModel creates its router from viewModelScope, so the router and its transformers are cleared with it:
class CartViewModel(cart: Transformer, promotions: Transformer) : ViewModel() {
private val router = viewModelScope.router("cart-router", cart, promotions) { global() }
val state: StateFlow<CartState> = router.state(Cart.state)
fun applyPromo(code: String) = router.process(CartSignal.ApplyPromo(code))
}
Pass transformers in rather than creating them inside the ViewModel, so tests and DI can choose them.
Dependency Injection
Transformers are created per router: each ViewModel gets new instances. Never share a transformer between routers or make it a singleton.
Koin
val dataModule = module {
single<ProductRepository> { RemoteProductRepository(get()) }
single<PaymentGateway> { StripePaymentGateway(get()) }
}
val viewModelModule = module {
viewModel { CatalogViewModel(catalogTransformer(get())) }
viewModel { CartViewModel(cartTransformer(), promotionTransformer()) }
viewModel { CheckoutViewModel(checkoutTransformer(get())) }
}
fun initKoin() = startKoin { modules(dataModule, viewModelModule) }
Hilt
@HiltViewModel
class CheckoutViewModel @Inject constructor(paymentGateway: PaymentGateway) : ViewModel() {
private val router = viewModelScope.router("checkout-router", checkoutTransformer(paymentGateway)) { global() }
val state = router.state(Checkout.state)
fun start() = router.process(CheckoutSignal.Start)
}
Hilt injects the transformer's dependencies. The ViewModel calls the transformer function itself, so each ViewModel gets new transformer instances.
Without DI
Call the transformer functions where you create the ViewModel:
val viewModel = viewModel { CartViewModel(cartTransformer(), promotionTransformer()) }
Compose
@Composable
fun CartScreen(viewModel: CartViewModel = koinViewModel()) {
val cart by viewModel.state.collectAsState()
CartContent(
cart = cart,
onApplyPromo = viewModel::applyPromo,
)
}
router.state(...) is a StateFlow, so the screen starts from the current value and needs no initial loading state
of its own.
ProGuard / R8
If you use ProGuard or R8, add:
-keep class com.yigitozgumus.transmission.** { *; }
-keepclassmembers class * implements com.yigitozgumus.transmission.Transmission { *; }
Older Versions
- 1.6.0 to 1.8.0 were published as
com.trendyol:transmission, withcom.trendyol.transmission.*packages. - 1.5.0 and earlier were published on JitPack as
com.github.Trendyol:transmission:<version>, from thehttps://jitpack.iorepository.
To move from 1.x, see Migrating to 2.0.