Search code examples
androidandroid-studiokotlingsonandroid-volley

Android Studio can't find SerializedName in Kotlin file, but compiles anyway


I'm building my first Android app in a long time, and learning Kotlin in the process. I'm using Volley to get JSON data and Gson to parse it. In some cases I use the SerializedName annotation in my model classes to get nicer property names, for example like this:

@SerializedName("_id")
var id = ""

Android studio is constantly complaining that it can't find the SerializedName annotation, even though I've added the import import com.google.gson.annotations.SerializedName.

Android Studio screenshot

The strange thing is that everything compiles fine reglardless, so it's more of an annoyance than a problem. Still, I would like to get rid of all the red markings that this causes, to make it easier to spot real problems, preferably by making Android Studio understand where the annotation can actually be found.

The project compiles fine with or without the implementation 'com.squareup.retrofit2:converter-gson:2.5.0' dependency that people seem to add to get the SerializedName annotation, so I guess it's either part of the basic Gson package (I'm using implementation 'com.google.code.gson:gson:2.8.6' in my Gradle file) or that it gets included as a secondary dependency.

Any ideas? Thanks!


Solution

  • A bit of a non-answer for others facing the same problem: As multiple comments suggested, this was probably due to Android Studio failing to take my dependencies into account when validating the code. The issue resolved itself after a while. Make sure to verify the dependency tree as suggested in the comments to make sure you actually have the right dependencies in place.