Verified integration examples¶
Start with the setup guide for dependencies and native registration, then
use the API reference for defaults, exceptions, and lifecycle rules.
These examples track main, not necessarily the latest published Maven release.
The code blocks below are included directly from compiled/tested source files when MkDocs builds the site. They are not separately maintained copies. In the raw Markdown, follow the source links to read the complete code. Missing included files fail the documentation build.
Shared parsing and serializable parameters¶
This complete test class demonstrates parsing a percent-encoded query, deserializing a
model, building an internal URL, and handling a missing parameter. It requires the
kotlinx.serialization compiler plugin and JSON dependency; assertions use kotlin.test.
The sample’s simple JSON value is safe for internal dispatch; buildUrl does not
percent-encode values for external delivery.
package dev.theolm.rinku
import dev.theolm.rinku.models.DeepLinkParam
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class DocumentationExamplesTest {
@Serializable
data class User(val name: String)
@Test
fun parseEncodedLink() {
val link = DeepLink(
"https://example.com/second/third?user=%7B%22name%22%3A%22Theo%22%7D"
)
assertEquals("https", link.schema)
assertEquals("example.com", link.host)
assertEquals(listOf("second", "third"), link.pathSegments)
assertEquals(User("Theo"), link.getParameter("user", User.serializer()))
}
@Test
fun buildInternalLink() {
val parameter = DeepLinkParam("user", User("Theo"), User.serializer())
val url = Rinku.buildUrl("https://example.com/second", parameter)
assertEquals("{\"name\":\"Theo\"}", parameter.serialize())
assertEquals(User("Theo"), DeepLink(url).getParameter("user", User.serializer()))
}
@Test
fun buildLinkFromComponents() {
val url = Rinku.buildUrl(schema = "https", host = "example.com", path = "second/third")
assertEquals("https://example.com/second/third", url)
}
@Test
fun rejectMissingParameter() {
val link = DeepLink("https://example.com/second")
assertFailsWith<IllegalArgumentException> {
link.getParameter("user", User.serializer())
}
}
}
Run just these examples on JVM:
./gradlew :rinku-core:jvmTest --tests 'dev.theolm.rinku.DocumentationExamplesTest'
They also belong to the shared test suite run by the existing iOS and JS/Wasm CI jobs. No platform adapter or OS scheme registration is needed for these parsing tests.
Shared Compose listener¶
The Voyager sample keeps a single DeepLinkListener in its shared root. Navigation
is application-owned: toScreenStack() and screens.First belong to the sample, not
Rinku. Use your own route interpretation if you do not use Voyager.
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.Navigator
import dev.theolm.rinku.DeepLink
import dev.theolm.rinku.compose.ext.DeepLinkListener
import screens.First
/**
* Represents the root composable function of the application. It listens for deep link changes
* and updates the UI accordingly by navigating to the relevant screen.
* Depending on the deep link received, the application navigates to different screens within the app.
*/
@Composable
fun App() {
// State holding the current deep link. Initially, there is no deep link (null).
var deepLink by remember { mutableStateOf<DeepLink?>(null) }
// Listens for deep link changes. When a deep link is received, the state is updated.
DeepLinkListener {
deepLink = it
}
// Renders the main screen of the application, passing the current deep link.
MainScreen(deepLink)
}
/**
* The main screen of the application. It decides which screen stack to display based on the
* current deep link.
*
* @param deepLink The current deep link received by the application. It may be null, indicating
* that no deep link is currently being handled.
*/
@Composable
private fun MainScreen(deepLink: DeepLink?) {
// Calculates the screen stack to be displayed based on the deep link. This is recalculated
// every time the deep link changes.
val screenStack = remember(deepLink) {
deepLink.toScreenStack()
}
MaterialTheme {
// Initializes the navigator with the first screen. Depending on the current screen stack,
// it may navigate to different screens within the app.
Navigator(screen = First()) {
CurrentScreen()
// Replaces the entire screen stack based on the current deep link. This ensures that
// the app navigates to the appropriate screen.
remember(screenStack) {
it.replaceAll(screenStack)
}
}
}
}
For a non-Compose collector, use listenForDeepLinks in a lifecycle-owned coroutine
scope; see common setup. Do not collect simultaneously through
both APIs expecting a broadcast.
Android entry point¶
This activity uses the Compose adapter instead of RinkuInit. App is the shared
root above. Install the sample to exercise native links; intent filters remain required.
package dev.theolm.rinku
import App
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import dev.theolm.rinku.compose.ext.Rinku
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Rinku {
App()
}
}
}
}
@Preview
@Composable
fun AppAndroidPreview() {
App()
}
./gradlew :samples:voyager:androidApp:assembleDebug
The Android CI job compiles this entry point and runs Maestro deep-link flows.
iOS entry point¶
This is the sample’s AppDelegate-based entry point, not a SwiftUI @main lifecycle.
ComposeApp is the exported shared framework; ContentView is the sample’s SwiftUI
host. Forward URL events from your SceneDelegate instead if your application uses
scenes, including its initial connection events.
import SwiftUI
import ComposeApp
@UIApplicationMain
class AppDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
let rinku = RinkuIos.init(deepLinkFilter: nil, deepLinkMapper: nil)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainViewController = UIHostingController(rootView: ContentView())
self.window!.rootViewController = mainViewController
self.window!.makeKeyAndVisible()
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
rinku.onDeepLinkReceived(url: url.absoluteString)
return true
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
rinku.onDeepLinkReceived(userActivity: userActivity)
return true
}
}
Open samples/voyager/iosApp/iosApp.xcodeproj on macOS and run the iosApp scheme on
an Apple Silicon simulator or device. The existing CI tests the Kotlin iOS adapter
and compiles the Compose extension, but does not build this Swift application.
Verify Swift integration and cold/warm native URL delivery in Xcode before shipping.
JVM desktop entry point¶
Initialize before creating windows, return immediately when forwarding to a primary
instance, and close the adapter after the UI exits. App is the shared root above.
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import dev.theolm.rinku.RinkuDesktop
fun main(args: Array<String>) {
RinkuDesktop(
applicationId = "dev.theolm.rinku.sample.voyager",
schemes = setOf("rinku-voyager"),
).use { rinku ->
if (rinku.start(args) == RinkuDesktop.StartResult.FORWARDED) return
application(exitProcessOnExit = false) {
Window(onCloseRequest = ::exitApplication, title = "Rinku — Voyager") {
App()
}
}
}
}
./gradlew :samples:voyager:desktopApp:run --args='rinku-voyager://example/second/third'
Desktop CI compiles this sample on macOS, Windows, and Linux. Running through Gradle does not register a native URL scheme; see packaging and OS verification.
Web entry point and adapter ownership¶
The entry point creates the browser UI. WebDemo owns the Rinku adapter and implements
application-controlled history buttons. Do not add another adapter around it.
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.ComposeViewport
import dev.theolm.rinku.common.WebDemo
import kotlinx.browser.document
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
ComposeViewport(document.getElementById("app")!!) {
WebDemo { App() }
}
}
Web adapter and history source
package dev.theolm.rinku.common
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import dev.theolm.rinku.Rinku
import dev.theolm.rinku.compose.ext.Rinku as RinkuHost
import dev.theolm.rinku.models.DeepLinkFilter
import dev.theolm.rinku.models.DeepLinkMapper
import kotlinx.browser.document
import kotlinx.browser.window
/** History writes belong to this sample application, never to the Rinku adapter. */
@Composable
fun WebDemo(content: @Composable () -> Unit) {
val origin = window.location.origin
// Set this meta tag when deploying under a prefix, for example /rinku/.
val basePath = remember {
document.querySelector("meta[name='rinku-base-path']")?.getAttribute("content") ?: "/"
}
val filter = remember(origin) {
object : DeepLinkFilter {
override fun isValid(url: String) = url.startsWith("$origin/")
}
}
val mapper = remember(origin, basePath) {
object : DeepLinkMapper {
override fun map(url: String) = if (url.startsWith("$origin$basePath")) {
"$origin/" + url.removePrefix("$origin$basePath")
} else {
url
}
}
}
RinkuHost(deepLinkFilter = filter, deepLinkMapper = mapper) {
Column(Modifier.fillMaxSize()) {
Text("History is controlled by the sample; Rinku receives initial URLs and Back/Forward.")
Row {
Button(onClick = { window.history.back() }) { Text("Browser back") }
Button(onClick = { window.history.forward() }) { Text("Browser forward") }
}
Row {
listOf("", "second", "second/third").forEach { route ->
Button(onClick = {
window.history.pushState(null, "", "$basePath$route?source=web")
// pushState does not fire popstate. Notify shared navigation explicitly,
// applying the same filter/mapper as the browser adapter, exactly once.
Rinku.handleDeepLink(window.location.href, applyFilter = true, applyMapper = true)
}) { Text(route.ifEmpty { "Home" }) }
}
}
Column(Modifier.weight(1f)) { content() }
}
}
}
Run one target at a time:
./gradlew :samples:voyager:webApp:jsBrowserDevelopmentRun
./gradlew :samples:voyager:webApp:wasmJsBrowserDevelopmentRun
Web CI bundles both targets and runs shared, browser-history, and Compose lifecycle tests. Direct navigation/reload also requires an SPA fallback from your production host. See Web hosting and deployment prefixes.
Documentation checks¶
python -m pip install mkdocs-material
mkdocs build --strict
The documentation-checks CI job builds these pages on pull requests. Executable source coverage comes from the existing platform CI jobs described above, not from compiling the API signature summaries in the reference. Native OS registration and packaged application delivery still require platform-specific manual checks.