Moshi:找不到为 class 生成的 JsonAdapter class

Moshi: Failed to find the generated JsonAdapter class for class

我正在尽可能地遵循 https://proandroiddev.com/getting-started-using-moshi-for-json-parsing-with-kotlin-5a460bf3935a 中的示例,但仍然无法 运行。

在我的Gradle中,我有

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

dependencies {
    implementation "com.squareup.moshi:moshi:1.8.0"
    kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
}

我有一个class

@JsonClass(generateAdapter = true)
data class Movie (
    @Json(name = "vote_count") val voteCount: Int = -1,
    val id: Int,
    val title: String,
    @Json(name = "image_path") val imagePath: String,
    val overview: String
)

当我 运行 我的测试如下

    @Test
    fun testMoshi() {
        val moviesJson: String = """
{
  "vote_count": 2026,
  "id": 19404,
  "title": "Example Movie",
  "image_path": "/example-movie-image.jpg",
  "overview": "Overview of example movie"
} 
        """.trimIndent()

        val moshi: Moshi = Moshi.Builder().build()
        val adapter: JsonAdapter<Movie> = moshi.adapter(Movie::class.java)
        val movie = adapter.fromJson(moviesJson)
    }

失败Failed to find the generated JsonAdapter class for class...

我错过了什么?

显然,这是因为我将以下内容放在测试文件夹而不是主文件夹中。

@JsonClass(generateAdapter = true)
data class Movie (
    @Json(name = "vote_count") val voteCount: Int = -1,
    val id: Int,
    val title: String,
    @Json(name = "image_path") val imagePath: String,
    val overview: String
)

当我将它移动到主文件夹后,它就可以工作了(因为现在可以生成适配器)