Kotlin在21岁以下的Android下使用MultiDex代表

时间:2017-11-10 06:59:18

标签: android kotlin multidex kotlin-reflect kotlin-delegate

我有以下代表......

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()或任何其他代码   MultiDex.install()完成之前的反射或JNI。 Multidex   跟踪不会跟随这些调用,导致ClassNotFoundException或   验证由于DEX文件之间的类分区错误而导致的错误。

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

  • 应用程序崩溃发生在Android 21以下
  • kotlin delegate使用反射
  • 为什么从字段变量更改为局部变量是解决问题?
  • Logcat没有帮助

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:

0 个答案:

没有答案