Fatal exception on handling the Flickr Response - Moshi Converter issue

I was getting the following error when I switched to Moshi converter and hoping that it would be able to handle FlickrResponse through fetchPhotos method.

Process: com.bignerdranch.android.photogallery, PID: 30598
java.lang.IllegalArgumentException: Unable to create converter for class com.bignerdranch.android.photogallery.api.FlickrResponse
for method FlickrApi.fetchPhotos

Looks Seems like you need to create a Kotlin adapter first and pass it on the MoshiConverterFactory’s create method as a parameter:

init {
    // Kotlin reflection adapter
    val moshi = Moshi.Builder()

    val retrofit: Retrofit = Retrofit.Builder()
    flickrApi = retrofit.create(FlickrApi::class.java)

Hope it helps others who have ran into this!

Did you comment out those lines “@JsonClass(generateAdapter = true)”? This is what is giving me the issue.

And which package is KotlinJsonAdapterFactory from? I’ve got a unresolved reference here.

Okay, I solved the issue. And here’s more detailed:

  1. I comment out all @JsonClass(generateAdapter = true) line
  2. Added another implementation for build.gradle:
  1. initialize the moshi, retrofit and flickerAPI outside of init and it works.

Here’s my PhotoRepository.kt:

package com.bignerdranch.android.photogallery

import com.bignerdranch.android.photogallery.api.FlickrApi
import com.bignerdranch.android.photogallery.api.GalleryItem
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.squareup.moshi.Moshi
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.create

private const val BASE_URL = "https://api.flickr.com/"

class PhotoRepository {
    private val moshi = Moshi.Builder()

    private val retrofit = Retrofit.Builder()

    private val flickrApi: FlickrApi = retrofit.create()

    suspend fun fetchPhotos(): List<GalleryItem> =