Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Architecture Components - ViewModel Observable & Proguard

I'm having trouble getting the ViewModel component to work with Proguard. I already had to add the following to prevent a crash due to NoSuchMethodException: init()

-keep class com....SlideshowViewModel {*;}

However, my observers in the activity aren't receiving any data. This works fine until I enable Proguard, so I know Proguard is the reason, I just don't know why (novice Proguardian here). What rule do I need to add to make the observables work?

I have the following in my ViewModel (Kotlin)

val currentItem = MediatorLiveData<MediaItem>()

....later...

        Timber.d("Setting next image: " + position + " out of " + mediaItemList.size)
        currentItem.value = mediaItemList[position]

and the Activity (Java)

    viewModel.getCurrentItem().observe(this, new Observer<MediaItem>() {
        @Override
        public void onChanged(@Nullable final MediaItem mediaItem) {
            Timber.d("Activity received new item");
        }
    });

In the log I receive: D/SlideshowViewModel: Setting next image: 0 out of 18

But nothing get's fired in the onChanged Observable.

like image 869
easycheese Avatar asked Jun 27 '17 00:06

easycheese


People also ask

What is observable in MVVM Android?

Observability refers to the capability of an object to notify others about changes in its data. The Data Binding Library allows you to make objects, fields, or collections observable. Any plain-old object can be used for data binding, but modifying the object doesn't automatically cause the UI to update.

What is difference between observable and LiveData?

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

What is ViewModel and Android ViewModel?

AndroidViewModel. Application context aware ViewModel . ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment . It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).


1 Answers

Found it on: https://issuetracker.google.com/issues/62113696

It should be fixed soon though (not in alpha3 yet)

## Android architecture components: Lifecycle
# LifecycleObserver's empty constructor is considered to be unused by proguard
-keepclassmembers class * implements android.arch.lifecycle.LifecycleObserver {
    <init>(...);
}
# ViewModel's empty constructor is considered to be unused by proguard
-keepclassmembers class * extends android.arch.lifecycle.ViewModel {
    <init>(...);
}
# keep Lifecycle State and Event enums values
-keepclassmembers class android.arch.lifecycle.Lifecycle$State { *; }
-keepclassmembers class android.arch.lifecycle.Lifecycle$Event { *; }
# keep methods annotated with @OnLifecycleEvent even if they seem to be unused
# (Mostly for LiveData.LifecycleBoundObserver.onStateChange(), but who knows)
-keepclassmembers class * {
    @android.arch.lifecycle.OnLifecycleEvent *;
}
like image 115
easycheese Avatar answered Nov 03 '22 02:11

easycheese