SerializedName annotation when using R8 code obfuscator

SerializedName annotation when using R8 code obfuscator

Tags
Android
Published
August 9, 2020
Author
Mateusz Teteruk
If you are still using Gson library in your Android project, you should be familiar with SerializedName annotation. When we fetch some data from server, we have to define API model. So in modern Android development, we use data class in Kotlin representing response where we annotate fields with @SerializedName. In general, you shouldn’t have to add proguard rules regarding keeping those models.
However, since Android Gradle Plugin 3.4 there is different default code obfuscator – R8. As a result, we need to define some proguard rules because those models will be obfuscated and app won’t work correctly in environment using obfuscated code. Answer is simple: you either add rules for keeping data models or add rules for not obfuscating @SerializedName.