Coil — New image loader in Android town

Nagendra Hari Karthick
2 min readMar 9, 2020

--

For many many years, Image loading in android was ruled by some common libraries like Glide, Picasso, Fresco and few others.
Now We have an new guy in the town.
Why do we need this guy, When we have Glide, Picasso and Fresco to solve our problems.
Ok let me elaborate,

  • Fast: This new guy is extremely fast. He performs number of optimizations including memory and disk caching, down sampling the image in memory, reusing bitmaps and more.
  • Lightweight: This new guy has almost 8x fewer lines of code than Glide and slightly less than Picasso. Also on the other hand this new guy adds ~1500 methods to your APK (for apps that already use OkHttp and Coroutines) which is comparable compared to other libraries.
  • Easy to use: The new guy’s API leverages Kotlin’s language features for simplicity and minimal boilerplate. Deep inside we all know it is how easy to code in Kotlin. The same rules applies for using this library.
  • Modern: This new guy is the first person in town to use modern libraries including coroutines, OkHttp, Okio and AndroidX Lifecycles.
  • He also supports dependency injection, as it provides singleton and non-singleton artifacts.
  • He also supports R8 so we no need to add any extra rules.

The Name is Coil .Coil is an acronym for Coroutine Image Loader

Thats it for introduction, lets dive into the implementation

Artifacts

Coil has 4 artifacts published to mavenCentral():

  • io.coil-kt:coil: The default artifact, which includes the Coil singleton.
  • io.coil-kt:coil-base: The base artifact, which does not include the Coil singleton. Prefer this artifact if you want to use dependency injection to inject your ImageLoader instance(s).
  • io.coil-kt:coil-gif: Includes a set of decoders to support decoding GIFs. See GIFs for more details.
  • io.coil-kt:coil-svg: Includes a decoder to support decoding SVGs. See SVGs for more details.

add artifact as per your requirement. You are good to go.

Implementation

To load an image into an ImageView, use the load extension function:

// URL
imageView.load("https://www.example.com/image.jpg")
// Resource
imageView.load(R.drawable.image)
// File
imageView.load(File("/path/to/image.jpg"))
// And more...

Requests can be configured with an optional trailing lambda:

imageView.load("https://www.example.com/image.jpg") {
crossfade(true)
placeholder(R.drawable.image)
transformations(CircleCropTransformation())
}

To get an image imperatively, use the get suspend function:

val drawable = Coil.get("https://www.example.com/image.jpg")

That’s it. The code is very simple since we are using extension functions, we can directly load the image from the imageview.

Thanks.

--

--