Android Architecture Components - ViewModel Observable & Proguard

I am unable to get 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 activity do not receive any data. This works fine until I turn on Proguard, so I know that Proguard is the reason, I just don't know why (new to Proguardian here). What rule needs to be added to make 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 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 get: D / SlideshowViewModel: setting the following image: 0 of 18

But nothing happens in the onChanged Observable.

+5
source share
1 answer

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

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

 ## Android architecture components: Lifecycle # LifecycleObserver empty constructor is considered to be unused by proguard -keepclassmembers class * implements android.arch.lifecycle.LifecycleObserver { <init>(...); } # ViewModel 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 *; } 
+5
source

Source: https://habr.com/ru/post/1269239/


All Articles