Skip to content
Advertisement

AndroidViewModel has no zero argument constructor. How to solve this?

I’ve searched for my problem nearly whole the Internet and couldn’t find the answer, which I need. Yes. My question is not unique, but there was no answer, which could help me. So I decided to ask it myself.

Now about the problem:

JavaScript

……………………………………..

JavaScript

………….

Error:

JavaScript
  1. I tried to add a constructor with no arguments in class AuthActivityViewModel, but got this error

There is no default constructor available in androidx.lifecycle.AndroidViewModel

  1. Advices like

    are not actual. Why? Cause they were already as they should be, but I have the error which must not be. As the problem exists, there must be something that I didn’t count.

Here is some part of the gradle file:

def lifecycle_version = “2.2.0” implementation “androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version”

JavaScript
JavaScript
JavaScript
JavaScript
JavaScript

How can I solve this problem?

Advertisement

Answer

Add the below dependency to gradle app module level.

implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle_version

Extensions include many libraries including LiveData, So, by adding it, you can get rid of:

implementation 'androidx.lifecycle:lifecycle-livedata:$lifecycle_version

But it appears lifecycle-extensions has been deprecated. Is there another dependency that solves this issue?

That is right it’s been deprecated as of version:2.2.0.

lifecycle-extensions Artifact Deprecation: With the above deprecation of ViewModelProviders.of(), this release marks the deprecation of the last API in lifecycle-extensions and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such as lifecycle-service if you’re using LifecycleService and lifecycle-process if you’re using ProcessLifecycleOwner) rather than lifecycle-extensions as there will not be a future 2.3.0 release of lifecycle-extensions.

As quoted by documentation, you can instead use the specific Lifecycle artifacts: And as we discussed in comments the specific lifecycler aritifacts that works was:

implementation "android.arch.lifecycle:runtime:$lifecycle_version

And also keep the other lifecycler dependency artifacts of yours.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement