2

我有以下代表...

fun integerPref(initialValue: Int) = object : ObservableProperty<Int>(initialValue) {

    override fun afterChange(property: KProperty<*>, oldValue: Int, newValue: Int) {
        getSharedPreference(INTEGER_PREF, EasyInjection.mode)
                .edit()
                .putInt(property.toString(), newValue)
                .apply()
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): Int {
        return getSharedPreference(INTEGER_PREF, EasyInjection.mode)
                .getInt(property.toString(), initialValue)
    }
}

该代表旨在处理股份偏好。当我的应用程序变为 MultiDex 时,就会出现问题。当我在活动中使用下面的代码时,应用程序崩溃。

class IntroduceActivity : BaseActivity() {

    private val CURRENT_VERSION = 1
    private val introVersion by integerPref()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_introduce)

        if (introVersion == CURRENT_VERSION) {
            finish()
        }
   }
}

但是,如果我将代码更改为这个。应用程序崩溃

class IntroduceActivity : BaseActivity() {

    private val CURRENT_VERSION = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_introduce)

        var introVersion by integerPref()
        if (introVersion == CURRENT_VERSION) {
            finish()
        }
    }
}

我已经阅读了这个文件,它有一些通知..

https://developer.android.com/studio/build/multidex.html

注意:在 MultiDex.install() 完成之前,不要通过反射或 JNI 执行 MultiDex.install() 或任何其他代码。Multidex 跟踪不会跟随这些调用,导致 ClassNotFoundException 或由于 DEX 文件之间的错误类分区而导致验证错误。

总之,我想知道在这种情况下会发生什么。

  • 应用程序崩溃发生在低于 21 的 Android 上
  • kotlin 委托使用反射
  • 为什么从字段变量更改为局部变量可以解决问题?
  • Logcat 没有帮助

日志猫

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apg.mobile.som.uat/com.apg.mobile.som.activity.MainActivity}: java.lang.TypeNotPresentException: Type android/support/v4/app/ActivityCompatApi23$SharedElementCallbackImpl not present
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.TypeNotPresentException: Type android/support/v4/app/ActivityCompatApi23$SharedElementCallbackImpl not present
    at java.lang.Class.getDeclaredClasses(Native Method)
    at java.lang.Class.getDeclaredClasses(Class.java:534)
    at kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass.getInnerClassNames(ReflectJavaClass.kt:35)
    at kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass.getInnerClassNames(ReflectJavaClass.kt:27)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassMemberScope$nestedClassIndex$1.invoke(LazyJavaClassMemberScope.kt:617)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassMemberScope$nestedClassIndex$1.invoke(LazyJavaClassMemberScope.kt:64)
    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:323)
    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:370)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassMemberScope$nestedClasses$1.invoke(LazyJavaClassMemberScope.kt:626)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassMemberScope$nestedClasses$1.invoke(LazyJavaClassMemberScope.kt:64)
    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$MapBasedMemoizedFunction.invoke(LockBasedStorageManager.java:408)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassMemberScope.getContributedClassifier(LazyJavaClassMemberScope.kt:651)
    at kotlin.reflect.jvm.internal.impl.resolve.scopes.InnerClassesScopeWrapper.getContributedClassifier(InnerClassesScopeWrapper.kt:29)
    at kotlin.reflect.jvm.internal.impl.resolve.jvm.JavaDescriptorResolver.resolveClass(JavaDescriptorResolver.kt:38)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.SingleModuleClassResolver.resolveClass(ModuleClassResolver.kt:29)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.types.JavaTypeResolver.computeTypeConstructor(JavaTypeResolver.kt:132)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.types.JavaTypeResolver.computeSimpleJavaClassifierType(JavaTypeResolver.kt:114)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.types.JavaTypeResolver.transformJavaClassifierType(JavaTypeResolver.kt:90)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.types.JavaTypeResolver.transformJavaType(JavaTypeResolver.kt:52)
    at kotlin.reflect.jvm.internal.impl.load.java.lazy.descriptors.LazyJavaClassDescriptor$LazyJavaClassTypeConstructor.computeSupertypes(LazyJavaClassDescriptor.kt:178)
    at kotlin.reflect.jvm.internal.impl.types.AbstractTypeConstructor$supertypes$1.invoke(AbstractTypeConstructor.kt:35)
    at kotlin.reflect.jvm.internal.impl.types.AbstractTypeConstructor$supertypes$1.invoke(AbstractTypeConstructor.kt:22)
    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:
4

0 回答 0