如果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

最佳答案

当您将minifyEnabled设置为true时,proguard会收缩,混淆模型类,而Activity无法识别它,请在pro guard-rules.pro中添加以下内容

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

07-24 22:15