3

I have added both dependencies for Moshi and converter-Moshi but yet MoshiConverterFactory is not accessible and prompting error. Why am I facing this error? I tried to use scalar-converter which worked perfectly but moshi-converter is not.

I have attached the photo of the code where red text clearly shows that converter is not imported.


    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    // Retrofit with Moshi Converter
    implementation 'com.squareup.moshi:moshi-kotlin:1.9.3'
    implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
import retrofit2.converter.moshi.MoshiConverterFactory


private val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    //here MoshiConverterFactory showing errors
    .addConverterFactory(MoshiConverterFactory.create())
    .baseUrl(BASE_URL)
    .build()

enter image description here

Shoaib Khalid
  • 55
  • 1
  • 6

2 Answers2

5

You forgot to add the main Moshi dependency. You have added the Kotlin specific dependency, but the main Moshi still is missing. Try adding that and try again.

implementation 'com.squareup.moshi:moshi:1.12.0'
che10
  • 2,176
  • 2
  • 4
  • 11
  • This is not working for me still. //network implementation "com.squareup.retrofit2:retrofit:$retrofit" implementation 'com.squareup.moshi:moshi-kotlin:1.13.0' implementation "com.squareup.retrofit2:converter-moshi:$moshi" implementation "com.squareup.moshi:moshi:$moshiConvertor" – user3701188 Jul 15 '22 at 21:16
3

You Must make sure that you have added all dependencies

//moshi
implementation "com.squareup.moshi:moshi-kotlin:1.14.0"
implementation 'com.squareup.moshi:moshi:1.14.0'
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.14.0"

//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")`
Mark S. Khalil
  • 113
  • 1
  • 4