When should we use android.arch.lifecycle:compiler (or android.arch.lifecycle:common-java8)?

AndroidAndroid Architecture-Components

Android Problem Overview


Currently, we are using LiveData, ViewModel and Room in our project.

We are using Java 8.

We use the following in build.gradle

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"

// Room (use 1.1.0-beta1 for latest beta)
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

I was wondering, when do we need to use

annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

(Or implementation "android.arch.lifecycle:common-java8:1.1.1" since we are using Java 8?!)

Currently, our code works fine, without using lifecycle:compiler or lifecycle:common-java8.

Android Solutions


Solution 1 - Android

> when do we need to use annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

AFAIK, that's only needed if you have lifecycle-related annotations in your code, specifically @OnLifecycleEvent.

> Or implementation "android.arch.lifecycle:common-java8:1.1.1" since we are using Java 8?

Same thing. The docs state "If your app uses Java 8, we recommend using this library instead of android.arch.lifecycle:compiler."

Solution 2 - Android

Lifecycle annotation processor dependency declaration for Java 8 should be as following:

implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

Instead of:

kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

Solution 3 - Android

Your options

As far as I can see you have 3 options:

LifecycleObserver

It's a marker interface, it doesn't have any methods. Your class will implement it and then you define a bunch of @OnLifecycleEvent methods as you need.

Lifecycle runtime will do one of 2 things:

  • Use reflection to look up the annotated methods,
  • or use generated adapters if you enabled the lifecycle-compiler annotation processor.

This interface is part of the lifecycle-common library.

LifecycleEventObserver

It provides a single method

void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event);

which will get called instead of any annotated methods.

This interface is part of the lifecycle-common library.

DefaultLifecycleObserver

It provides an interface with several empty methods:

default void onCreate(@NonNull LifecycleOwner owner) {}
default void onStart(@NonNull LifecycleOwner owner) {}
default void onResume(@NonNull LifecycleOwner owner) {}
default void onPause(@NonNull LifecycleOwner owner) {}
default void onStop(@NonNull LifecycleOwner owner) {}
default void onDestroy(@NonNull LifecycleOwner owner) {}

Your class will implement this interface and you can pick which methods to implement.

This interface is part of the lifecycle-common-java8 library. Interfaces with some implemented methods (default methods) are supported since Java 8. If your project has enabled Java 8 language features you can use it.

What to make of it

LifecycleEventObserver and DefaultLifecycleObserver add methods to your class, this may not be what you want. It's definitely not what I like.

I'd expect that you create a method with semantically accurate name and tell the Lifecycle framework only when it should call it. Like so:

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void startListening();

It doesn't pollute your class with extra methods. And you can use the annotation processor to make it faster at run-time. (The generated adapter is still looked up using reflection.)

I find this statement from Lifecycle release notes inaccurate:

> annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor > // alternately - if using Java8, use the following instead of lifecycle-compiler > implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

The compiler generates an adapter so you don't have to alter your class' interface. It works totally different from DefaultLifecycleObserver.

Solution 4 - Android

android.arch.lifecycle:compiler:1.1.1 is used when you write custom class implementing LifecycleObserver

public class MyObserver implements LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void connectListener() {
        ...
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void disconnectListener() {
        ...
    }
}

extracted from https://developer.android.com/topic/libraries/architecture/lifecycle.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCheok Yan ChengView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidBennyView Answer on Stackoverflow
Solution 3 - AndroidEugen PechanecView Answer on Stackoverflow
Solution 4 - AndroidSamuel EminetView Answer on Stackoverflow