DEV Community

Cover image for Gson and R8 force final object to be null at runtime
Abdalla Elnaggar
Abdalla Elnaggar

Posted on

Gson and R8 force final object to be null at runtime

I had this weird issues that a field is null only on release build, even if I checked it's nullity before access it,
it turns out that R8 when it minify code and you use Gson Library the reflection can alter final fields to be null, WHAT, ya that is the case,final values are able to be modified during reflection - which is how Gson updates final values, that is the statement from one of google employees comment on the issue,

ok how we end up fixing this issue is by adding proguard rule

#Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * { <fields>;
Enter fullscreen mode Exit fullscreen mode

you can check this issue on issue tracker
I had so many hours trying to check null, build release and try ,so here is the issue and here how to fix it.
I hope it helps any who will face this issue.
I read a couple of posts that advocate against Gson and advertise Moshi, I am starting to believe.

Top comments (1)

abdelkawi profile image

Great job bro