2
  • 错误日志

    进程: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
    }

}
  • 前卫:
-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

我不知道该怎么办 ...

4

1 回答 1

0

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

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

于 2022-01-26T14:45:59.410 回答