Android架构组件 – ViewModel Observable&Proguard

我无法使ViewModel组件与Proguard一起使用。 我已经不得不添加以下内容来防止由于NoSuchMethodException导致崩溃:init()

保持类com …. SlideshowViewModel {*;}

但是,我在活动中的观察员没有收到任何数据。 这工作正常,直到我启用Proguard,所以我知道Proguard是原因,我只是不知道为什么(新手Proguard在这里)。 我需要添加什么规则才能使观测值工作?

我有以下在我的ViewModel(Kotlin)

val currentItem = MediatorLiveData<MediaItem>() 

….后来…

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

和活动(Java)

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

在日志中,我收到:D / SlideshowViewModel:设置下一个图像:0出18

但是onChanged Observable中没有任何东西被解雇。

找到它: https : //issuetracker.google.com/issues/62113696

应该尽快修复(不在alpha3中)

 ## 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 *; }