mardi 11 février 2020

ProGuard for Kotlin: JVM signature cannot be resolved

When activating ProGuard, I get the following error message that seems to stem from Kotlin Reflect.

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Property 'selectedAccount' (JVM signature: getSelectedAccount()Lmy/app/package/data/model/Account;) not resolved in class my.app.package.data.SharedPrefsData
    at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findPropertyDescriptor(:115)
    at kotlin.reflect.jvm.internal.KPropertyImpl$_descriptor$1.invoke(:102)
    at kotlin.reflect.jvm.internal.KPropertyImpl$_descriptor$1.invoke(:27)
    at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(:92)
    at kotlin.reflect.jvm.internal.KPropertyImpl.getDescriptor(:105)
    at kotlin.reflect.jvm.internal.KPropertyImpl$_javaField$1.invoke(:52)
    at kotlin.reflect.jvm.internal.KPropertyImpl$_javaField$1.invoke(:27)
    at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(:62)
    at kotlin.reflect.jvm.internal.KPropertyImpl.getJavaField(:79)
    at kotlin.reflect.jvm.ReflectJvmMapping.getJavaField(:40)
    at kotlin.reflect.jvm.KCallablesJvm.a(:65)
    at com.chibatching.kotpref.livedata.KotprefLiveDataExtensionsKt$a.<init>(:16)
    at com.chibatching.kotpref.livedata.KotprefLiveDataExtensionsKt.a(:11)
    at my.app.package.data.SharedPrefsData.getSelectedAccountLiveData(:20)

SharedPrefsData is a simple KotprefModel to store and load from SharedPreferences:

class SharedPrefsData(context: Context) : KotprefModel(context), ISharedPrefsData {

    override val kotprefName: String = "${context.packageName}_preferences_data"

    override var selectedAccount by gsonNullablePref<Account>()

    override fun getSelectedAccountLiveData(): LiveData<Account?> {
        return asLiveData(this::selectedAccount)
    }
}

And this is what happens in KotprefLiveDataExtensionsKt

fun <T> KotprefModel.asLiveData(property: KProperty0<T>): LiveData<T> {
    return object : LiveData<T>(), SharedPreferences.OnSharedPreferenceChangeListener {
        private val key: String

        init {
            value = property.get()
            property.isAccessible = true
            key = (property.getDelegate() as? PreferenceKey)?.key ?: property.name
            property.isAccessible = false
        }

        override fun onSharedPreferenceChanged(prefs: SharedPreferences, propertyName: String) {
            if (propertyName == key) {
                postValue(property.get())
            }
        }

        override fun onActive() {
            this@asLiveData.preferences.registerOnSharedPreferenceChangeListener(this)
        }

        override fun onInactive() {
            this@asLiveData.preferences.unregisterOnSharedPreferenceChangeListener(this)
        }
    }
}

So there seems to be some Kotlin Reflection going on which is famous to cause problems with ProGuard. But I am already keeping the involved model classes as well as the SharedPrefsData classes. The only ProGuard rules I can find on the KotPref Github are already applied here.

Right now I am using the following ProGuard rules:

-keep class kotlin.reflect.jvm.internal.** { *; }
-keep class kotlin.reflect.jvm.ReflectJvmMapping { *; }
-keep class com.chibatching.kotpref.livedata.KotprefLiveDataExtensionsKt
-keep class kotlin.reflect.jvm.KCallablesJvm  
-keep class my.app.package.data.model.Account { *; }
-keep class my.app.package.data.model.User { *; }
-keep class my.app.package.data.model.Team { *; }
-keep class my.app.package.data.SharedPrefsData { *; }
-keep class my.app.package.data.ISharedPrefsData { *; }

But the error is still there. Any idea what to do here??





Aucun commentaire:

Enregistrer un commentaire