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)
- adapter(Class<TypeVariable(T)!>!)
- adapter(Type!)
- adapter(Type!, (MutableSet<out Annotation!>..Set<Annotation!>?))
- adapter(Type!, (MutableSet<out Annotation!>..Set<Annotation!>?), String?)
- adapter(Type!, Class<out Annotation!>!)
- adapter(type: Type!, annotationType: Class<out Annotation!>!)
- adapter(Type!, vararg Class<out Annotation!>!)
- adapter(type: Type!, vararg annotationTypes: Class<out Annotation!>!)
— basically, no matching overload found.
Things I've already tried:
- passing
kotlin.reflect.typeOf<Blob>().javaClass - a bunch of nutty rearrangings of the syntax
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!
Wow... thank you @BeNYaMiN!
Finally works