RuntimeException if minifyEnabled 对于 ViewModel 类为真



如果minifyEnabledfalse一切正常。但是在我将minifyEnabled更改为true运行时异常后:

java.lang.RuntimeException: Unable to start activity ComponentInfo{.ui.main.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class .ui.main.MainViewModel

我的MainActivity类扩展了AppCompatActivity,并实现了LifecycleRegistryOwner,然后我声明:

private val lifecycleRegistry: LifecycleRegistry by lazy { LifecycleRegistry(this) }
private val viewModel: MainViewModel by lazy { ViewModelProviders.of(this).get(MainViewModel::class.java) }
override fun getLifecycle(): LifecycleRegistry = lifecycleRegistry
// and other simple stuff

也许我应该在我的保护规则文件中放一些东西吗?

完整的异常日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.appchamp.wordchunks, PID: 32558
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appchamp.wordchunks/com.appchamp.wordchunks.ui.main.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.appchamp.wordchunks.ui.main.MainViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.appchamp.wordchunks.ui.main.MainViewModel
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(Unknown Source)
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source)
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source)
at com.appchamp.wordchunks.ui.main.l.a(Unknown Source)
at a.e.a(Unknown Source)
at com.appchamp.wordchunks.ui.main.MainActivity.c(Unknown Source)
at com.appchamp.wordchunks.ui.main.MainActivity.onStart(Unknown Source)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265)
at android.app.Activity.performStart(Activity.java:6915)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at com.appchamp.wordchunks.ui.main.l.a(Unknown Source) 
at a.e.a(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.c(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.onStart(Unknown Source) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265) 
at android.app.Activity.performStart(Activity.java:6915) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.AssertionError: impossible
at java.lang.Enum$1.create(Enum.java:45)
at java.lang.Enum$1.create(Enum.java:35)
at libcore.util.BasicLruCache.get(BasicLruCache.java:54)
at java.lang.Enum.getSharedConstants(Enum.java:211)
at java.util.EnumMap.initialization(EnumMap.java:738)
at java.util.EnumMap.<init>(EnumMap.java:391)
at io.realm.ad.<init>(Unknown Source)
at io.realm.ad.a(Unknown Source)
at io.realm.ad.a(Unknown Source)
at io.realm.aa.l(Unknown Source)
at com.appchamp.wordchunks.ui.main.MainViewModel.<init>(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at com.appchamp.wordchunks.ui.main.l.a(Unknown Source) 
at a.e.a(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.c(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.onStart(Unknown Source) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265) 
at android.app.Activity.performStart(Activity.java:6915) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.NoSuchMethodException: values []
at java.lang.Class.getMethod(Class.java:630)
at java.lang.Class.getDeclaredMethod(Class.java:589)
at java.lang.Enum$1.create(Enum.java:41)
at java.lang.Enum$1.create(Enum.java:35) 
at libcore.util.BasicLruCache.get(BasicLruCache.java:54) 
at java.lang.Enum.getSharedConstants(Enum.java:211) 
at java.util.EnumMap.initialization(EnumMap.java:738) 
at java.util.EnumMap.<init>(EnumMap.java:391) 
at io.realm.ad.<init>(Unknown Source) 
at io.realm.ad.a(Unknown Source) 
at io.realm.ad.a(Unknown Source) 
at io.realm.aa.l(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainViewModel.<init>(Unknown Source) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at android.arch.lifecycle.ViewModelProvider.get(Unknown Source) 
at com.appchamp.wordchunks.ui.main.l.a(Unknown Source) 
at a.e.a(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.c(Unknown Source) 
at com.appchamp.wordchunks.ui.main.MainActivity.onStart(Unknown Source) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265) 
at android.app.Activity.performStart(Activity.java:6915) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  end

我能够通过将以下行添加到我的proguard-rules.pro文件中来解决问题。

-keep class * extends androidx.lifecycle.ViewModel { *; }

就我而言,我使用的是自定义 ViewModelProvider,这意味着我的init()语句包含许多参数。 如果您没有使用自定义 ViewModelProvider,您也许可以按照 Android ViewModel ProGuard 示例文件中的说明执行以下操作:

-keep class * extends androidx.lifecycle.ViewModel {
<init>();
}

当你把minifyEnabled设置为true时,proguard会缩小,混淆你的模型类,活动无法识别它,在你的专业 guard-rules.pro 请添加以下行

-keep class com.appchamp.wordchunks.ui.main.MainViewModel;

要使用 ProGuard 启用代码收缩,请将 minifyEnabled true 添加到 build.gradle 文件中的相应构建类型。

来自 build.gradle文件的以下代码片段为发布版本启用代码缩减:

android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
...
}

相关内容

  • 没有找到相关文章

最新更新