DelegatedProperty反射不适用于版本构建

在运行时,我需要访问委托属性的委托实例中的属性。

当我在调试中编译以下代码时,它可以正常工作:

class Potato {
    val somePropGoesHere: Int by PotatoDeletgate("This is the key",0)

    fun getKey(property: KProperty1<Potato,*>): String {
        property.isaccessible = true
        val delegate = property.getDelegate(this)

        return when (delegate) {
            is PotatoDeletgate<*> -> delegate.key
            else -> throw IllegalStateException("Can't observe the property - ${property.name}")
        }
    }

    class PotatoDeletgate<T>(val key: String,defaultvalue: T) {
        private var innerValue = defaultvalue

        operator fun getvalue(thisRef: Any?,property: KProperty<*>): T {
            // more logic
            return innerValue
        }

        operator fun setvalue(thisRef: Any?,property: KProperty<*>,value: T) {
            // more logic
            innerValue = value
        }
    }
}

class PotatoShredder {
    fun doStuff() {
        val potato = Potato()
        val key = potato.getKey(Potato::somePropGoesHere)
    }
}

当我在调试中调用“ doStuff”方法时,“ key”值将获得“ This is the key”字符串。

但是,当我在发行版中编译此代码时,出现错误:

2019-11-07 16:16:04.141 7496-7496 /? E / AndroidRuntime:致命异常:main     流程:com.trax.retailexecutioncution,PID:7496

e.a.a.a.j0:类com.trax.retailexecution.util.Potato中未解析的属性'somePropGoesHere'(JVM签名:getsomePropGoesHere()I)

在e.a.a.p.c(KDeclarationContainerImpl.kt:40)

在e.a.a.a.z $ d.invoke(KPropertyImpl.kt:4)

在e.a.a.l0.a(ReflectProperties.java:4)

在e.a.a.z.e(KPropertyImpl.kt:2)

我不确定

在e.a.a.a.z $ e.invoke(KPropertyImpl.kt:1)

在e.a.a.m0.a(ReflectProperties.java:3)

在e.a.a.z.i(KPropertyImpl.kt:1)

在c.m.a.b.a.a(DefaultConfigurationFactory.java:82)

在c.m.a.b.a.a(DefaultConfigurationFactory.java:153)

在com.trax.retailexecution.util.Potato.getKey(Potato.kt:1)

在mynamespacegoeshere.PotatoShredder.doStuff(Potato.kt:2)

我认为这与proguard有关,因为在压缩/收缩过程中我们删除了类和/或方法,但我真的找不到合适的方法来解决问题。

xiaoyandexinsi 回答:DelegatedProperty反射不适用于版本构建

它确实与proguard连接-在混淆的代码中找不到您的somePropGoesHere。

尽管没有使用反射:),但没有任何解决方法,(我建议)

您还可以将您的PotatoDelegate添加到proguard规则中:)

干杯!

本文链接:https://www.f2er.com/3144089.html

大家都在问