1

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!

Martin Gunnarsson
  • 159
  • 1
  • 1
  • 11
  • It is part of the Gson library, look at the import. You can try to sync you project again because sometimes Android Studio doesn't see all the dependencies, even if it compiles fine. – Iulian Popescu Jul 02 '21 at 07:26
  • Have you tried _Invalidate Caches/Restart_ to Android Studio? – Piyush Jul 02 '21 at 07:27
  • I tried that, and all the red markings disappeared. However, as soon as I edited one of the files SerializedName turned red again :-( – Martin Gunnarsson Jul 02 '21 at 08:38
  • IntelliJ IDEA **does have** constant issues updating dependencies (even if I manually trigger dependency updates, sometimes it fails even with cache invalidation -- the only IDE saying "clear the cache whatever you've done -- it might help", it's funny). I use Apache Maven even for Android projects with `mvn dependency:tree` to make sure the dependency tree is what I expect. Doing the same with Gradle: https://stackoverflow.com/questions/21645071/using-gradle-to-find-dependency-tree . If Gradle shows up the Gson dependency, then it is the same IntelliJ IDEA dependency update issue again. – terrorrussia-keeps-killing Jul 02 '21 at 08:38
  • (Note: by "intellij idea" I mean every IDEA-based IDE, including Android Studio, as I'm almost sure they all share the same codebase therefore have the same issues). – terrorrussia-keeps-killing Jul 02 '21 at 08:43
  • I checked the dependency tree, and Gson is indeed in there. It's not surprising since the app both builds and puts the right data in the property, but it's still annoying to get the error markings in the IDE. I guess I might have to live with it. Thanks for the input! – Martin Gunnarsson Jul 02 '21 at 19:01
  • implementation 'com.google.code.gson:gson:2.9.0' add in gradle. i resolve my issue – tej shah Jul 12 '22 at 11:02
  • Same problem here. Did you solve this? @MartinGunnarsson – Ahmet Yılmaz Jun 21 '23 at 15:25
  • I know it's not a very satisfying answer, but eventually the problem resolved itself. – Martin Gunnarsson Jun 22 '23 at 09:31

1 Answers1

0

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.

Martin Gunnarsson
  • 159
  • 1
  • 1
  • 11