Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Reflection with Proguard fails

Let's say I have this class

data class Person(val name: String?)

When I proguard and run the app I am getting the following exception

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: No accessors or field is found for property val com.whatever.packagee.Person.name: kotlin.String?

I also found that the issue is thrown from this reflection codebase. Any help would be appreciated

like image 701
ImMathan Avatar asked Jan 31 '19 12:01

ImMathan


2 Answers

Please make sure to not use too broad -keep options, this will prevent ProGuard from properly optimising and shrinking the code.

It's important to preserve the fields of classes you wish to serialise. Based on the exception you see I would expect a -keep option similar to the one below would do the trick;

-keep class com.whatever.packagee.Person {
    <fields>;
}

You can experiment with these -keep options via the ProGuard Playground, which is quite useful to see what parts of code are affected with your -keep rules. This way you can inspect if all cases of serialization were tackled. I created a Playground for this particular case: Playground link

like image 139
TrueStory Avatar answered Nov 13 '22 03:11

TrueStory


This solved the issue for me.

Add following to proguard rules

-keep class kotlin.reflect.jvm.internal.**

-keep class kotlin.Metadata { *; }

like image 1
user1331404 Avatar answered Nov 13 '22 03:11

user1331404