Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal access error while using FullLifecycleObserver

I am using FullLifecycleObserver in my project since long time but lately after updating my App's Android Gradle Plugin to 7.0.2, I am getting the following crash .

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.demo, PID: 463
    java.lang.IllegalAccessError: Interface androidx.lifecycle.FullLifecycleObserver implemented by class com.myapp.AndroidXLifecycle$Wrapper is inaccessible (declaration of 'com.myapp.AndroidXLifecycle$Wrapper' appears in /data/app/~~7rat7QSnMLMmpeTBuQ9x4Q==/com.example.demo-NJOtI6dh-fR3aEGN-YGrjg==/base.apk)

On further digging I found that Flutter developers have also faced this issue and declaring changes in Proguard file would work for them like the one below :

-keep class androidx.lifecycle.FullLifecycleObserver
 

But that did not make any difference as well.

Is there any solution to this other than downgrading gradle plugin version ?

like image 233
Madhur Lalit Avatar asked Aug 26 '26 12:08

Madhur Lalit


1 Answers

Found error and solution .

Error : After Android Gradle Plugin update, accessing FullLifecycleObserver has been an issue as using it does not give any compile time warning but causes issue in resolving its classpath on runtime and hence causes this error. I used this for its Lifecycle callbacks (onResume ,onPause, onStop etc) to handle Lifecycle observer

Solution : Using androidx.lifecycle.Lifecycle.Event which is an enum indicating different states of Lifecycle and LifecycleEventObserver which provides onStateChanged callback under which the Lifecycle.Event can be handled is a great alternative to this. I used this and I am not facing this error now.

Sample code below :

public class MyLifecycle implements LifecycleEventObserver {

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull androidx.lifecycle.Lifecycle.Event event) {
        switch (event){
            case ON_CREATE:
                break;
            case ON_START:
                break;
            case ON_RESUME:
                break;
            case ON_PAUSE:
                break;
            case ON_STOP:
                break;
            case ON_DESTROY:
                break;
            case ON_ANY:
                break;
        }
    }
}
like image 123
Madhur Lalit Avatar answered Aug 29 '26 21:08

Madhur Lalit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!