Kotlin:ProGuard会擦除属性属性 [英] Kotlin: ProGuard erases properties attributes

查看:211
本文介绍了Kotlin:ProGuard会擦除属性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR :启用了proguard的情况下,尽管使用proguard配置应保留所有这些属性,但在使用反射时,我的属性看起来是私有的,不可为空且没有注释.

TL;DR: with proguard enabled, when using reflection, my properties look private, non-nullable and without annotations, despite proguard config that should keep all these attributes.

我有一些带有公共属性的简单data class es,它们可以用作我的Android应用程序中的数据模型.稍后,当对所述类进行泛型[反]序列化时,我会像这样过滤属性列表:

I have some simple data classes with public properties to serve as data models in my Android app. Later, when doing generic [de]serialization of said 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<*>>()

它在我的调试版本中正常工作(我的意思是它选择了我想要的属性).但是,当执行Proguard处于活动状态的 release 版本时,结果为空.为了检查原因,我记录了有关一个类的属性的所有相关信息-证明它们的visibility字段读取PRIVATE(所有其他属性与调试版本相同).

It works normally on my debug builds (I mean it selects the properties I want it to). But, when doing a release build, where proguard is active, the result is empty. To check why, I logged all the relevant stuff about the properties of one class -- turns out their visibility field reads PRIVATE (and all other attributes remain the same as on a debug build).

我在proguard config中已经有一行可以保留所有模型:

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

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

我之前尝试过此方法,结果相同:

I tried this one before, with same result:

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

Proguard为什么/如何影响财产可视性?我应该以某种方式修改配置吗?还是我在这里想念其他东西?

Why/how does proguard affect property visibility? Should I modify the config somehow? Or am I missing something else here?

更新:似乎并不是唯一的可见性. prop.returnType.isMarkedNullable也不起作用,对于声明为可空的属性,它返回false.即使我要求proguard保留注释,注释也似乎会丢失.有什么办法可以解决此问题?这几乎使我的工作两周没用了...

UPDATE: It seems like visibility is not the only thing. prop.returnType.isMarkedNullable also doesn't work, it returns false for properties declared nullable. And annotations also seem to get lost, even though I asked proguard to keep them. Is there any way to work around this? It pretty much renders 2 weeks of my work useless...

推荐答案

感谢@yole提出的有问题的评论,我已经能够完成这项工作.即使将我的类配置为由ProGuard保留,它也从它们中删除了kotlin.Metadata批注.这些注释是Kotlin存储我所缺少的所有属性的地方.解决方案是防止ProGuard删除它们,将其添加到配置中:

Thanks to the suggestion from @yole in question comments, I've been able to make this work. Even though my classes were configured to be kept by ProGuard, it stripped the kotlin.Metadata annotations from them. These annotatons are where Kotlin stores all the attributes I was missing. The solution is to prevent ProGuard from deleting them, adding to configuration:

-keep class kotlin.Metadata { *; }

(在旁注:奇怪的是,它不包含在默认配置中,至少在使用kotlin.reflect.full软件包的情况下.或者至少应该在文档中的某处明确提及...)

(on a side note: it's weird that it's not included in the default config, at least if you're using the kotlin.reflect.full package. Or at least it should be mentioned clearly somewhere in the docs...)

这篇关于Kotlin:ProGuard会擦除属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆