Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 委派的属性反射不';发布版本不工作_Kotlin_Release_Kotlin Reflect_Delegated Properties - Fatal编程技术网

Kotlin 委派的属性反射不';发布版本不工作

Kotlin 委派的属性反射不';发布版本不工作,kotlin,release,kotlin-reflect,delegated-properties,Kotlin,Release,Kotlin Reflect,Delegated Properties,在运行时,我需要访问委托属性的委托实例中的属性 当我在调试中编译以下代码时,它工作得很好: class Potato { val somePropGoesHere: Int by PotatoDeletgate("This is the key", 0) fun getKey(property: KProperty1<Potato, *>): String { property.isAccessible = true val dele

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

当我在调试中编译以下代码时,它工作得很好:

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)
    }
}
类马铃薯{
val SomePropGoesher:Int by PotatoDeletgate(“这是关键”,0)
fun getKey(属性:KProperty1):字符串{
property.isAccessible=true
val delegate=property.getDelegate(此)
返回时(委托){
是PotatoDeletgate->delegate.key吗
else->throw IllegalStateException(“无法观察属性-${property.name}”)
}
}
PotatoDeletgate类(val键:字符串,
默认值:T){
私有变量innerValue=defaultValue
运算符fun getValue(thisRef:Any?,属性:KProperty):T{
//更多逻辑
返回内部值
}
运算符fun setValue(thisRef:Any?,属性:KProperty,值:T){
//更多逻辑
innerValue=值
}
}
}
类马铃薯粉碎机{
fundostuff(){
val马铃薯=马铃薯()
val key=potato.getKey(potato::somepropgoesher)
}
}
当我在debug中调用“doStuff”方法时,“key”val将获得“This is the key”字符串

但是,当我在发行版中编译这段代码时,会出现以下错误:

2019-11-07 16:16:04.141 7496-7496/?E/AndroidRuntime:致命异常:主 进程:com.trax.retailexecution,PID:7496

e、 a.a.a.j0:未在com.trax.retailexecution.util.Potato类中解析属性“SomePropGoeSher”(JVM签名:GetSomePropGoeSher()I)

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

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

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

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

我不知道怎么做

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

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

在e.a.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)上

在MyNamespaceGoesher.potatoshreder.doStuff(马铃薯kt:2)


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

它确实与proguard相连-在模糊代码中找不到SomePropGoesher

除了不使用反射之外,没有任何解决方法:)(我建议这样做)

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


干杯

谢谢你的回复!我尝试了你的两个建议(将命令和类本身添加到proguard中),但都没有成功。你能发布proguard命令来做这件事吗?