LyrePyre
LyrePyre

Reputation: 481

Using Moshi in Kotlin (Android), Moshi.adapter() signatures seem to differ from the docs

My build.gradle dependencies (for a non-main Android Studio module):

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
    ksp 'com.squareup.moshi:moshi-kotlin-codegen:1.14.0'

    // (...)
}

This code doesn't work despite seeming to be functionally identical to the examples in the docs:

class Blob (val string: String);

internal object JsonMan {
    @NonNull
    val moshi = Moshi.Builder()
                     .addLast(KotlinJsonAdapterFactory())
                     .build();

    val mapAdapter = moshi.adapter<Blob>();
                   //      ^^^^^^^ No matching overload found.
}

The specific Android Studio error message on hover is:

None of the following functions can be called with the arguments supplied.

(removed some details for brevity)

— basically, no matching overload found.

Things I've already tried:

I was really hoping that simply following the docs would Just Work ™ – I suppose that one's on me.

I'm brand new to Kotlin, so I think I'm probably just ignorant of something that would be obvious to most Moshi-Kotlin users... Any help would be appreciated :)

Edit:

The comment by @BeNYaMiN did the trick.

I am a bit salty though that the Kotlin syntax used throughout Moshi's docs aren't an actual signature you can use out of the box... I am still suspicious that there has to be some way to get the "official" syntax to compile. Or is it all because this is Android Kotlin? If anyone knows, I would still appreciate your knowledge!

Upvotes: 1

Views: 2078

Answers (1)

LyrePyre
LyrePyre

Reputation: 481

Wow... thank you @BeNYaMiN!

val mapAdapter = moshi.adapter(Blob::class.java)

Finally works 😁

Upvotes: 2

Related Questions