Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 492
Cheok Yan Cheng Avatar asked Apr 01 '18 20:04

Cheok Yan Cheng


3 Answers

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."

like image 163
CommonsWare Avatar answered Nov 09 '22 16:11

CommonsWare


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"
like image 49
Benny Avatar answered Nov 09 '22 15:11

Benny


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.

like image 18
Eugen Pechanec Avatar answered Nov 09 '22 15:11

Eugen Pechanec