如果使用ViewModel类,则minifyEnabled为true,则为RuntimeException

时间:2017-08-16 10:19:06

标签: java android exception kotlin 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

我应该在proguard-rules文件中放一些东西吗?

完整的例外日志:

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

4 个答案:

答案 0 :(得分:0)

我用Stanislav Bondar的帮助解决了我的问题,他给了我一个有用的链接gist.github.com/Jackgris/... - 它帮助了我。谢谢,斯坦尼斯拉夫!

答案 1 :(得分:0)

通过将以下行添加到我的<a href="maliciouslink.com">harmless link</a>文件中,我可以解决此问题。

proguard-rules.pro

就我而言,我使用的是自定义ViewModelProvider,这意味着我的-keep class * extends androidx.lifecycle.ViewModel { *; } 语句包括许多参数。如果您未使用自定义ViewModelProvider,则可以按照Android ViewModel ProGuard example file中的说明摆脱以下限制:

init()

答案 2 :(得分:-1)

当你将minifyEnabled设为true时,proguard缩小,模糊你的模型类,而Activity无法识别它,在你的pro guard-rules.pro中请添加以下行

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

答案 3 :(得分:-3)

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

build.gradle 文件中的以下代码段可以为发布版本启用代码缩减功能:

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