【问题标题】:KotlinJsonAdapterFactory unresolved referenceKotlinJsonAdapterFactory 未解析的引用
【发布时间】:2021-12-30 07:35:16
【问题描述】:

我正在 Kotlin 中构建一个 Android 应用,我正在使用 Retrofit 进行 API 调用,并且我也在使用 Moshi。但是在构建 Moshi 时,它说 KotlinJsonAdapteryFactory - 未解析的参考,我不能使用 Moshi,因为它在这一行给我一个错误。知道为什么会这样吗? 这些是我的 Moshi 依赖项:

    implementation("com.squareup.moshi:moshi:1.12.0")
    kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0"
    implementation "com.squareup.retrofit2:converter-moshi:2.5.0" 

这是构建 Moshi 和 Retrofit 的代码

 private val moshi: Moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
 val retrofit: Retrofit = Retrofit.Builder()
        .client(getLoggingHttpClient())
        .baseUrl(BASE_URL)
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .build()

【问题讨论】:

    标签: android kotlin moshi


    【解决方案1】:

    来自 Moshi 的 github documentation

    反射适配器需要以下附加依赖

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

    您错过了上面发布的依赖项,只需将其添加到您的 gradle 文件并重新同步您的项目。

    【讨论】:

    • 好的,成功了!
    • 当然,我刚刚做过,但我只是 stackoverflow 的新手
    猜你喜欢
    • 2019-11-23
    • 2021-06-17
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2018-05-31
    • 2023-01-16
    • 2019-08-19
    相关资源
    最近更新 更多