Proguard使用Kotlin-reflect为所有INSTANCE字段生成注释

时间:2018-07-10 17:41:16

标签: android kotlin singleton proguard kotlin-reflect

我已经将kotlin-reflect库添加到了我的Android项目中,现在Proguard生成了数百行笔记,列出了所有具有名为OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: Connecting to github.com [192.30.253.112] port 22. debug1: Connection established. debug1: identity file /home/vagrant/.ssh/id_rsa type 1 debug1: identity file /home/vagrant/.ssh/id_rsa-cert type -1 debug1: identity file /home/vagrant/.ssh/id_dsa type -1 debug1: identity file /home/vagrant/.ssh/id_dsa-cert type -1 debug1: identity file /home/vagrant/.ssh/id_ecdsa type -1 debug1: identity file /home/vagrant/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/vagrant/.ssh/id_ed25519 type -1 debug1: identity file /home/vagrant/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.10 debug1: Remote protocol version 2.0, remote software version libssh_0.7.0 debug1: no match: libssh_0.7.0 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-ctr hmac-sha1 none debug1: kex: client->server aes128-ctr hmac-sha1 none debug1: sending SSH2_MSG_KEX_ECDH_INIT debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48 debug1: Host 'github.com' is known and matches the RSA host key. debug1: Found key in /home/vagrant/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /home/vagrant/.ssh/id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 279 debug1: key_parse_private2: missing begin marker debug1: read PEM private key done: type RSA debug1: Authentication succeeded (publickey). Authenticated to github.com ([192.30.253.112]:22). debug1: channel 0: new [client-session] debug1: Entering interactive session. debug1: Sending environment. debug1: Sending env LANG = en_US.UTF-8 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 Hi {username}! You've successfully authenticated, but GitHub does not provide shell access. debug1: channel 0: free: client-session, nchannels 1 Transferred: sent 3312, received 1792 bytes, in 0.1 seconds Bytes per second: sent 25624.1, received 13864.3 debug1: Exit status 1 的字段的类,其中当然包括每一个Kotlin { {1}}。

这只是Proguard输出的一小部分:

INSTANCE

如您所见,它既包含我自己的object类,又包含Kotlin标准库中的许多类,并且还可以在任何其他库中找到这些字段(如果存在)。

1 个答案:

答案 0 :(得分:1)

错误的第一行告诉我们这里出了什么问题,它发现无论Note: kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance$2 accesses a declared field 'INSTANCE' dynamically Maybe this is program field 'co.zsmb.example.proguardkttest.MySingleton { co.zsmb.example.proguardkttest.MySingleton INSTANCE; }' Maybe this is program field 'kotlin.UNINITIALIZED_VALUE { kotlin.UNINITIALIZED_VALUE INSTANCE; }' Maybe this is program field 'kotlin.Unit { kotlin.Unit INSTANCE; }' Maybe this is program field 'kotlin._Assertions { kotlin._Assertions INSTANCE; }' Maybe this is program field 'kotlin.collections.EmptyIterator { kotlin.collections.EmptyIterator INSTANCE; }' Maybe this is program field 'kotlin.collections.EmptyList { kotlin.collections.EmptyList INSTANCE; }' Maybe this is program field 'kotlin.collections.EmptyMap { kotlin.collections.EmptyMap INSTANCE; }' Maybe this is program field 'kotlin.collections.EmptySet { kotlin.collections.EmptySet INSTANCE; }' Maybe this is program field 'kotlin.comparisons.NaturalOrderComparator { kotlin.comparisons.NaturalOrderComparator INSTANCE; }' 是什么,它都通过反射访问名为MySingleton的字段。 Proguard不能告诉您该代码访问哪个kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance$2字段,所以它会向我们警告每个带有该字段的类,以防万一。

如果我们查看source of KClassImpl,就会很快找到culprit:它包含一个名为INSTANCE的属性,该属性在某些情况下会调用Class#getDeclaredField方法,传入一个以值INSTANCE作为参数的常量。这就是Proguard正在检测的内容。

因此,除非您具有单例类,并且您正在通过其objectInstance访问其实例,如下所示:

"INSTANCE"

您可以忽略将以下规则添加到KClass文件中的Proguard注释:

val instance = MySingleton::class.objectInstance

您也可以精确地以proguard-rules.pro结尾,但是库中的实现更改将来可能会更改该位置编号。这样,您就可以忽略关于-dontnote kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance** 属性及其实现的所有注释。