leakcanary: LeakCanary crashing app on start: java.lang.AbstractMethodError: androidx.lifecycle.ViewModel

Description

Hello in our app we use LeakCanary library. We have recently upgraded gradle, kotlin gradle scripts to KTS, etc. Now when we build APK through Android Studio by “Generate Signed APK” or “Generate APKs” or in terminal by “gradlew assembleSomethingDebug” as Debug type it causing crash on start app. Once I remove the LeakCanary library from debugImplementation in build.gradle.kts. It will stop crashing.

Log:

  java.lang.AbstractMethodError: abstract method "androidx.lifecycle.ViewModel androidx.lifecycle.ViewModelProvider$Factory.create(java.lang.Class, androidx.lifecycle.viewmodel.CreationExtras)"
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:168)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:137)
        at leakcanary.internal.ViewModelClearedWatcher$Companion.install(ViewModelClearedWatcher.kt:52)
        at leakcanary.internal.AndroidXFragmentDestroyWatcher.invoke(AndroidXFragmentDestroyWatcher.kt:66)
        at leakcanary.internal.AndroidXFragmentDestroyWatcher.invoke(AndroidXFragmentDestroyWatcher.kt:25)
        at leakcanary.FragmentAndViewModelWatcher$lifecycleCallbacks$1.onActivityCreated(FragmentAndViewModelWatcher.kt:59)
        at android.app.Application.dispatchActivityCreated(Application.java:353)
        at android.app.Activity.dispatchActivityCreated(Activity.java:1334)
        at android.app.Activity.onCreate(Activity.java:1607)
        at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:85)
        at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:343)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:246)
        at com.ilab.spaceti.ui.base.BaseActivity.onCreate(BaseActivity.kt:99)

Version Information

  • LeakCanary version: 2.8.1
  • Android OS version: 12
  • Gradle version: 7.4
  • Android Gradle Plugin: 7.1.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 18
  • Comments: 15 (7 by maintainers)

Most upvoted comments

This is absolutely not the right fix. LeakCanary should not declare an explicit dependency on lifecycle, the whole point of it being optional is that consumers can leverage whichever version they like or even not have that dependency and everything will work just fine. Lifecycle just broke backward compatibility.

Same thing is happening for me also. But I could not understand the actual reason, but now i understand the reason and trying to find the solution. Removing the dependency is just a workaround.