0

I am trying to work on a sample project to learn MvRx. However, seems something is wrong. Android Studio is not able to find and import activityViewModel automatically.

1) I tried to import it manually by writing its package name but it is still gray.

2) From Gradle tab, I selected my root project and clicked on Refresh Gradle Project in order to refresh all dependencies. It did not help, too.

What is the problem?

enter image description here

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
Hesam
  • 52,260
  • 74
  • 224
  • 365

3 Answers3

2

If someone is getting same error even when using activityViewModel() in fragment, this answer may be helpful.

In my case I was using activityViewModel() inside fragment. Still I was getting this as well as many other errors. Finally I figured out that mvrx is now using kotlin coroutines and all dependencies on rxjava are removed in 2.0.0-beta1.

To solve this use

implementation "com.airbnb.android:mvrx-rxjava2:2.0.0-beta3"

in place of

implementation "com.airbnb.android:mvrx:2.0.0-beta3"

in dependencies section of your build.gradle.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
0

Your feature code must be in a Fragment (that extends BaseMvRxFragment), not in an Activity.

Gabe
  • 1,239
  • 1
  • 13
  • 20
  • Yes, you are right. I did that after I realized I'm unable to implement my code in this way. Anyways, thank you for your answer. – Hesam Apr 11 '19 at 18:51
0

Because you have other com.airbnb.mvrx. references that have resolved correctly, it means you do have a reference to the com.airbnb.mvrx library. However, it is likely that you have a different version of the library referenced than the sample's original author. Look in your build.gradle file for dependencies and see if you have the library referenced there. If so, compare its version to the one in the sample, if different, then modify your reference to the sample's version number. If not, then add a reference to the library with the appropriate version number of the library where that object exists.

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.airbnb.mvrx:mvrx:12.0.1'

}

Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • Thanks, Michael for your answer. that is internal reference I guess. I have this line in my gradle file: `implementation 'com.airbnb.android:mvrx:1.0.0'` which is coming from https://github.com/airbnb/MvRx#installation – Hesam Apr 11 '19 at 20:08
  • See if you can find any documentation on the activityViewModel in that library. Sometimes, they will have information as to which version of the library it was available in. Perhaps that component is only available in version 1.1.0 of the library (or whatever you discover from the documentation), in which case you would change your gradle file to read: implementation 'com.airbnb.android:mvrx:1.1.0' Then, you will need to resynch your gradle files. – Michael Dougan Apr 11 '19 at 21:16