Ktor 3.0.0 Help

Creating a cross-platform mobile application

The Ktor HTTP client can be used in multiplatform projects. In this tutorial, we'll create a simple Kotlin Multiplatform Mobile application, which sends a request and receives a response body as plain HTML text.

Prerequisites

First, you need to set up an environment for cross-platform mobile development by installing the necessary tools on a suitable operating system. Learn how to do this from the Set up an environment section.

Create a new project

  1. In Android Studio, select File | New | New Project.

  2. Select Kotlin Multiplatform App in the list of project templates, and click Next.

  3. Specify a name for your application, and click Next. In our tutorial, the application name is KmmKtor.

  4. On the next page, leave the default settings and click Finish to create a new project. Now, wait while your project is set up. It may take some time to download and set up the required components when you do this for the first time.

Configure build scripts

Update Kotlin Gradle plugins

Open the build.gradle.kts file and update versions of Kotlin Gradle plugins to the latest:

plugins { kotlin("android").version("1.9.10").apply(false) kotlin("multiplatform").version("1.9.10").apply(false) }

Add Ktor dependencies

To use the Ktor HTTP client in your project, you need to add at least two dependencies: a client dependency and an engine dependency. Open the shared/build.gradle.kts file and follow the steps below:

  1. Specify the Ktor version inside sourceSets:

    sourceSets { val ktorVersion = "3.0.0-eap-808" }
  2. To use the Ktor client in common code, add the dependency to ktor-client-core to the commonMain source set:

    val commonMain by getting { dependencies { implementation("io.ktor:ktor-client-core:$ktorVersion") } }
  3. Add an engine dependency for the required platform to the corresponding source set:

    • For Android, add the ktor-client-okhttp dependency to the androidMain source set:

      val androidMain by getting { dependencies { implementation("io.ktor:ktor-client-okhttp:$ktorVersion") } }

      For Android, you can also use other engine types.

    • For iOS, add the ktor-client-darwin dependency to iosMain:

      val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) dependencies { implementation("io.ktor:ktor-client-darwin:$ktorVersion") } }

Add coroutines

To add kotlinx.coroutines to your project, specify a dependency in the common source set. To do so, add the kotlinx-coroutines-core to the build.gradle.kts file of the shared module:

val commonMain by getting { dependencies { implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") } }

Then, open the androidApp/build.gradle.kts and add the kotlinx-coroutines-android dependency:

dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4") }

This allows us to use coroutines in Android code.

Click Sync Now in the top right corner of the gradle.properties file to install the added dependencies.

Update your application

Shared code

To update code shared between Android and iOS, open the shared/src/commonMain/kotlin/com/example/kmmktor/Greeting.kt file and add the following code to the Greeting class:

package com.example.kmmktor import io.ktor.client.* import io.ktor.client.request.* import io.ktor.client.statement.* class Greeting { private val client = HttpClient() suspend fun greeting(): String { val response = client.get("https://ktor.io/docs/") return response.bodyAsText() } }
  • To create the HTTP client, the HttpClient constructor is called.

  • The suspending greeting function is used to make a request and receive the body of a response as a string value.

Android code

To call the suspending greeting function from the Android code, we'll be using rememberCoroutineScope.

Open the androidApp/src/main/java/com/example/kmmktor/android/MainActivity.kt file and update MainActivity code as follows:

import androidx.compose.runtime.* import kotlinx.coroutines.launch class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { val scope = rememberCoroutineScope() var text by remember { mutableStateOf("Loading") } LaunchedEffect(true) { scope.launch { text = try { Greeting().greeting() } catch (e: Exception) { e.localizedMessage ?: "error" } } } GreetingView(text) } } } } }

Inside the created scope, we can call the shared greeting function and handle possible exceptions.

iOS code

  1. Open the iosApp/iosApp/iOSApp.swift file and update the entry point for the application:

    import SwiftUI @main struct iOSApp: App { var body: some Scene { WindowGroup { ContentView(viewModel: ContentView.ViewModel()) } } }
  2. Open the iosApp/iosApp/ContentView.swift file and update ContentView code in the following way:

    import SwiftUI import shared struct ContentView: View { @ObservedObject private(set) var viewModel: ViewModel var body: some View { Text(viewModel.text) } } extension ContentView { class ViewModel: ObservableObject { @Published var text = "Loading..." init() { Greeting().greeting { greeting, error in DispatchQueue.main.async { if let greeting = greeting { self.text = greeting } else { self.text = error?.localizedDescription ?? "error" } } } } } }

    On iOS, the greeting suspending function is available as a function with a callback.

Enable internet access on Android

The final thing we need to do is to enable internet access for the Android application. Open the androidApp/src/main/AndroidManifest.xml file and enable the required permission using the uses-permission element:

<manifest> <uses-permission android:name="android.permission.INTERNET" /> <application> ... </application> </manifest>

Run your application

To run the created multiplatform application on the Android or iOS simulator, select androidApp or iosApp and click Run. The simulator should display the received HTML document as plain text.

Android simulator
iOS simulator
Last modified: 09 December 2022