1

I am having a library for SDK that extends both Application.ActivityLifecycleCallbacks, LifecycleObserver. In the constructor, I am registering the class as an observer for ProcessLifecycleOwner.

ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

For the method that does ON_START work, I have added the following annotation for the same.

@OnLifecycleEvent(Lifecycle.Event.ON_START)

This works completely fine in a local and testing environment. Even for the release version, it works fine. But very rarely it fails with the following error:

Caused by java.lang.NoSuchFieldException: ON_START
   at java.lang.Class.getDeclaredField(Class.java:929)
   at libcore.reflect.AnnotationAccess.decodeValue(AnnotationAccess.java:685)
   at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:663)
   at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:641)
   at libcore.reflect.AnnotationAccess.getDeclaredAnnotation(AnnotationAccess.java:170)
   at java.lang.reflect.Method.getAnnotation(Method.java:301)
   at android.arch.lifecycle.ClassesInfoCache.createInfo(SourceFile:124)
   at android.arch.lifecycle.ClassesInfoCache.hasLifecycleMethods(SourceFile:59)
   at android.arch.lifecycle.Lifecycling.resolveObserverCallbackType(SourceFile:137)
   at android.arch.lifecycle.Lifecycling.getObserverConstructorType(SourceFile:119)
   at android.arch.lifecycle.Lifecycling.getCallback(SourceFile:57)
   at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.<init>(SourceFile:347)
   at android.arch.lifecycle.LifecycleRegistry.addObserver(SourceFile:162)

This error is very rare. I have only seen this happen for Android 5.0, 5.0.1 and 5.0.2 on device Samsung.

I am using android.arch.lifecycle:extensions:1.1.1.

Can someone please help me with this?

1 Answers1

0

Apparently there is some bug on Samsung devices for which reflection fails, very occasionally though.

I posted this on google issue tracker and got the below answer:

https://issuetracker.google.com/issues/124584380

Solution is to migrate to Java 1.8 / Kotlin and use DefaultLifecycleObserver that internally does not use reflection.

Haven't tried this solution yet. Will post if it worked or not.