5

I am trying to use Moshi's Kotlin codegen to get annotation support in Kotlin. Despite following the instructions carefully in the moshi codegen documentation, the annotation JsonClass in @JsonClass(generateAdapter = true) is not recognized and I am getting the following error:

error: incompatible types: NonExistentClass cannot be converted to Annotation@error.NonExistentClass()

My app build.gradle file is as follows:

...
apply plugin: 'kotlin-kapt'

android {
    ...
}

dependencies {
    ...
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'

    kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
}

The @JsonClass annotation is recognized when I add

implementation("com.squareup.moshi:moshi-kotlin:1.8.0").

However, the moshi reflection documentation indicates that this dependency is only required if using reflection instead of codegen.

Any idea what I'm missing? Thanks!

rstockbridge
  • 233
  • 3
  • 11

1 Answers1

15

@JsonClass is a type in the standard Moshi artifact. Retrofit's converter-moshi artifact brings in Moshi transitively but does not have the latest version of Moshi. Specify implementation("com.squareup.moshi:moshi:1.8.0").

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91