【问题标题】:kotlin reflect crash , setting minifyEnabled to true . KotlinReflectionInternalError: Property 'xxx' not resolved in class 'xxx'kotlin reflect crash ,设置 minifyEnabled 为 true 。 KotlinReflectionInternalError:属性“xxx”未在“xxx”类中解析
【发布时间】:2020-04-29 02:23:03
【问题描述】:
  • 错误日志

    进程:com.sjianjun.test.kotlin.delegate,PID:23730 kotlin.reflect.jvm.internal.KotlinReflectionInternalError:属性 'delegateTest'(JVM 签名:getDelegateTest()Ljava/lang/Object;)未在 com.sjianjun.test.kotlin.delegate.MainActivity 类中解析 在 kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findPropertyDescriptor(:115)

  • 主活动:

@Keep
class MainActivity : AppCompatActivity() {
    var delegateTest by DelegateTest()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this::class.java.declaredMethods.forEach {
            Log.e("DelegateTest", it.toString())
        }

        Log.e("DelegateTest ", delegateTest.toString()+DelegateTest::class.java.name)
        delegateTest = 1
        Log.e("DelegateTest ", delegateTest.toString())
    }
}

  • 委托测试:
@Keep
class DelegateTest {
    var value: Any? = null
    operator fun getValue(thisRef: Any?, property: KProperty<*>): Any {
// crash : property
        return "value:$value $property"
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Any) {
        this.value = value
    }

}
  • proguard:
-keep class com.sjianjun.test.kotlin.delegate.DelegateTest{*;}
-keep class com.sjianjun.test.kotlin.delegate.MainActivity{*;}
-keepclassmembers class com.sjianjun.test.kotlin.delegate.MainActivity{*;}
-keep class kotlin.reflect.**{*;}
  • 并记录:
 E/kotlin.delegat: Unable to peek into adb socket due to error. Closing socket.: Connection reset by peer
 E/DelegateTest: public void com.sjianjun.test.kotlin.delegate.MainActivity._$_clearFindViewByIdCache()
 E/DelegateTest: public android.view.View com.sjianjun.test.kotlin.delegate.MainActivity._$_findCachedViewById(int)
 E/DelegateTest: public final java.lang.Object com.sjianjun.test.kotlin.delegate.MainActivity.getDelegateTest()
 E/DelegateTest: protected void com.sjianjun.test.kotlin.delegate.MainActivity.onCreate(android.os.Bundle)
 E/DelegateTest: public final void com.sjianjun.test.kotlin.delegate.MainActivity.setDelegateTest(java.lang.Object)
 E/DelegateTest: value:null var com.sjianjun.test.kotlin.delegate.MainActivity.delegateTest: kotlin.Anycom.sjianjun.test.kotlin.delegate.DelegateTest
 E/DelegateTest: value:1 var com.sjianjun.test.kotlin.delegate.MainActivity.delegateTest: kotlin.Any

我发现proguard会删除以下字段:

 E/DelegateTest: value:null var com.sjianjun.test.kotlin.delegate.MainActivity.delegateTest: kotlin.Anycom.sjianjun.test.kotlin.delegate.DelegateTest
 E/DelegateTest: value:1 var com.sjianjun.test.kotlin.delegate.MainActivity.delegateTest: kotlin.Any

我不知道该怎么办......

【问题讨论】:

标签: android kotlin kotlin-reflect


【解决方案1】:

Android gradle 插件默认使用 R8 混淆器。从 Proguard 切换到 R8 后,我遇到了同样的问题。

切换回 Proguard 帮助了我。您可以在此处查看如何在项目中手动启用 proguard 而不是 R8:
https://www.guardsquare.com/manual/setup/gradleplugin

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    相关资源
    最近更新 更多