使用 Moshi 的 Kotlin 代码生成器的困难

rst*_*dge 2 android kotlin moshi retrofit2

我正在尝试使用 Moshi 的 Kotlin 代码生成器来获得 Kotlin 中的注释支持。尽管仔细遵循了moshi codegen 文档中的说明,但无法识别中的注释JsonClass@JsonClass(generateAdapter = true)并且出现以下错误:

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

我的应用程序build.gradle文件如下:

...
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'
}
Run Code Online (Sandbox Code Playgroud)

@JsonClass当我添加注释认可

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

但是,moshi反射文档表明只有在使用反射而不是时才需要此依赖项。

知道我错过了什么吗?谢谢!

Eri*_*ran 10

@JsonClass是标准魔石神器中的一种。Retrofit 的converter-moshi神器可传递地引入 Moshi,但没有最新版本的 Moshi。指定implementation("com.squareup.moshi:moshi:1.8.0")