89

In same cases Android studio displays warning message when I call observe method of LiveData object

viewModel.emailValidationResult.observe(viewLifecycleOwner, {onEmailChanged(it)})

Candidate resolution will be changed soon, please use fully qualified name to invoke the following closer candidate explicitly:

public open fun observe(owner: LifecycleOwner, observer: Observer<in Int?>): Unit defined in androidx.lifecycle.LiveData

As I understand it's happened after updating kotlin 1.4 What does it actually mean ?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47

6 Answers6

133

It means that the extension in androidx is not needed anymore.

Simply remove its import import androidx.lifecycle.observe.

It will be actually deprecated in androidx. Read more reasoning there.

EDIT:

Please note the "issue" from Erik Hoogendoorn

This change causes values from the observed LiveData object to be interpreted as nullable(since the converted lambda syntax is based on nullable Java code). This was not the case for the Kotlin extension and causes a loss in functionality for the user. In my opinion this change should be reverted and a different solution found.

I'm curious if they will rename & retain the helper or come up with another solution.

hrach
  • 2,443
  • 2
  • 26
  • 37
  • 2
    The helper livedata-ktx existed only as a crutch before Kotlin 1.4 was able to do proper SAM conversions on this method's callback parameter. There is no functionality loss as the values observed are actually not nullable, they are "platform type" which means you can ignore the possibility of null in kotlin unless you need to handle it. The nullability issue of the kotlin extension was because it was actually written in kotlin and mistakenly assigned a nonnull type. I feel a bit bad about that because it was added to fix a bug I had reported https://issuetracker.google.com/issues/111179594 – Josh Oct 07 '20 at 20:08
  • @Josh, out of curiosity, what was the Kotlin 1.3 issue that prevented SAM conversion here? – Tenfour04 Nov 07 '20 at 16:35
  • @Tenfour04 was just weakness in the type inference algorithm. One of the major improvements of Kotlin 1.4 compiler was to type inference. – David Pisoni Nov 13 '20 at 01:06
  • Removing only the import will again ask to re-Import it. So, remove the import `import androidx.lifecycle.observe` and then `viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->` ` ` `}) ` – skafle May 08 '21 at 13:09
  • 1
    Not really a solution when other imports from `androidx.lifecycle` are required though, since it either makes a very long list, or tools like IDEA will merge them eventually anyway. – RedGlyph Mar 18 '22 at 15:46
  • If the warning persists because your IDE insists on importing `androidx.lifecycle.*`, try using the `observe(owner::getLifecycle)` form of the method from @skafle's answer. More verbose, but warning-free. – Sterling Jan 06 '23 at 16:39
34

AndroidX extension is deprecated, so we have to use the original one.

Remove import androidx.lifecycle.observe

then

viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->
    
})

or

viewModel.liveData.observe(viewLifecycleOwner) { result ->
                
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
BollMose
  • 3,002
  • 4
  • 32
  • 41
2

I tried remove the "import androidx.lifecycle.observe" and converted our observers like the following:

// From this
livedata.observe(viewLifecycleOwner, Observer<MyClass?> {
    // do stuff with it
})

// To this
livedata.observe(viewLifecycleOwner) {
    // do stuff with it
}

The issue occured when we need to remove the observer inside the onChange call, e.g.

    // From this
    livedata.observe(viewLifecycleOwner, object : Observer<MyClass?> {
        override fun onChanged(t: MyClass?) {
            // Do stuff with it t
            livedata.removeObserver(this)
        }
    })
    
    // To this
    livedata.observe(viewLifecycleOwner) {
        // do stuff with it
        livedata.removeObserver(this) <--- not working ofc
    }

I've heard rumors that folks have broken out the observer as a variable and thus been able to add/remove it by referencing the variable. However I'm unable to figure out how to instantiate a local observer without using the "Observer<MyClass?> syntax.

Atte Backenhof
  • 468
  • 5
  • 7
2

If you are observing LiveData in ViewModel and encountered this issue then you might need to call owner::getLifecycle instead.

class MyViewModel: ViewModel(), DefaultLifecycleObserver {
 
    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)

        repository.myAwesomeMethod().observe(owner) {
            // This will show warning.
        }
    }
}

Now replace owner with owner::getLifecycle and you are good.

class MyViewModel: ViewModel(), DefaultLifecycleObserver {
 
    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)

        repository.myAwesomeMethod().observe(owner::getLifecycle) {
            // Do your things
        }
    }
}
skafle
  • 627
  • 1
  • 6
  • 16
  • Nice. This is a good solution when your IDE won't let you simply remove the `import`, as the accepted answer says. – Sterling Jan 06 '23 at 16:41
0

The same issue occurred my code base too. The kotlin version is '1.4.32'

After removing below import sentiment, the warning disappeared.

import androidx.lifecycle.observe
burak isik
  • 395
  • 5
  • 6
-2

In my case I used Kotlin 1.4.31 in buildgradle(project level) file.

 ext {
    kotlin_version = '1.4.31'
}

 dependencies {
 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

The warning/deprecation error was gone.

Tonnie
  • 4,865
  • 3
  • 34
  • 50