lixi
lixi

Reputation: 31

Accidental override by migrate to androidx

kotlin: 1.2.71 andorid studio: 3.2.1 gradle plugin: 3.2.0 gradle: 4.6


I`m getting an "accidental override" error(code sample from below), if u find any wrong use, please tell me, be appreciated.

abstract class ResourceLiveData<T: Resource<*>> : LiveData<T>(){

    override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
        do something here ...
        super.observe(owner, observer)
    }
}

demo impl code:

// Inherited platform declarations clash: The following declarations have the same JVM signature (observe(Landroidx/lifecycle/LifecycleOwner;Landroidx/lifecycle/Observer;)V):
//   fun observe(owner: LifecycleOwner, observer: Observer<Resource<String>>): Unit defined in  com.demo.live.StringResourceLiveData 
//   fun observe(p0: LifecycleOwner, p1: Observer<in Resource<String>!>): Unit defined in com.demo.live.StringResourceLiveData 

class StringResourceLiveData : ResourceLiveData<Resource<String>>()

UPDATE

The ResourceLiveData is in the third-party-lib, and the lib dependence android.arch.lifecycle.

My App is migrating to androidx, so, i add android.useAndroidX=true and android.enableJetifier=true in gradle.properties.

After sync, ResourceLiveData.class import have change to androidx, but problem here, ResourceLiveData override method not contain contravariant in, but androidx.lifecycle.LiveData need it

Upvotes: 1

Views: 421

Answers (1)

Kirill Rakhman
Kirill Rakhman

Reputation: 43831

Your code has a bunch of issues.

  • ResourceLiveData needs to be open if you want to extend it.
  • The override is defined incorrectly. I used the IDE to generate the correct version
  • Extending from a class requires you to call the super class constructor ()

Here's the corrected code

open class ResourceLiveData<T: Resource<*>> : LiveData<T>(){
    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        do something here ...
        super.observe(owner, observer)
    }
}

class StringResourceLiveData : ResourceLiveData<Resource<String>>()

Upvotes: 5

Related Questions