Moshi 1.9.x Cannot serialize Kotlin type

The error message specifically says Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact

As per the Kotlin part of the readme, you must add the KotlinJsonAdapterFactory if you're not using Moshi's codegen. This was a specific behavior change in Moshi 1.9 as per the blog post about Moshi 1.9.

Moshi.Builder()
    .add(AbsPlayerConverter)
    .add(AbsRegionConverter)
    .add(AbsTournamentConverter)
    .add(MatchConverter)
    .add(RankedPlayerConverter)
    .add(SimpleDateConverter)
    .add(KotlinJsonAdapterFactory())
    .build()

And make sure you're using implementation("com.squareup.moshi:moshi-kotlin:1.9.1")


I'm using retrofit and I had to do the following:

In the build.grade:

implementation "com.squareup.moshi:moshi-kotlin:$moshiVersion"

In my repository:

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

val retrofit = Retrofit.Builder()
    .baseUrl(WEB_SERVICE_URL)
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .build()

I got this when I forgot to add @JsonClass(generateAdapter = true) to one of my classes.