LifecycleObserver produce exception with methods that use newer APIs
Asked Answered
T

4

6

My ViewModel class implements LifecycleObserver. When I call fragment.lifecycle.addObserver(this) it produces exception.

Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.

Strange, that firstly it was working fine, but not long ago this exception has appeared. I've found, that audioFocusRequest is cause of this bug.

private val audioFocusRequest by lazy {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
        .setOnAudioFocusChangeListener(this)
        .build() else throw RuntimeException("Can't be done for Android API lower than 26")
}

Does anybody know how it can be fixed?

UPD

Tried to use annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version", but got compilation error: enter image description here (decided to paste screenshot, because whole logs are quite big)

UPD 2
At the end I've decided to delete audioFocusRequest field and to use old deprecated method - requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) instead of recommended requestAudioFocus(@NonNull AudioFocusRequest focusRequest)

It helped me to make code working again, so it can be solution. But I didn't find answer - why this problem had appeared. It strange because code used to be working before.

So problem has been solved but question still stays unanswered

Teferi answered 6/2, 2019 at 14:20 Comment(2)
"GradleException: Compilation error. See log for more details" have you found anything of interest in the log?Amphitheater
Nothing at all.Teferi
G
6

Try to use kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"

Gipsy answered 5/3, 2019 at 7:27 Comment(4)
Why should one try that, and how? Please add some explanation to your answer such that others can learn from itDeranged
I tried to use kapt instead of annotationProcessor and project didn't build at all, there was an error during syncTeferi
it's really help to meEinberger
I tried both annotationProcessor and kapt and both at the same time since the project is a mix of Java and Kotlin, and this doesn't address the issueVasculum
K
0

The class which implements LifecycleObserver has some method, which has parameters with type that only exist for higher APIs.

Your variables (i guess) and function parameters must exist on all APIs even function is not called (maybe this is requirement for classes who implement LifecycleObserver).

A possible solution is to change function parameter type to Any (kotlin) or Object (Java) and cast it to appropriate type inside function.

Kordofanian answered 13/8, 2019 at 9:33 Comment(1)
As I've written - it was caused by AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) and it recommends higher API. Finally I've decided to delete audioFocusRequest field and to use old deprecated methods for handling audiofocus changes. But I still don't know what exactly cased this error and how it can be handledTeferi
S
0

I have to remove this set method on SpinnerView: lifecycleOwner = viewLifecycleOwner

Subtenant answered 2/4, 2020 at 11:1 Comment(0)
I
0

I was able to fix this by moving the offending methods into another class, but still called from my LifecycleObserver. After reading the error message again:

Caused by: java.lang.IllegalArgumentException: The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.

It seems as though no methods or objects are allowed in the class extending LifecycleObserver if they don't exist in the device's OS, even if they are wrapped in an SDK version check and never accessed.

Incrocci answered 24/6, 2020 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.