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

Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105

4 Answers4

6

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

miha_dev
  • 161
  • 4
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.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • 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 handled – Andrii Turkovskyi Aug 13 '19 at 09:40
0

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

Ho Luong
  • 726
  • 8
  • 12
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.

Ben Russell
  • 61
  • 1
  • 4