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

How to Integrate Razorpay in Kotlin Multiplatform Mobile (KMM) for WebApp JS Browser Target

Integrate Razorpay in KMM for WebApp JS Browser Target How to Integrate Razorpay in Kotlin Multiplatform Mobile (KMM) for WebApp JS Browser Target If you're working with Kotlin Multiplatform Mobile (KMM) and want to integrate Razorpay for payments, you might find plenty of documentation for Android and iOS. However, integrating Razorpay into the WebApp JS Browser target isn't as straightforward. In this blog post, I'll guide you through the steps to get Razorpay working in your KMM project for the WebApp JS Browser target. Why Kotlin Multiplatform Mobile (KMM)? KMM allows you to share business logic across Android, iOS, Web, and other platforms, making it easier to maintain a single codebase. But, when it comes to platform-specific features like payments, you need to implement certain functionality separately for each platform. Integrating Razorpay in the JS Browser Target To integrate Razo...

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...

Part 2: Getting Started with Kotlin Multiplatform Mobile (KMM)

Getting Started with Kotlin Multiplatform Mobile (KMM) Getting Started with Kotlin Multiplatform Mobile (KMM) If you're new to Kotlin Multiplatform Mobile, here’s a quick guide to help you get started: Setup Your Development Environment Install the latest version of Android Studio . Install the Kotlin plugin . Set up Xcode on your Mac for iOS development . Create a New KMM Project Use Android Studio's KMM project template to create a new project. Configure the project for both Android and iOS targets. Write Shared Code Start by writing common code that can be shared between platforms, such as data models, network logic, and business rules. Here’s a simple example: // Shared code module expect class Platform() { val name: String ...