Kotlin Multiplatform (KMP)

DivKit provides a library for using in Kotlin Multiplatform applications for both iOS and Android platforms. The library is published in Maven Central inside group com.yandex.divkit.multiplatform and versioned in accordance with the versions of the main DivKit library.

Usage

1) Gradle dependency

Add the library as a dependency of your shared module:

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("com.yandex.divkit.multiplatform:divkit-multiplatform:<divkit-version>")
        }
    }
}

2) iOS: add the DivKitKMP product through SPM

In Xcode, choose File → Add Package Dependencies, enter the DivKit iOS package URL, and select Exact Version with the same <divkit-version> as the Maven dependency. Add the DivKitKMP product to your iOS application target. It includes the required DivKit and DivKitExtensions dependencies.

Configure your shared Kotlin module to produce a static iOS framework:

View Gradle configuration
kotlin {
    listOf(iosArm64(), iosSimulatorArm64()).forEach { target ->
        target.binaries.framework {
            baseName = "Shared"
            isStatic = true
        }
    }
}

Connect that framework to the application using Kotlin's direct integration. The application links both the Kotlin framework and the SPM product. The Maven dependency contains the Kotlin bindings, while SPM provides the native implementation.

Note

Use the same release version for the Maven dependency and the DivKit iOS Swift package, and update them together.

When migrating an existing app, remove the DivKitKMP entry from its Podfile and remove any explicit DivKit pods that are replaced by SPM. Update the Pods integration if the app still uses other pods. If the Kotlin framework itself was integrated through CocoaPods, follow the migration steps in Kotlin's direct integration guide as well.

3) Render DivKit card in Kotlin multiplatform

You can create DivKit scope using function DivKit and pass common DivKitDependencies in it from shared code. Inside this scope you can call DivView function to render JSON and call setVariables to set variables to global storage.

In common code:

DivKit(dependencies) {
    var greeting by variable(name = "greeting", value = "Hello, Username")
    Column(
        modifier = Modifier.padding(WindowInsets.safeDrawing.asPaddingValues()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        DivView(
            cardId = "Sample",
            jsonData = jsonString,
            modifier = Modifier.wrapContentSize()
        )
        Button(
            onClick = {
                greeting = "Hello, DivKit User"
            },
            content = {
                Text("Say \"Hello\"")
            }
        )
    }
}

Also you can setup some platform-specific dependencies in platform-specific code. For example:

In Android part:

val environment = DivKitEnvironment.Builder(baseContext = this)
    .imageLoaderFactory { ctx -> CoilDivImageLoader(ctx) }
    .lifecycleOwner(this)
    .build()

setContent {
    inject(environment) {
        MainScreen()
    }
}

In the iOS part, the makeDivKitView method returns DivKitKMPView, which automatically handles:

  • intrinsicContentSize and sizeThatFits for proper size determination
  • Layout updates when bounds change
  • Calling onVisibleBoundsChanged when the visible area changes

Learn more

You can discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_en.

DivKit Repository