android - 使用 Moshi 的 Kotlin codegen 有困难

标签 android kotlin retrofit2 moshi

我正在尝试使用 Moshi 的 Kotlin codegen 在 Kotlin 中获得注释支持。尽管仔细按照 moshi codegen documentation 中的说明进行操作,@JsonClass(generateAdapter = true) 中的注释 JsonClass 未被识别,我收到以下错误:

错误:类型不兼容:NonExistentClass 无法转换为 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'
}

@JsonClass注解在我添加的时候被识别

实现(“com.squareup.moshi:moshi-kotlin:1.8.0”)

然而,moshi reflection documentation表明只有在使用反射而不是代码生成时才需要这种依赖。

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

最佳答案

@JsonClass 是标准 Moshi Artifact 中的一种类型。 Retrofit 的 converter-moshi Artifact 可传递地引入 Moshi,但没有最新版本的 Moshi。 指定实现("com.squareup.moshi:moshi:1.8.0")

关于android - 使用 Moshi 的 Kotlin codegen 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991041/

相关文章:

android - 使用 PageRow 的 leanback BrowseFragment 的自定义 header

Android:当设置为背景时,如何使用 layer-list 和 shape 元素绘制水平线?

java - 为什么我在使用 Retrofit 和 GSON 转换器映射的对象属性上得到空值?

android - 将支持库更新到 27.0.0 后,我的 fragment 中出现多个错误

android - Canvas 的 drawLine 和 drawRect 不包括结束位置?

java - 如何在Recycler View适配器中访问exoplayer方法

android - Android searchView明文按钮监听器

spring-boot - Kotlin 使用存储库进行测试(spring-boot、kotlin、jersey、jax-rs)

android - 使用 Moshi/Retrofit2 访问深度嵌套的 JSON 数组

android - 复用Retrofit方法