Kotlin: ProGuard erases property attributes

TL; DR : With proguard enabled, when using reflection, my properties appear to be private, non-null and no annotations, despite the proguard config that must contain all of these attributes.

I have some simple data class

es with public properties to use as data models in an android app. Later, when you do generic serialization of the specified classes, I filter the property list like this:

val properties = instance::class.memberProperties
        .filter { it.visibility == KVisibility.PUBLIC } // && some other conditions, unrelated here
        .filterIsInstance<KMutableProperty<*>>()

      

It works fine on my debug builds (I mean it picks the properties I want). But when creating a release where proguard is active, the result is empty . To check why, I wrote down all the relevant materials about the properties of one class - it turns out that their field visibility

reads PRIVATE

(and all other attributes remain the same as in the debug build).

I already have a line in proguard config to save all models:

-keepclassmembers class * extends com.models.package.name.BaseModel { *; }

      

I've tried this before, with the same result:

-keep class com.models.package.name.** { *; }

      

Why / how does proguard affect property visibility? Do I have to change the configuration in any way? Or am I missing something else here?


UPDATE . Seems like visibility isn't the only thing. prop.returnType.isMarkedNullable

also doesn't work, it returns false

for properties declared as nullable. And the annotations also seem to be lost, although I asked the lawyers to keep them. Is there a way to get around this? This pretty much makes my 2 weeks of work useless ...

+3


source to share


1 answer


Thanks to a suggestion from @yole's comments, I was able to get the job done. Even though my classes were configured to support ProGuard, it stripped annotations kotlin.Metadata

away from them. These annotators are where Kotlin stores all the attributes I was missing. The solution is to prevent ProGuard from uninstalling by adding to the configuration:

-keep class kotlin.Metadata { *; }

      



(on the side of the note: strange that it's not included in the default config, at least if you're using a package kotlin.reflect.full

. Or at least it should be clearly stated somewhere in the docs ...)

+3


source







All Articles