Ktorを追加してPlatform Enginesを選ぶ
ターゲットごとにOkHttpとDarwin engineを接続します
「Ktorを追加してPlatform Enginesを選ぶ」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
One HTTP Client Everywhere
Ktor Client is a multiplatform HTTP library, so you write networking once in commonMain and it runs on Android and iOS alike.
Core Plus Engine
Ktor splits into a shared core and per-platform engines. The core gives the API, the engine does the actual socket work.
Add the Core Dependency
In your shared module you add ktor-client-core to commonMain so every target can build requests with the same API.
implementation("io.ktor:ktor-client-core:2.3.12")Android Wants OkHttp
For Android you pick the OkHttp engine, a battle-tested JVM client that Ktor wraps cleanly behind its shared API.
implementation("io.ktor:ktor-client-okhttp:2.3.12")iOS Wants Darwin
For iOS you pick the Darwin engine, which runs on Apple's native URLSession under the hood for true platform networking.
implementation("io.ktor:ktor-client-darwin:2.3.12")Engines Live in Source Sets
Each engine belongs in its own source set: OkHttp in androidMain and Darwin in iosMain, never in commonMain.
androidMain.dependencies { implementation(okhttp) }Create the Client Once
In commonMain you create an HttpClient with no engine argument, and Ktor auto-selects the one bundled for that platform.
val client = HttpClient()Auto Engine Selection
Because only one engine ships per target, Ktor's auto-selection finds it. Your shared code stays free of any platform import.
Or Pass an Engine
You can also pass an engine explicitly, which is handy when you want per-platform config like timeouts or proxies.
val client = HttpClient(OkHttp)Close When Done
The client holds real resources, so call close when you no longer need it to release connections cleanly.
client.close()Why Split This Way
This core-plus-engine design lets Ktor reuse each OS's best networking stack while you keep one shared API for all of them.
Quick Check
Which engine should an iOS target use?
Recap
Ktor shares a core API but needs an engine per platform: OkHttp for Android, Darwin for iOS. Auto-selection keeps shared code clean.
AI チューターと学ぶ Kotlin — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「Ktorを追加してPlatform Enginesを選ぶ」レッスンは無料ですか?
はい。「Ktorを追加してPlatform Enginesを選ぶ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「Ktorを追加してPlatform Enginesを選ぶ」で何を学びますか?
ターゲットごとにOkHttpとDarwin engineを接続します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「Ktorを追加してPlatform Enginesを選ぶ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Ktorを追加してPlatform Enginesを選ぶ
- 最初のGETリクエスト
- POST、Headers、Query Params
- Timeouts、Logging、Base URLs