Hello Everyone, Is it possible to use Coil's `Asyn...
# multiplatform
r
Hello Everyone, Is it possible to use Coil's
AsyncImage
composable in the
commonMain
package of a Kotlin Multiplatform project to load an image from a URL? I tried using it, but
AsyncImage
is not accessible in
commonMain
composable functions. Do we need to use `expect`/`actual` declarations to support image loading on both Android and iOS platforms?
m
Coil should support CPM. Did you add the dependency to the
commonMain
source set?
r
yes I have added coil-compose dependency in commonMain source set.
Copy code
coil-compose = { group = "io.coil-kt.coil3", name = "coil-compose", version.ref = "coil"}
Copy code
commonMain.dependencies {
    implementation(compose.runtime)
    implementation(compose.foundation)
    implementation(compose.material)
    implementation(compose.ui)
    implementation(compose.components.resources)
    implementation(compose.components.uiToolingPreview)
    implementation(libs.androidx.lifecycle.viewmodel)
    implementation(libs.kotlinx.coroutines)
      implementation(libs.androidx.lifecycle.runtime.compose)
    implementation(libs.coil.compose)
}
m
Then the next question would be does it not compile when built with gradle or could the IDE just be incorrectly showing an error. Might need to manually add the import. I've not used it, but looking at GitHub both Android and iOS are included in their list of targets.
r
It is not importing while calling AsyncImage composable see in below screenshot.
Do you have github repo which uses AsyncImage in commonMain?
m
Try manual import sometimes the IDE just gets broken. Useful to understand the problem.
r
Ok, I will check once because I can access in androidMain without manual import but in commonMain even after adding manual import still getting error in import statement only.
m
I would guess that the IDE is confused, it happens sometimes
r
yes will rebuild project and invalidate the cache once.
thanks