Skip to main content

Coil 3 Image Loading in Kotlin Multiplatform (KMM) – A Complete Guide

Coil 3 Image Loading in Kotlin Multiplatform (KMM) – Complete Guide | KMP with Suraj

Coil 3 Image Loading in Kotlin Multiplatform (KMM) – A Complete Guide

Author: Suraj Sharma – kmpwithsuraj.com

Introduction

Loading images efficiently in a Kotlin Multiplatform Mobile (KMM) project can be challenging. You need a solution that works across Android, iOS, Desktop, and Web. Coil 3 v3.3.0 is a lightweight, Kotlin-first image loading library designed for Compose Multiplatform. It works perfectly with Ktor v3.3.0 for network requests. Whether you are building profile screens, product catalogs, or news feeds, Coil 3 makes image loading simple with support for placeholders, error handling, caching, and animations.

Coil 3 v3.3.0 and Ktor v3.3.0 Dependencies

Add these entries in your libs.versions.toml:

# ==============================
# Coil 3 Image Loading Setup
# ==============================
coil3 = "3.3.0"
coil3-network-ktor = { module = "io.coil-kt.coil3:coil-network-ktor3", version.ref = "coil3" }
coil3-compose       = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil3" }

# ==============================
# Ktor HTTP Clients
# ==============================
ktor = "3.3.0"
ktor-client-okhttp  = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }   # Android
ktor-client-darwin  = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }   # iOS
ktor-client-cio     = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }      # Desktop (JVM)
ktor-client-js      = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }      # Web
        

Build Script Setup

Add Coil 3 v3.3.0 and Ktor v3.3.0 dependencies in your build.gradle.kts:

androidMain.dependencies {
    implementation(compose.preview)
    implementation(libs.androidx.activity.compose)
    implementation(libs.ktor.client.okhttp)
}

commonMain.dependencies {
    implementation(compose.runtime)
    implementation(compose.foundation)
    implementation(compose.material3)
    implementation(compose.ui)
    implementation(compose.components.resources)
    implementation(compose.components.uiToolingPreview)
    implementation(libs.androidx.lifecycle.viewmodelCompose)
    implementation(libs.androidx.lifecycle.runtimeCompose)
    implementation(libs.coil3.network.ktor)
    implementation(libs.coil3.compose)
}

jvmMain.dependencies {
    implementation(compose.desktop.currentOs)
    implementation(libs.kotlinx.coroutinesSwing)
    implementation(libs.ktor.client.cio)
}

iosMain.dependencies {
    implementation(libs.ktor.client.darwin)
}

wasmJsMain.dependencies {
    implementation(libs.ktor.client.js)
}
        

Tip: Coil 3 works best if your project already uses Ktor for API calls. This ensures smooth network image fetching across platforms.

Using AsyncImage in Compose Multiplatform

Example: Loading a news banner image with Coil 3 v3.3.0:

AsyncImage(
    model = url, 
    contentDescription = "News Banner",
    placeholder = painterResource(Res.drawable.placeholder),
    error = painterResource(Res.drawable.image_error),
    contentScale = ContentScale.Crop,
    modifier = Modifier
        .fillMaxWidth()
        .height(180.dp)
        .clip(RoundedCornerShape(16.dp))
)
        

Explanation:

  • model – Image URL, with automatic caching.
  • placeholder – Temporary image while loading.
  • error – Displayed if loading fails.
  • contentScale – Adjusts how the image fits the space.
  • clip – Rounded corners for better UI.

Why Coil 3 is Ideal for KMM Developers

  • Cross-platform ready: Works on Android, iOS, Desktop, and Web.
  • Network integration: Perfect with Ktor v3.3.0.
  • Lightweight & Kotlin-first: Minimal boilerplate.
  • AsyncImage support: Placeholders and error handling improve UX.
  • Customizable: Rounded corners, circle crop, borders, crossfade animations.

Example Project

Check a working Coil 3 v3.3.0 + KMM example project here: GitHub Repo

Conclusion

Using Coil 3 v3.3.0 in KMM projects is a clean and efficient solution for image loading on multiple platforms. It simplifies network image fetching, caching, and UI rendering in Compose, saving time for KMM and Compose Multiplatform developers.

For more tutorials, tips, and Kotlin Multiplatform insights, visit KMP with Suraj.

Comments

Popular posts from this blog

Build Once, Run Anywhere: A Simple Weather App using Kotlin Multiplatform (KMM/KMP/CMP)

Simple Weather App using Kotlin Multiplatform (KMM/KMP) Simple Weather App using Kotlin Multiplatform (KMM/KMP) 🚀 In today’s cross-platform app development world, Kotlin Multiplatform (now popularly known as KMP or KMM ) is a game-changer. It allows us to share business logic across Android and iOS without compromising the native feel and performance of the UI. As a passionate mobile developer, I recently built a Simple Weather Application using KMP (Kotlin Multiplatform Project) . This app is a real-world example of how you can target both Android and iOS platforms with a single codebase using: 💻 Jetpack Compose for Android UI 🍎 SwiftUI for iOS UI ⚙️ Shared business logic in Kotlin 🔗 GitHub Project Link 🌟 Why Kotlin Multiplatform (KMP)? Unlike Flutter or React Native, KMP doesn't replace the native UI — instead, it empowers developers to write platform-s...

Understanding Kotlin Yarn Lock in KMM Projects

Kotlin Yarn Lock in KMM Projects Understanding Kotlin Yarn Lock in KMM Projects As a Kotlin Multiplatform Mobile (KMM) developer, working with JavaScript (JS) targets is a crucial aspect when building truly cross-platform applications. In this post, we’ll dive into the importance of the yarn.lock file in KMM projects, how it functions, and where it fits within your project structure. What is yarn.lock ? The yarn.lock file is a critical component in any KMM project that targets JavaScript, whether it's jsBrowser or jsNode . When managing JavaScript dependencies with Yarn, the yarn.lock file locks down the versions of all installed packages, ensuring that every developer and every environment running your project uses the exact same versions. This consistency is vital for avoiding the dreaded "it works on my machine" problem. Why is yarn.lock Important in KMM? When building a KMM project, you might need to int...

How to Load Images in Kotlin Multiplatform (KMM) Using Coil

How to Load Images in Kotlin Multiplatform (KMM) Using Coil How to Load Images in Kotlin Multiplatform (KMM) Using Coil Feature Description Library Used Coil (Image Loading Library) Platforms Supported Android, iOS, Web, Desktop (via KMM) Key Benefits Fast, lightweight, easy integration Implementation Kotlin Multiplatform with Jetpack Compose & SwiftUI Use Cases Displaying remote/local images, caching, placeholders Introduction to KMM and Coil Kotlin Multiplatform (KMM) allows developers to build cross-platform apps for Android, iOS, Web, and Desktop with a single codebase. One challenge in KMM is image loading since different platforms handle images differently. The Coil image loading library , originally built for Android, now supports Kotlin Multiplatform and integrates smoothly with Jetpack Compose, offering a robust solution. Why Use Coil ...